Add custom field in course unit

I need to add custom fields in course unit page in admin. But add_meta_boxes and other tries are failed. I also tried with custompress but that didn't help either. Any help is appreciated.

  • Panos

    Hi André ,

    Apologies for delay!

    I'm afraid this is quite hard to accomplish since this is done with backbone and ajax. Even changing the files for that is very complex.

    Only way I can think of is adding a complete custom block at the bottom of that page and a custom "save" button. Then use some ajax to save.

    As this requires custom coding it exceeds the purpose of this forum. I can give some sample code where you can change only the meta keys, or add more if you like. I can't guarantee it will be exactly what you want. It is to be used mostly as a guide which you can modify to meet your needs:

    add_action( 'wp_ajax_wpmudev_cp_save_units_custom_meta', function(){
    	check_ajax_referer( 'wpmudev_cp_save_units_custom_meta', 'security' );
    	$return = array(
            'success'	    => true,
        $units_meta = isset( $_POST['units_meta'] ) ? $_POST['units_meta'] : null;
        if( ! is_null( $units_meta ) ){
        	foreach( $units_meta as $meta ){
        		$mixed_key = explode('%%', $meta['name']);
        		$unit_id = $mixed_key[0];
        		$meta_key = $mixed_key[1];
        		update_post_meta( $unit_id, $meta_key, $meta['value'] );
    }, 10 );
    add_action( 'dbx_post_sidebar', function(){
    	if( ! isset( $_GET['tab'] ) || 'units' != $_GET['tab'] ){
    	$units = CoursePress_Data_Course::get_units( $_GET['post'] );
    	$rows = '';
    	foreach( $units as $unit ){
    		$unit_custom_meta = get_post_meta( $unit->ID, 'unit_custom_meta_one', true );
    		$rows .= '<tr>';
    			$rows .= '<td style="width: 18%;"><h3>' . $unit->post_title . '</h3></td>';
    			$rows .= '<td>';
    				$rows .= '<p>';
    					$rows .= '<div><strong>Unit custom meta one:</strong></div>';
    					$rows .= '<input type="text" name="'. $unit->ID .'%%unit_custom_meta_one" value="'. $unit_custom_meta .'" />';
    				$rows .= '</p>';
    			$rows .= '</td>';
    		$rows .= '</tr>';
    	<div style="margin-left: 191px;">
    		<table class="widefat cp-units-custom-meta">
    			<?php echo $rows; ?>
    		<?php submit_button( 'Save', 'primary', 'save-unit-custom-meta', false ); ?>
    	<script type="text/javascript">
    				$( '#save-unit-custom-meta' ).on('click', function(e){
    					let units_meta = $( 'table.cp-units-custom-meta' ).find('input').serializeArray();
    					let data = {
                            action: 'wpmudev_cp_save_units_custom_meta',
                            security: '<?php echo wp_create_nonce( "wpmudev_cp_save_units_custom_meta" ); ?>',
                            units_meta: units_meta
                        $.post(ajaxurl, data, function(response) {
                            if( response.success ){
                                alert( 'Stored the custom meta' );
                                alert( 'Error in request' );
    }, 10 );

    Hope this helps!


Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.