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?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 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:

Solution 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>'; 
}

Solution 3

the site here have many question for this, like this search, also a google search

also an working example (git rebo), that you can use and change for your requirement

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply