Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
}
share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
Anytime someone prefaces their comment with "First of all...", my shields engage... But thanks for your full response and I'll check it out. –  Scott B Sep 15 '10 at 15:42
    
@EAMann - This is definitely a more elegant solution, but one small problem....The checkboxes are coming up checked regardless of the previous state on publish (even on newly created pages). Any ideas what's causing that? I'm testing it on 2.9.1 presently. –  Scott B Sep 15 '10 at 15:54
    
Not sure, but I'll double check the logic later today and get right back to you. Also, sorry for the "First of all..." start ... :-) –  EAMann Sep 15 '10 at 15:59
    
No problem, I really appreciate the quick help :) Been using categories as a hack and if I can get this to work, I'll move all my custom post settings to this method. –  Scott B Sep 15 '10 at 16:05
    
@EAMann - I did notice a type on chk_noIndex and ck_noIndex, but correcting it had no effect. –  Scott B Sep 15 '10 at 16:15
show 5 more comments

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

share|improve this answer
1  
-1 For pitching your own plugin without disclosing that it's your plugin. –  EAMann Oct 1 '10 at 17:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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