Check if on Custom Post Type for TinyMCE buttons

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:

http://www.tutorialchip.com/wordpress/wordpress-shortcode-tinymce-button-tutorial-part-2/

Read More

How can I have that check if they are editing/posting a post in a custom post type of lets say, newpages?

Related posts

Leave a Reply

2 comments

  1. 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

  2. Thank you Bainternet. Very useful. I used your solution with one small modification.

    Instead of

    global $typenow;
    

    I took

    global $current_screen;
    $current_screen->post_type;
    

    $typenow only returns the correct type, if you are inserting a new post. When you are editing an existing post, it always returns “post”. So it’s better to use $current_screen->post_type.