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.

I'm at a loss with this. Do you see anything wrong with the code below specific to the noindex, nofollow checkboxes? The meta box gets drawn to the screen fine, but the values do not stick.

The code for the custom page title and custom excerpt works fine.

// ===================
// = 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('post_header', 'Custom Post Header Code (optional)', 'custom_post_images', 'post', 'normal', 'low');
        add_meta_box('post_title', 'Custom Post Title', 'custom_post_title', 'post', 'normal', 'high');
        add_meta_box('post_title_page', 'Custom Post Title', 'custom_post_title_page', 'page', 'normal', 'high');
        add_meta_box('postexcerpt', __('Excerpt'), 'post_excerpt_meta_box', 'page', 'normal', 'core');
        add_meta_box('categorydiv', __('Page Index Options'), 'post_categories_meta_box_modified', 'page', 'side', 'high');
    }
}

//Adds the custom images box
function custom_post_images() {
    global $post;
    ?>
    <div class="inside">
        <textarea style="height:70px; width:100%;margin-left:-5px;" name="cb2_customHeader" id="cb2_customHeader"><?php echo get_post_meta($post->ID, 'cb2_customHeader', true); ?></textarea>
        <p>Enter your custom html code here for the post page header/image area. Whatever you enter here will override the default post header or image listing <b>for this post only</b>. You can enter image references like so &lt;img src='wp-content/uploads/product1.jpg' /&gt;. To show default images, just leave this field empty</p>
    </div>
<?php
}

//Adds the custom post title box to posts
function custom_post_title() {
    global $post;
    ?>
    <div class="inside">
        <p><input style="height:25px;width:100%;margin-left:-10px;" type="text" name="cb2_customTitle" id="cb2_customTitle" value="<?php echo get_post_meta($post->ID, 'cb2_customTitle', true); ?>"></p>
        <p>Enter your custom Post Title here and it will be used for the html &lt;title&gt; for this post page and the Google link text used for this page.</p>
    </div>
<?php
}

//Adds the custom post title box to pages
function custom_post_title_page() {
    global $post;
    ?>
    <div class="inside">
        <p><input style="height:25px;width:100%;margin-left:-10px;" type="text" name="cb2_customTitle" id="cb2_customTitle" value="<?php echo get_post_meta($post->ID, 'cb2_customTitle', true); ?>"></p>
        <p>Enter your custom Page Title here and it will be used for the html &lt;title&gt; for this page and the Google link text used for this page.</p>
    </div>
<?php
}

//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
}

add_action('save_post', 'custom_add_save');

function custom_add_save($postID){
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $postID;
    }
    else
    {
        // called after a post or page is saved
        if($parent_id = wp_is_post_revision($postID))
        {
        $postID = $parent_id;
        }

        if ($_POST['cb2_customHeader']) 
        {
            update_custom_meta($postID, $_POST['cb2_customHeader'], 'cb2_customHeader');
        }
        else
        {
            update_custom_meta($postID, '', 'cb2_customHeader');
        }
        if ($_POST['cb2_customTitle']) 
        {
            update_custom_meta($postID, $_POST['cb2_customTitle'], 'cb2_customTitle');
        }
        else
        {
            update_custom_meta($postID, '', 'cb2_customTitle');
        }
    }
}

function update_custom_meta($postID, $newvalue, $field_name) {
    update_post_meta($postID, $field_name, $newvalue);
}
share|improve this question
    
Can I assume correctly that you did not provide all the code, specifically the code that creates the metabox? That might help. Also, since the real value of StackExchange is to create a repository of answered questions for others to learn from in the future please do go to the extra effort to create titles that actually mean something to those who might experience the same issue as you did. –  MikeSchinkel Aug 26 '10 at 22:53
    
Mike, thanks for your help with the title. –  Scott B Aug 27 '10 at 11:45
    
No problem, trying to make the titles match the necessary solution so that people googling in the future are more likely to find them. –  MikeSchinkel Aug 27 '10 at 18:10

2 Answers 2

up vote 0 down vote accepted

I'm not sure but I think the simple answer is you have not yet added "noindex" and "nofollow" as categories to your site. Go to the category page in the admin and add them and then I think your code will work, it did for me:

http://example.com/wp-admin/edit-tags.php?taxonomy=category

Of course if you need to add those categories programmatically, ideally for use in a plugin activation so it only ever has to run when the plugin is first activated you can use the following code:

<?php 
/** Include the WordPress Taxonomy Administration API */
require_once(ABSPATH . 'wp-admin/includes/taxonomy.php');
$categories = get_categories('hide_empty=0&fields=names');
if (!in_array('noindex',$categories))
  wp_insert_category(array('cat_name'=>'noindex'));
if (!in_array('nofollow',$categories))
  wp_insert_category(array('cat_name'=>'nofollow'));

UPDATE:

@Scott B: Is it possible that you never associated the category taxonomy with the page post type? You know that Pages (post_type='page') don't get categories by default, right? Somewhere in your code you need to run register_taxonomy_for_object_type(); my guess is you haven't?

If I'm right then adding this to your code will almost certainly solve your problem:

add_action('init','attach_category_to_page');
function attach_category_to_page() {
    register_taxonomy_for_object_type('category','page');
}
share|improve this answer
    
Thanks Mike, the categories are there, however, they are children of the uncategorized cat. Should that matter? –  Scott B Aug 27 '10 at 12:11
    
So you have "Uncategorized>nofollow" and "Uncategorized>noindex"? Yeah, it's all working for me. Could you have something else that's getting in the way? Another plugin or other custom code? (BTW, just curious, have you removed the Category Metabox and if so how did you accomplish that? There may be more than one way so I'm just curious.) –  MikeSchinkel Aug 27 '10 at 18:09
    
@Mike> This is on the "PAGE" editor, so there's no category meta box there. That's really why I'm adding a meta box for this purpose. This works fine when I ad noindex or nofollow to a post, using the default categories widget. –  Scott B Aug 31 '10 at 18:21
    
@Scott Ah.. (Doh! :) You know you can add the category metabox to Pages if you need them. Would that work better? If yes, ask that specific question and I or someone else will show you how... –  MikeSchinkel Aug 31 '10 at 18:25
    
@Scott Also I take it they are still not saving? –  MikeSchinkel Aug 31 '10 at 18:27

Hmm, maybe this is totally far away, but you are not saving the checkbox values in your saving hook, aren't you?

btw: update will create if not exists. so no need for the if in that function. in the end no need for the helper function. less complexity will probably solving your issue easier.

share|improve this answer
    
> Great suggestions. Thanks! –  Scott B Aug 31 '10 at 18:18

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.