4

I'd like to add a simple checkbox option to the exsisting standard Page Attributes Meta Box.

Is there an obvious/intended way to do that using the WordPress API, rather than registering a new metabox for my one new option?

1
  • sadly no... but
    – Bainternet
    Commented May 15, 2012 at 13:10

3 Answers 3

4

Unfortunately not. The only way is to deregister the metabox, and then re-register it, supplying your own callback function which mimics the original metabox, but with your alterations (making sure the names of the inputs do not change).

This method is outlined in these posts:

Alternatively you can insert the options with javascript as outlined in:

1

Stephen Harris gently pointed that I misread the question. Unfortunately, there's no action post_submitbox_publish_actions where we can hook.

So, to make my solution work, the workaround is to make jQuery move the checkbox from its original placement. I've added the script to make this happen.

This is a complete wild attempt to do it, based in
- this Q&A: How to Move the Author Metabox into the "Publish" metabox?
- and the codex first example: http://codex.wordpress.org/Function_Reference/add_meta_box

I'm running the code inside a plugin (wp-content/mu-plugins/tests.php) and haven't tested with functions.php...

I cannot guarantee it is a "correct" code, but it works in my local WordPress.

add_action( 'post_submitbox_misc_actions', 'wpse_52193_custombox_in_publish' );
add_action( 'save_post', 'wpse_52193_save_postdata' );
add_action( 'admin_head', 'wpse_52193_script_enqueuer');

function wpse_52193_custombox_in_publish() {
    global $post;
    if ('page' != get_post_type($post)) return;

    wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' );
    $checked = (get_post_meta($post->ID, 'myplugin_new_field',true)) ? 'checked="yes"' : '';

    echo '<div id="myplugin_new_field_div" class="misc-pub-section" style="border-top-style:solid; border-top-width:1px; border-top-color:#EEEEEE; border-bottom-width:0px;"><div style="font-weight: bold;">Description for this field:</div>';
    echo '<input name="myplugin_new_field" id="myplugin_new_field" type="checkbox" '.$checked.'>';
    echo '</div>';
}

function wpse_52193_save_postdata( $post_id ) {
    if ( 'page' != $_POST['post_type'] )
          return;

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return;

    if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) )
        return;

    $mydata = $_POST['myplugin_new_field'];
    update_post_meta($post_id, 'myplugin_new_field', $mydata);
}

function wpse_52193_script_enqueuer(){
    global $current_screen;
    if('page' != $current_screen->id) return;

    echo '<script type="text/javascript">
        jQuery(document).ready( function($) { 
            $("#myplugin_new_field_div").appendTo("#pageparentdiv"); 
         });
        </script>'; 
}
5
  • This adds options to the publish metabox - the question asks for adding options to the page attributes metabox. Commented May 15, 2012 at 18:19
  • 1
    Oh, my... I can't down vote myself... Will wait for some of them and delete the answer. Thanks Harris, will try not to go wild before re-checking that Publish is not equal to Attributes.
    – brasofilo
    Commented May 15, 2012 at 18:32
  • Just edit the answer. If you detail the jQuery method, I'll upvote :) Commented May 15, 2012 at 18:46
  • @StephenHarris - thanks for the heads up, Sensei. Now the answer has a There, I fixed it solution :o)
    – brasofilo
    Commented May 15, 2012 at 19:19
  • @StephenHarris thanks for the effort, Not sure if I should accept this as a 'correct' answer, as it is too much of a 'hack' approach in my opinion, even if it works. I think the answer is, that there is no obvious/intended way (using the proper WP API). Instead there is of course the option of manipulating the markup using Javascript after page load. Thanks for the effort with this reply, and if enough users tell me to accept it/upvote it, I will do that. Commented Jun 12, 2012 at 13:13
1

I do it like this:

add_action( 'page_attributes_misc_attributes', function() {
    global $post; ?>
    <p class='post-attributes-label-wrapper'>
        <label class='post-attributes-label' for='my_checkbox'>
            <input name='my_checkbox' type='checkbox' id='my_checkbox' <? checked( get_post_meta( $post->ID, 'my_checkbox', 1 ) ); ?> />My checkbox
        </label>
    </p> <?
});
add_action( 'save_post', function() {
    $val = isset( $_POST['my_checkbox'] ) ? 1 : 0;
    update_post_meta( $_POST['post_ID'], 'my_checkbox', $val );
});

Your Answer

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

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