Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress, add custom button on post type list page

Tags:

php

wordpress

I am trying to add custom button on top of post type page like this image enter image description here

Is there any filter or action I can use to add custom button there?

Thanks

like image 523
user98239820 Avatar asked Apr 23 '15 00:04

user98239820


People also ask

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.

How do I add a classic button in WordPress?

First, you need to create a new post or edit an existing one where you want to add a button. On your post edit screen, click on the '+' icon to add a New Block and select the Button block under the Layout Elements section. Simply click on the 'Add text…' area and enter your button text.


3 Answers

If you are using the class WP_Lists_table (and you should) then this is the right way to do it:

add_action('manage_posts_extra_tablenav', 'add_extra_button');
function add_extra_button($where)
{
    global $post_type_object;
    if ($post_type_object->name === 'shop_order') {
        // Do something
    }
}
like image 68
Macr1408 Avatar answered Sep 19 '22 08:09

Macr1408


Digging into WordPress core code i did not find any hook or any filter for that buttonm you can also see that code from line no 281 to line number 288 . But you can add your button here according to this filter.

add_filter('views_edit-post','my_filter');
add_filter('views_edit-page','my_filter');

function my_filter($views){
    $views['import'] = '<a href="#" class="primary">Import</a>';
    return $views;
}

Hope it helps you.

like image 31
Touqeer Shafi Avatar answered Sep 17 '22 08:09

Touqeer Shafi


Unfortunately there is no hook called after showing the "Add New" button. The closest possible place to add anything without using javascript is below the title and "Add new" like this:

enter image description here

In my example I added a button to my custom post type "Event" using the hook "edit_form_top":

add_action('edit_form_top', 'add_custom_button');

function add_custom_button($id){
    if ($post->post_type != 'event') return false;
    echo('<button>Custom button</button>');
}
like image 24
Geza Gog Avatar answered Sep 17 '22 08:09

Geza Gog