Create new field using action in coursepress

Hi,

Suppose I want to add a new field in coursepress (course creation) which will be a dropdown. The dropdown will have options drawn from the course category taxonomy, wherein only the childs of a certain parent category will be present as the options (we can select parent in the args). Now, lets say I want to add this field to step 1 or step 2 of the course creation process, then how do I go about it using the action 'course_step_1_fields' or 'course_step2_fields' so that the values are saved in the database, and I can create a shortcode in wp-content/plugins/coursepress/includes/classes/class.shortcodes.php to use it further?

I was able to do this for a textbox, but a dropdown and other options are giving me trouble. Please help as soon as possible. Thanks :slight_smile:

  • Hoang Ngo

    Hi @Kunal,

    I hope you are well today.
    I assumed that, we have a dropdown field named test, which having 2 options. So we will append the element to step 1 by this code

    add_action( 'course_step_1_fields', 'course_step_1_fields' );
    function course_step_1_fields( $id ) {
    	$selected = get_post_meta( $id, 'test', true );
    	?>
    	<select name="test">
    		<option <?php echo selected($selected,'val1')  ?> value="val1">value 1</option>
    		<option <?php echo selected($selected,'val2')  ?> value="val2">value 2</option>
    	</select>
    	<?php
    }

    After the form get processed, we will store the field value into database. Here is the hook & sample code doing that.

    add_action( 'coursepress_course_created', 'update_new_data_to_course' );
    add_action( 'coursepress_course_updated', 'update_new_data_to_course' );
    function update_new_data_to_course( $id ) {
    	if ( isset( $_POST['dropval'] ) && ! empty( $_POST['dropval'] ) ) {
    		update_post_meta( $id, 'test', $_POST['dropval'] );
    	}
    }

    Unfortunately, we will need to modify the plugin code, to append the POST param sending to server. File /coursepress/js/coursepress-courses.js, line 642, you will see a code block like this

    return {
            // Don't remove
            action: initialVars[ 'action' ],
            course_id: initialVars[ 'course_id' ],
            course_name: initialVars[ 'course_name' ],
            course_nonce: initialVars[ 'course_nonce' ],
            user_id: initialVars[ 'user_id' ],
            // Alter as required
            course_excerpt: content,
            meta_featured_url: $( '[name=meta_featured_url]' ).val(),
            _thumbnail_id: _thumbnail_id,
            //meta_course_category: $('[name=meta_course_category]').val(),
            meta_course_language: $( '[name=meta_course_language]' ).val(),
            course_category: $( '[name=meta_course_category]' ).val(),
            // Don't remove
            meta_course_setup_progress: initialVars[ 'meta_course_setup_progress' ],
            meta_course_setup_marker: 'step-2'
        }

    Please update it to

    return {
            // Don't remove
            action: initialVars[ 'action' ],
            course_id: initialVars[ 'course_id' ],
            course_name: initialVars[ 'course_name' ],
            course_nonce: initialVars[ 'course_nonce' ],
            user_id: initialVars[ 'user_id' ],
            // Alter as required
            course_excerpt: content,
            meta_featured_url: $( '[name=meta_featured_url]' ).val(),
            _thumbnail_id: _thumbnail_id,
            //meta_course_category: $('[name=meta_course_category]').val(),
            meta_course_language: $( '[name=meta_course_language]' ).val(),
            course_category: $( '[name=meta_course_category]' ).val(),
            // Don't remove
            meta_course_setup_progress: initialVars[ 'meta_course_setup_progress' ],
            meta_course_setup_marker: 'step-2',
            dropval:$('[name="test"]').val()
        }

    You will see that we append the dropval value at the end. This is the param we will use to get the value submitted.

    If you have any additional issues, please let us know and we'll be happy to help.

    Best regards,
    Hoang

      • Kunal

        I have a little follow-up though Hoang if you dont mind. In your example, you have shown the option values as val1 and val2, which works great, but what if those values were being generated from the admin panel (sub category of course categories). I can fetch the values in an array like this:
        $args = array(
        'orderby' => 'name',
        'order' => 'ASC',
        'hide_empty' => false,
        'fields' => 'all',
        'hierarchical' => true,
        'parent' => '15',
        );

        but how would the function look like as per this? I tried to look at how the categories function works in plugins/coursepress/includes/admin-pages/courses-details-overview.php, but I am unable to perfectly replicate it for my need.

        As I was planning on editing this core file itself, you might have saved me a bit from that, but just to be clear all the code you mentioned except the 'js' file goes into my custom functions file, right?

        Again, thanks a lot for your input :slight_smile:

  • Sajid

    Hi @Kunal

    Hope you are doing good today :slight_smile:

    To list the child categories and show their values dynamically. Replace the code that Hoang gave you first to add your field in step on.

    add_action( 'course_step_1_fields', 'course_step_1_fields' );
    function course_step_1_fields( $id ) {
    	$selected = get_post_meta( $id, 'test', true );
    	?>
    	<select name="test">
    		<option <?php echo selected($selected,'val1')  ?> value="val1">value 1</option>
    		<option <?php echo selected($selected,'val2')  ?> value="val2">value 2</option>
    	</select>
    	<?php
    }

    With this one:

    add_action( 'course_step_1_fields', 'course_step_1_fields' );
    function course_step_1_fields( $id ) {
    	$selected = get_post_meta( $id, 'test', true );
    
    	$args = array(
    	'orderby' => 'name',
    	'order' => 'ASC',
    	'hide_empty' => false,
    	'fields' => 'all',
    	'hierarchical' => true,
    	 'parent' => '15',
    	);
    
    	$taxonomies = array(
    						'course_category'
    						);
    
    	$terms = get_terms( $taxonomies , $args );
    
    	?>
    	<label>Your label</label>
    	<select name="test">
    		<?php if ($terms) {
    				foreach ( $terms as $term ) {
    
    				?>
    
    		<option <?php echo selected($selected, $term->term_id )  ?> value="<?php echo $term->term_id; ?>" ><?php echo $term->name; ?></option>
    
    		<?php }
    		}?>
    
    	</select>
    	<?php
    }

    And yes, except js code, everything else will go into functions.php file or in mu-plugin.

    Take care and have a nice day :slight_smile:

    Cheers, Sajid

  • Kunal

    Hi,

    As per your instructions Sajid, the value of my field gets saved in the database, but it does not get updated on the admin panel. The count of those categories does not increase. I have attached a screenshot for this. A little background on the screenshot. I have two parent course categories named: location(id=15) & categories(id=18). I have changed the default functionality of the categories in /plugins/coursepress/includes/admin-pages/courses-details-overview.php to show only childs of parent "categories(id=18)" in the dropdown, and that works absolutely fine. I have used a separate action to create a dropdown with only childs of parent course category "location(id=15":wink:. Now, the data gets saved in db, but as mentioned, the count does not increase in the panel (the attachment will depict that).

    I also noticed while "course_categories" meta is being saved as "a:1:{i:0;s:1:"8";}" in the db but the new post meta that we just created only saves the id of the category, so eg. it only saves the value "17" or "16" in the database. As the count of my subcategories (parent id - 15, subcat id -16,17) does not increase, I am unable to successfully create a search form based on it. Please help me with this and guide me how to increase the count of my newly created action field? Thanks.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.