Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Punal Chotrani
Punal Chotrani
8,817 Points

Data not saving in a newly created custom fields in a custom post.

I have created a new custom post type "skills", and added some fields to it.

  • Fields 1: a normal text metabox
  • Field 2: a Radio buttons, which when selected should display on the frontend.

I have 2 issues. Both the data is not saving, when i hit update, and when it updates i need to save it on the front-end.

Below is the code.

function true_add_a_metabox() {
    add_meta_box(
        'true_metabox', 
        'Skill Level Percentage',
        'true_display_metabox',
        'skills',
        'normal',
                'high' 
    );
}
add_action( 'admin_menu', 'true_add_a_metabox' );

function true_display_metabox($post) {
    wp_nonce_field( basename( __FILE__ ), 'true_metabox_nonce' );
        $html .= '<p><label>Enter Skill % <input type="text" name="seotitle" value="' . get_post_meta($post->ID, 'true_title',true) . '" /></label></p>';

    $html .= '<p><label><input type="radio" name="noindex"';
    $html .= (get_post_meta($post->ID, 'true_noindex',true) == 'on') ? ' checked="checked"' : '';
    $html .= ' /> Expert</label></p>';
    $html .= '<p><label><input type="radio" name="noindex"';
    $html .= (get_post_meta($post->ID, 'true_noindex',true) == 'on') ? ' checked="checked"' : '';
    $html .= ' /> Proficient</label></p>';
    $html .= '<p><label><input type="radio" name="noindex"';
    $html .= (get_post_meta($post->ID, 'true_noindex',true) == 'on') ? ' checked="checked"' : '';
    $html .= ' /> Basic</label></p>';
    $html .= '<p><label><input type="radio" name="noindex"';
    $html .= (get_post_meta($post->ID, 'true_noindex',true) == 'on') ? ' checked="checked"' : '';
    $html .= ' /> Learning</label></p>';
    /*
     * print all of this
     */
    echo $html;
}

Any help with this would be great.

Thanks

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Punal,

Have a read through the following article as you're missing the save functionality for your custom fields as WordPress doesn't handle this automatically for us.

http://www.wproots.com/complex-meta-boxes-in-wordpress/

Punal Chotrani
Punal Chotrani
8,817 Points

Hi Chris,

I thought as much, i had to do a save function.

Thanks for your help, great link.

Chris Shaw
Chris Shaw
26,676 Points

No worries, glad you got it sorted.