I’m adding TinyMCE buttons to my plugin and they’re working, but I only want these buttons to show up for a certain custom post type. I followed this tutorial if it helps:
WordPress Shortcode TinyMCE Button Tutorial Part 2
How can I have that check if they are editing/posting a post in a custom post type of lets say, newpages
?
If you followed that tutorial you linked then look at the function that registers the buttons:
function mylink_button() {
if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages'){
return;
}
if ( get_user_option('rich_editing') == 'true' ) {
add_filter( 'mce_external_plugins', 'add_plugin' );
add_filter( 'mce_buttons', 'register_button' );
}
}
and you change it a bit to check for the post type:
function mylink_button() {
if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages')){
return;
}
if ( get_user_option('rich_editing') == 'true' ) {
global $typenow;
if (empty($typenow) && !empty($_GET['post'])) {
$post = get_post($_GET['post']);
$typenow = $post->post_type;
}
if ("newpages" == $typenow){
add_filter( 'mce_external_plugins', 'add_plugin' );
add_filter( 'mce_buttons', 'register_button' );
}
}
}
this way you only register the buttons on your “newpages” type