1

All I'm doing is creating a simple meta box for my posts that calls the existing tags, then displays them in a select drop down list for the editor to choose. I feel I'm so close in making this done...But my selected meta value is not saving when I save the post. Is there a step I need to reconsider or adjust, in order to save my data fully?

//meta box functions for adding the meta box and saving the data
add_action('add_meta_boxes','nffa_meta_box_tags');

function nffa_meta_box_tags() {    
add_meta_box('nffa_meta', 'Project Tag Status','nffa_meta_box','post','side','high');
}

function nffa_meta_box($post) {

    //GET TAGS
    $args = array(
        'orderby'    => 'name',
        'order'      => 'ASC',
        'hide_empty' => 0,
        'taxonomy'   => 'post_tag'
        );

    $categories=get_categories($args);
        foreach($categories as $category) { 
          $tags[] =  $category->name ;  
        }

    $prefix = 'NFFA_meta_';
    $mtags = array(
        'name' => 'tags',
        'desc' => 'Project Status:',
        'subdesc' => 'Please set the status of this project',
        'id' => $prefix . 'tags',
        'type' => 'select',
        'options' => $tags
    );

    print "<pre>";
    print_r($mtags);
    print "</pre>";

    //retrieve the custom meta box values
    $nffa_meta_value = get_post_meta($post->ID,$mtags['id'], true);
    //nonce for security
    wp_nonce_field(plugin_basename(__FILE__),'nffa_save_meta_box');
    //custom meta box form elements via select
    echo '<p>'.$mtags['desc'].' <select name="'. $mtags['id'].'" id="'.$mtags['id'].'">';
    foreach ($mtags['options'] as $vtag) {
        echo '<option', $nffa_meta_value == $vtag ? ' selected="selected"' : '', '>', $vtag, '</option>';
    }
    echo '</select>';
    echo '</p>';
    echo '<p class="howto" id="new-tag-post_tag-desc">'.$mtags['subdesc'].'</p>';
}


//save our meta data
add_action ('save_post','nffa_save_meta_box_tags');
function nffa_save_meta_box_tags($post_id) {
    //process form data if $_POST is set
    if (isset($_POST[$mtags['id']])) {
        //if auto saving skip saving our meta box data
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return;

        //check nonce for security
        check_admin_referer(
            plugin_basename(__FILE__),'nffa_save_meta_box');
            //save the metabox data as post meta using the post ID as a  unqiue prefix
            update_post_meta($post_id, $mtags['id'], santize_text_field($_POST[$mtags['id']]));
    }
}
2

in nffa_save_meta_box_tags(), you use $mtags, but $mtags is not defined in that function. Manually replacing the two instances of $mtags['id'] with 'NFFA_meta_tags' makes the value save - showing that's part of the problem.

The other problem is that santize_text_field() has a typo, should be sanitize_text_field().

  • Many thanks in helping me understand the scope of how our I was processing my variable data and for the misspelling. – klewis Aug 8 '18 at 20:49
  • This also opened my eyes in seeing how we can create a function that passes string values into other functions, which is the approach I am now taking. Thanks again! – klewis Aug 8 '18 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.