Home > Blockchain >  How to run function in specific post_type on WordPress?
How to run function in specific post_type on WordPress?

Time:04-09

I created a WordPress Plugin and want to add a shortcode next to the title, so that it appears only in the specific post_type

my code:

function haizdesign_add_post_admin_book_column($haizdesign_columns){    
    $haizdesign_columns['haizdesign_book'] = __('book shortcode');
    return $haizdesign_columns;    
}

function haizdesign_show_post_book_column($haizdesign_columns, $haizdesign_id){
echo '<input type="text" width="100%" onclick="jQuery(this).select();" value="[embed_book id=&quot;'.get_the_ID().'&quot;]" />';        
}      
add_filter('manage_posts_columns', 'haizdesign_add_post_admin_book_column', 2);
add_action('manage_posts_custom_column', 'haizdesign_show_post_book_column', 5, 2);

This function appears in all types of posts, and I want it to appear only in a "Books" type

CodePudding user response:

The hooks you're using (manage_posts_columns and manage_posts_custom_column) will trigger your callbacks for all post types.

There are dynamic hooks you can use instead which include the name of the post type you want your code to apply to.

If your post type was 'book' for instance, the hooks would be:

add_filter('manage_book_posts_columns', 'haizdesign_add_post_admin_book_column', 2);
add_action('manage_book_posts_custom_column', 'haizdesign_show_post_book_column', 5, 2);

Documentation:

  • Related