Search code examples
phpwordpresstinymceeditor

WordPress: Add custom style in <p>-tag in TinyMCE Editor


I want to add a custom style to <p> tags in the TinyMCE editor.

I tried the following code. But it doesn't work with <p> tags. It works if I'm using <span> tags instead?!

Are there any limitations or is there something wrong with my code?

function my_mce_before_init_insert_formats( $init_array ) {  

    $style_formats = array(  
        array(  
            'title' => 'Lead',  
            'block' => 'p', 
            // 'block' => 'span',  <-- works
            'classes' => 'lead',
            'wrapper' => true,
        ),
    );  
    $init_array['style_formats'] = wp_json_encode( $style_formats );  
    
    return $init_array;  

} 

Solution

  • I guess I found the solution. The wrapper has to be false for paragraphs:

    function my_mce_before_init_insert_formats( $init_array ) {  
    
        $style_formats = array(  
            array(  
                'title' => 'Lead',  
                'block' => 'p', 
                'classes' => 'lead',
                'wrapper' => false,
            ),
        );  
        $init_array['style_formats'] = wp_json_encode( $style_formats );  
        
        return $init_array;  
    
    }