3

I want to add custom styles to Wordpress using dynamic arrays.

My question is how can I add the two arrays to the style_formats array within the 'my_mce_before_init_insert_formats' function dynamically?

I've tried using array_merge(), and a few other dynamic array techniques but it just resets the TinyMCE editor to its default settings or breaks it completely.

function my_mce_before_init_insert_formats( $init_array ) {  

  $style_formats = array(  

  array(  
      'title' => '.translation',  
      'block' => 'blockquote',  
      'classes' => 'translation',
      'wrapper' => true,

  ),  

  //add arrays here

  );  

  $init_array['style_formats'] = json_encode( $style_formats );  

  return $init_array;  

} 


//arrays to add
array(  
    'title' => '⇠.rtl',  
    'block' => 'blockquote',  
    'classes' => 'rtl',
    'wrapper' => true,
),

array(  
    'title' => '.ltr⇢',  
    'block' => 'blockquote',  
    'classes' => 'ltr',
    'wrapper' => true,
),
  • You might have to check what's in style_formats first and add to it - have you echo'ed it out to see what's there before you set it? Alternatively - and this is probably more likely to be the case - you might need to actually set everything you want to have there, including the TinyMCE defaults. – Tim Malone May 22 '16 at 9:53
  • Btw further reading here: codex.wordpress.org/TinyMCE_Custom_Styles – Tim Malone May 22 '16 at 9:53

Your Answer

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

Browse other questions tagged or ask your own question.