Add custom option to Standard Page Attributes Meta Box

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?

Related posts

Leave a Reply

4 comments

  1. 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:

  2. 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>'; 
    }
    
  3. 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 );
    });