Question

My theme has code that places a custom meta box on the Page editor interface for toggling noindex and/or nofollow attributes to the page.

Its a simple little deal with two checkboxes, one says noindex, the other says nofollow.

These work flawlessly in all versions of WP prior to 3.0.1

I've found that in 3.0.1, the values of the checkboxes are not saved. No matter what I do, they always come up unchecked, and the values are not being passed to the database either (its not just reporting unchecked in the interface, it never sends the values to the db)

I'm at a loss at what's different and how to get my code to work with 3.0.1

Does anyone know of what might have changed to make these checkbox values not pass when the page is Updated in WP 3.0.1?

// ===================
// = POST OPTION BOX =
// ===================

add_action('admin_menu', 'my_post_options_box');

function my_post_options_box() {
    if ( function_exists('add_meta_box') ) { 
        add_meta_box('categorydiv', __('Page Index Options'), 'post_categories_meta_box_modified', 'page', 'side', 'high');
    }
}

//adds the custom categories box
function post_categories_meta_box_modified($post) {
global $post, $noindexCat, $nofollowCat;
    $noindexCat = get_cat_ID('noindex');
    $nofollowCat = get_cat_ID('nofollow');
    if(in_category("noindex")){ $noindexChecked = " checked='checked'";} 
    if(in_category("nofollow")){ $nofollowChecked = " checked='checked'";}
?>
<div id="categories-all" class="ui-tabs-panel">
    <ul id="categorychecklist" class="list:category categorychecklist form-no-clear">
        <li id='category-<?php echo $noindexCat ?>' class="popular-category"><label class="selectit"><input value="<?php echo $noindexCat ?>" type="checkbox" name="post_category[]" id="in-category-<?php echo $noindexCat ?>"<?php echo $noindexChecked ?> /> noindex</label></li> 
        <li id='category-<?php echo $nofollowCat ?>' class="popular-category"><label class="selectit"><input value="<?php echo $nofollowCat ?>" type="checkbox" name="post_category[]" id="in-category-<?php echo $nofollowCat ?>"<?php echo $nofollowChecked ?> /> nofollow</label></li> 
        <li id='category-1' class="popular-category" style="display:none;"><label class="selectit"><input value="1" type="checkbox" name="post_category[]" id="in-category-1" checked="checked"/> Uncategorized</label></li> 
    </ul>
</div>
<?php
}
Was it helpful?

Solution

First of all, this isn't using custom post meta ... you're trying to retrieve categories and use the presence of those categories to check the boxes. I also don't see any script to save the values once you've clicked them ... When this did work, I assume it was a very hackish solution, so I'm not surprised that it's unstable and has broken.

However, you can actually use custom post meta to do what it is you want to do. Use the following script instead:

// ===================
// = POST OPTION BOX =
// ===================

add_action('admin_menu', 'my_post_options_box');

function my_post_options_box() {
    if ( function_exists('add_meta_box') ) { 
        add_meta_box('categorydiv', __('Page Index Options'), 'post_categories_meta_box_modified', 'page', 'side', 'high');
    }
}

//adds the custom categories box
function post_categories_meta_box_modified() {
    global $post;
    if( get_post_meta($post->ID, '_noIndex', true) ) $noindexChecked = " checked='checked'";
    if( get_post_meta($post->ID, '_noFollow', true) ) $nofollowChecked = " checked='checked'";
?>
<div id="categories-all" class="ui-tabs-panel">
    <ul id="categorychecklist" class="list:category categorychecklist form-no-clear">
        <li id='noIndex' class="popular-category"><label class="selectit"><input value="noIndex" type="checkbox" name="chk_noIndex" id="chk_noIndex"<?php echo $noindexChecked ?> /> noindex</label></li> 
        <li id='noFollow' class="popular-category"><label class="selectit"><input value="noFollow" type="checkbox" name="chk_noFollow" id="chk_noFollow"<?php echo $nofollowChecked ?> /> nofollow</label></li>  
    </ul>
</div>
<?php
}

function save_post_categories_meta($post_id) {
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id;

    $noIndex = $_POST['chk_noIndex'];
    $noFollow = $_POST['chk_noFollow'];

    update_post_meta( $post_id, '_noIndex', $noIndex );
    update_post_meta( $post_id, '_noFollow', $noFollow );

    return $post_id;
}

add_action('save_post', 'save_post_categories_meta');

This will store Boolean flags in the custom post meta fields "noIndex" and "noFollow", retrieve the values of those fields to use in your custom meta box, and allow you to easily access them elsewhere in your site. Just use get_post_meta( $post->ID, 'meta-name', true ) to retrieve them.

OTHER TIPS

I'd like to also suggest the use of WPAlchemy Meta Box (a php class i've created) for creating your meta boxes ...

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top