4

Question

  • what hook can be used to add a custom button to the 'Publish' metabox on the 'Edit Post' admin page?

Use case

I have a hand-rolled custom post type called ash_newsletter. I want to add a button to the post.php?post={$post->ID}&action=edit admin page for this post type. The button will only appear after the post has been published. The button will only be clickable once. The button will be located in the "Publish" metabox.

My idea is that when the button is clicked, a script will check the current post status and will check that a post_meta, ash_already_ran==false. If both conditions pass, a function will be called that contacts another service. If the function returns true, 'ash_already_ran' will be updated to true.

9

This will get you started;

add_action( 'post_submitbox_misc_actions', 'custom_button' );

function custom_button(){
        $html  = '<div id="major-publishing-actions" style="overflow:hidden">';
        $html .= '<div id="publishing-action">';
        $html .= '<input type="submit" accesskey="p" tabindex="5" value="Customize Me!" class="button-primary" id="custom" name="publish">';
        $html .= '</div>';
        $html .= '</div>';
        echo $html;
}

Adds a custom button to Publish Meta Box - example;

enter image description here

So far you still need to;

  • register the custom button on your custom post type only
  • write conditional check for to only show button when post has post_status of publish
  • write custom function to call your custom service

I need to go now, but do attempt to tackle the points above and post your results. I'll return tomorrow to see where you're at and if I anyone else can help further.

  • awesome! So I've got my button appearing only on my custom post and only when the post is published and doesn't have a value for 'already_done'. I've written and tested my service_call function. The only thing I'm still not sure of is what hook service_call() should be attached to. – dnagirl Oct 5 '12 at 18:11
  • i know this post is old but could you show the full code, this is pretty exact to what i was looking to do. – rudtek Sep 9 '16 at 22:31
  • how do we add this in WP 5.0? – Haseeb Dec 8 '18 at 9:50
2

Thanks userabuser for the head start. I believe the expanded code would look closer to:

add_action( 'post_submitbox_misc_actions', 'custom_button', 10, 1 );

function custom_button( $post ) {

    // Show only for published pages.
    if ( ! $post
         || 'publish' !== $post->post_status
         || 'page' !== $post->post_type ) {
        return;
    }

    $html = '<div id="major-publishing-actions" style="overflow:hidden">';
    $html .= '<div id="publishing-action">';
    $html .= '<input type="submit" accesskey="p" tabindex="5" value="Custom Button" class="button-primary" id="submitbox-custom-button" name="publish">';

    // Post meta example.
    $user = get_user_by( 'ID', get_post_meta( $post->ID, '_edit_last', true ) );
    if ( $user ) {
        $html .= '<div>' . ucfirst( $post->post_type ) . ' ' . $post->ID . ' last edited by ' . $user->display_name . '</div>';
    }

    $html .= '</div>';
    $html .= '</div>';

    // Customize button click event.
    $html .= "<script>window.addEventListener('load', function(){ jQuery('input#submitbox-custom-button').click(function(event){  event.preventDefault(); alert('You\'re editing " . $post->post_type . " " . $post->ID . "'); return true; }); }); </script>";

    echo $html;
}

There are several ways to do the service layer so there is no need to solve that 5+ year old issue today. But this should be a good copy/paste start for anyone in the future and will work out of the box.

It's obviously better to include your JS in a separately loaded file but if you feel like hacking in a bit more readable JS you can use ob_start and ob_get_clean.

ob_start(); ?>
<script>window.addEventListener('load', function() {
    jQuery('input#submitbox-custom-button').click(function(event) {
      event.preventDefault();
      alert('You\'re editing <?php echo $post->post_type . " " . $post->ID; ?>');
      return true;
    });
  });
</script>
<?php $html .= ob_get_clean();
  • how we add in WP 5.0 version – Haseeb Dec 8 '18 at 9:49
1

Using post_submitbox_misc_actions hook is an appropriate way, but you shouldn't use DOM ids twice as suggested in previous answers. So, your HTML output should look like this:

add_action( 'post_submitbox_misc_actions', function( $post ){
    // check something using the $post object
    if ( get_post_status( $post->ID ) === 'publish' ){
        echo '<div class="misc-pub-section">Some button...</div>';
    }
});

See: https://developer.wordpress.org/reference/functions/post_submit_meta_box/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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