Search code examples
phpwordpress

Wordpress 6.6 add button next to publish on custom post type


I am wanting to add a custom button next to the Publish button in the wordpress edit post menu. I have tried several other solutions involving different actions and hooks but nothing has worked so far.

function custom_meta_box($post)
{
    add_meta_box('newBox', 'sample name', 'custom_markup', 'post', 'side', 'high');
    wp_die("<h1> this is being run</h1>");
}

add_action('post_submitbox_start', 'custom_meta_box');

Here are some solutions I have tried but haven't worked: (add a button to execute a custom function in wordpress admin edit page area), (https://wordpress.stackexchange.com/questions/45720/is-it-possible-to-add-an-item-the-post-publish-panel). I've also tried a few approaches with the "admin_menu", "post_submit_metabox" and "add_metaboxes". I'm working in a theme and my code is running out of functions.php.I've tried with and without different params. I've run this code with wp_die() and it doesn't crash, but I'm not sure if I'm doing something horribly wrong or this action isn't running even though it should be. This code works with other actions, but doesn't give the location desired.


Solution

  • The add_meta_box function is for adding meta boxes, not for adding buttons to the publish box.

    Try below:

    function add_custom_publish_button() {
        global $post;
        
        ?>
        <div id="custom-publish-action">
            <a href="#" id="custom-publish-button">Custom Action</a>
        </div>
        <script type="text/javascript">
        jQuery(document).ready(function($) {
            $('#custom-publish-action').insertAfter('.misc-pub-section:last');
            
            $('#custom-publish-button').on('click', function(e) {
                e.preventDefault();
                // Add your custom action here
                alert('Custom button clicked');
            });
        });
        </script>
        <?php
    }
    add_action('post_submitbox_misc_actions', 'add_custom_publish_button');