0

I've written a hook in my functions file that adds a metabox, from an installed plugin, to a custom post type that I've created. The metabox appears fine, but the default selection that is checked in the metabox is "index,follow" whereas I would like it to be "noindex,nofollow" by default. Is there anyway to change the plugins function using my hook?

My hook:

    function robotsmeta_add_custom_box() {
        add_meta_box('robotsmeta','Robots Meta',array('RobotsMeta_Admin','noindex_option_fill'),'secured-area','side');
    }
    add_action('add_meta_boxes', 'robotsmeta_add_custom_box');

Plugin's noindex_option_fill function:

    function noindex_option_fill() {
        global $post;
        $robotsmeta = $post->robotsmeta;
        if (!isset($robotsmeta) || $robotsmeta == "") {
            $robotsmeta = "index,follow";
        }
        ?>
        <label for="meta_robots_index_follow" class="selectit"><input id="meta_robots_index_follow" name="robotsmeta" type="radio" value="index,follow" <?php if ($robotsmeta == "index,follow") echo 'checked="checked"'?>/> index, follow</label><br/>
        <label for="meta_robots_index_nofollow" class="selectit"><input id="meta_robots_index_nofollow" name="robotsmeta" type="radio" value="index,nofollow" <?php if ($robotsmeta == "index,nofollow") echo 'checked="checked"'?>/> index, nofollow</label><br/>
        <label for="meta_robots_noindex_follow" class="selectit"><input id="meta_robots_noindex_follow" name="robotsmeta" type="radio" value="noindex,follow" <?php if ($robotsmeta == "noindex,follow") echo 'checked="checked"'?>/> noindex, follow</label><br/>
        <label for="meta_robots_noindex_nofollow" class="selectit"><input id="meta_robots_noindex_nofollow" name="robotsmeta" type="radio" value="noindex,nofollow" <?php if ($robotsmeta == "noindex,nofollow") echo 'checked="checked"'?>/> noindex, nofollow</label><br/>
        <?php
    }

1 Answer 1

0

Maybe try the following:

function set_robotsmeta_default( $post_object ) {
    if (!isset($post_object->robotsmeta) || $post_object->robotsmeta == "") {
        $post_object->robotsmeta = "noindex,nofollow";
    }
    return $post_object;
}
add_action( 'the_post', 'set_robotsmeta_default' );

EDIT: Since the above didn't work, the code below may work, by editing the global $post object before the plugin add_meta_boxes is called. I just don't know if this will affect posts that already have that value set.

function set_robotsmeta_default() {
    global $post;
    if (!isset($post->robotsmeta) || $post->robotsmeta == "") {
        $post->robotsmeta = "noindex,nofollow";
    }  
}
add_action( 'add_meta_boxes', 'set_robotsmeta_default', 1);
2
  • I tried adding that to my functions file with no luck. Should I try it someplace else? Commented Mar 13, 2015 at 3:17
  • You got it. The updated code is good to go. I'm not worried about existing posts, what you wrote is perfect. Thank you very much! Commented Mar 16, 2015 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.