2

How can we apply filters only to posts with a specific custom post type? I would like some of my custom posts to start out in HTML mode for their editors. This is what I am working with so far...

       add_filter( 'wp_default_editor', create_function('', 'return "html";') );
1

You can determine the current post type using get_post_type() inside the callback function hooked to wp_default_editor:

add_filter( 'wp_default_editor', 'wpse242896_wp_default_editor' );
function wpse242896_wp_default_editor( $editor ) {
    $post_type = get_post_type();

    if ( 'custom_post_type' === $post_type ) {
        return 'html';
    } else if ( 'other_post_type' === $post_type ) {
        return 'tinymce';
    }   

    return $editor;
}

Also, anonymous functions are generally discouraged because they can't be unhooked very easily.

1

First check if we are on custom post type screen using get_current_screen

Then apply wp_default_editor hook for custom post type

add_action( 'current_screen', 'get_current_screen' );
function get_current_screen() {
    $current_screen = get_current_screen();
    if ( $current_screen->post_type == 'custom_post_type' ) {
        add_filter( 'wp_default_editor', 'cpt_set_default_editor' );
    }
}

function cpt_set_default_editor() {
    $editor = 'tinymce'; // visual mode
    // $editor = 'html'; // Text mode
    return $editor;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.