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'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?

share|improve this question
    
sadly no... but –  Bainternet May 15 '12 at 13:10

3 Answers 3

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:

share|improve this answer

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>'; 
}
share|improve this answer
    
This adds options to the publish metabox - the question asks for adding options to the page attributes metabox. –  Stephen Harris May 15 '12 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 May 15 '12 at 18:32
    
Just edit the answer. If you detail the jQuery method, I'll upvote :) –  Stephen Harris May 15 '12 at 18:46
    
@StephenHarris - thanks for the heads up, Sensei. Now the answer has a There, I fixed it solution :o) –  brasofilo May 15 '12 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. –  mikkelbreum Jun 12 '12 at 13:13

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

share|improve this answer
    
This is how to add a post meta box - the OP is asking how to edit an existing core metabox. –  Stephen Harris May 15 '12 at 18:24
    
yes, you have right; not read correctly. –  bueltge May 23 '12 at 7:10

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.