Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a button to a custom post type in Wordpress?

Tags:

wordpress

I have a "Products" custom post type. Normally, this custom post type have an "Add New" button. I want to add another button call "Update from Provider".

Currently, I have modify the Wordpress code (in "wordpress\wp-admin\includes\class-wp-list-table.php") to add that button. In this case, when I update Wordpress, my modified code will be deleted. Therefore, I need to move that button to my plug-in code.

In this case, please help me how to move that button to my plug-in code.

enter image description here

like image 610
Leap Bun Avatar asked Dec 11 '12 04:12

Leap Bun


People also ask

How do I add a button to a WordPress post?

To add the Buttons block, click on the + Block Inserter icon and search for “buttons”. Click it to add the block to the post or page. Once you add your first button, you can type what you'd like the button to say.

How do I link a button to a form in WordPress?

You can start by editing any page or post and go to your WordPress editor. Next, click the (+) plus sign at the top and add a 'Buttons' block. After that, enter a text for your button and then click the link icon.

How do I add a button without plugin in WordPress?

Adding a button without using a plugin can be done in a few different ways. One way is to use the WordPress customizer. To do this, go to the Customizer and click on the Buttons tab. Here, you can add a new button by clicking on the Add new button button.


1 Answers

Well, if you opened the core file you saw that there's no action in it where we can hook.

Only a couple of filters. We can use the following:

add_filter( 'views_edit-movies', 'so_13813805_add_button_to_views' );
function so_13813805_add_button_to_views( $views )
{
    $views['my-button'] = '<button id="update-from-provider" type="button"  title="Update from Provider" style="margin:5px">Update from Provider</button>';
    return $views;
}

It produces this:

custom button in cpt

To put it in an approximate position from where you'd like, use the following:

add_action( 'admin_head-edit.php', 'so_13813805_move_custom_button' );

function so_13813805_move_custom_button(  )
{
    global $current_screen;
    // Not our post type, exit earlier
    if( 'movies' != $current_screen->post_type )
        return;
    ?>
    <script type="text/javascript">
        jQuery(document).ready( function($) 
        {
            $('#update-from-provider').prependTo('span.displaying-num');    
        });     
    </script>
    <?php 
}

Which results in this:
jquery dom manipulation

like image 117
brasofilo Avatar answered Oct 16 '22 05:10

brasofilo