X

Creación de estilos personalizados desplegables en TinyMCE

Snippets by IsItWP

¿Quieres crear tu propio menú desplegable de estilos personalizados? Si bien es probable que haya un plugin para esto, hemos creado un fragmento de código rápido que puede utilizar para crear estilos personalizados desplegables en el editor visual TinyMCE.

Instrucciones:

Todo lo que tienes que hacer es añadir este código al archivo functions.php de tu tema o en un plugin específico del sitio:

function themeit_mce_buttons_2( $buttons ) {
  array_unshift( $buttons, 'styleselect' );
  return $buttons;
}
add_filter( 'mce_buttons_2', 'themeit_mce_buttons_2' );


function themeit_tiny_mce_before_init( $settings ) {
  $settings['theme_advanced_blockformats'] = 'p,a,div,span,h1,h2,h3,h4,h5,h6,tr,';

  $style_formats = array(
      array( 'title' => 'Button',         'inline' => 'span',  'classes' => 'button' ),
      array( 'title' => 'Green Button',   'inline' => 'span',  'classes' => 'button button-green' ),
      array( 'title' => 'Rounded Button', 'inline' => 'span',  'classes' => 'button button-rounded' ),
      
      array( 'title' => 'Other Options' ),
      array( 'title' => '½ Col.',      'block'    => 'div',  'classes' => 'one-half' ),
      array( 'title' => '½ Col. Last', 'block'    => 'div',  'classes' => 'one-half last' ),
      array( 'title' => 'Callout Box',        'block'    => 'div',  'classes' => 'callout-box' ),
      array( 'title' => 'Highlight',          'inline'   => 'span', 'classes' => 'highlight' )
  );

  $settings['style_formats'] = json_encode( $style_formats );
  return $settings;
}
add_filter( 'tiny_mce_before_init', 'themeit_tiny_mce_before_init' );

Nota: Si es la primera vez que añade fragmentos de código en WordPress, consulte nuestra guía sobre cómo copiar / pegar correctamente fragmentos de código en WordPress, para no romper accidentalmente su sitio.

Si te ha gustado este fragmento de código, por favor, considere revisar nuestros otros artículos en el sitio como: 46 marcas más grandes del mundo que utilizan WordPress activamente y cómo crear un formulario de registro de usuario personalizado en WordPress.

Comentarios   Deja una respuesta

  1. Does not seem to work anymore. I just added this, nothing happened in my MCE. Perhaps a conflict with my theme/page builder? (Madrigal/WPBakery)

  2. Sorry to dig up an ancient post, but is there any way to do this and create 2 separate ‘Format’ drop downs? I’d like one containing P options and one containing colour options if possible.

  3. Kevin Donnigan abril 20, 2014 en 1:33 pm

    Thank you SO much for this. I was using another code snippet for the same effect, but after upgrading to WP with the new TinyMCE plugin, that code no longer worked. I tried this, and it sitll works.

    Appreciate it.

    1. Perfect glad to hear this TinyMCE snippet was able to help you out!

  4. Kevin Donnigan abril 20, 2014 en 1:33 pm

    Thank you SO much for this. I was using another code snippet for the same effect, but after upgrading to WP with the new TinyMCE plugin, that code no longer worked. I tried this, and it sitll works.

    Appreciate it.

  5. Kevin Donnigan abril 20, 2014 en 1:33 pm

    Thank you SO much for this. I was using another code snippet for the same effect, but after upgrading to WP with the new TinyMCE plugin, that code no longer worked. I tried this, and it sitll works.

    Appreciate it.

  6. Kevin Donnigan abril 20, 2014 en 1:33 pm

    Thank you SO much for this. I was using another code snippet for the same effect, but after upgrading to WP with the new TinyMCE plugin, that code no longer worked. I tried this, and it sitll works.

    Appreciate it.

  7. Kevin Donnigan abril 20, 2014 en 1:33 pm

    Thank you SO much for this. I was using another code snippet for the same effect, but after upgrading to WP with the new TinyMCE plugin, that code no longer worked. I tried this, and it sitll works.

    Appreciate it.

  8. Kevin Donnigan abril 20, 2014 en 1:33 pm

    Thank you SO much for this. I was using another code snippet for the same effect, but after upgrading to WP with the new TinyMCE plugin, that code no longer worked. I tried this, and it sitll works.

    Appreciate it.

  9. Great post Kevin, thanks. Is there anyway we can remove the styles in the editor, much the same way the user can click on the remove formatting button?

  10. Thanks Kevin,
    This is exactly the solution i was looking for.

    1.  Perfect glad to see you like the snippet 🙂

  11. brilliant! THANK YOU!!! this is exactly the solution i was looking for… even with the right styles all ready to go!

    1. @twitter-17003417:disqus  Cool glad that you find this wordpress snippet useful. The right styles wow it’s like I can read your mind 😉 cheers enjoy…

  12. Thanks Kevin,

    I have found some solution as working with line-breaks with the More Tag (split content).
    Its also very easy to add.

    Dont wanna drop urls here but this helped me a lot.
    http://digwp.com/2010/03/wordpress-post-content-multiple-columns/

  13. Kevin, sorry but  i’ve got one last question.

    The code is all working correct there is only a little problem with WordPress wrapping each Paragraph into a single  Left or Right column.

    This is my function, nothing fancy.

    function themeit_mce_buttons_2( $buttons ) {  array_unshift( $buttons, 'styleselect' );  return $buttons;}add_filter( 'mce_buttons_2', 'themeit_mce_buttons_2' );function themeit_tiny_mce_before_init( $settings ) {  $settings['theme_advanced_blockformats'] = 'p,a,div,span,h1,h2,h3,h4,h5,h6,tr,';  $style_formats = array(      array( 'title' => 'Add Columns' ),      array( 'title' => '← Column',      'block'    => 'div',  'classes' => 'content-col-left' ),      array( 'title' => 'Column →', 'block'    => 'div',  'classes' => 'content-col-right' )  );  $settings['style_formats'] = json_encode( $style_formats );  return $settings;}add_filter( 'tiny_mce_before_init', 'themeit_tiny_mce_before_init' );

    And when i start typing a story,
    Lets say it’s a story with 3 paragraphs, and when i’m done  typing i select all content (3 paragraphs) together  and hit the style > Left column button from the TinyCME editor but it wraps each paragraph into a single …

    This is very frustrated to see and for some reason i can’t get it to work.
    I want my 3 paragraphs to be wrapped into P-tags and all 3 P-tags together need to be wrapped into one div with a class left-column.

    1. Hi B.J. Yes I can see what you mean, that would get frustrating however at the point that things are inserted that would be TinyMCE and not part of my snippet so I don’t think anything can be done. The snippet just adds news settings, 

      However have you tried to add a line break after it merges the three paragraphs into one? does that fix things. Ill fool around with things and see if I can do something about it for you.

  14. Thank you so much Kevin,

    1. No problem glad that I could help.

  15. Could you make columns with those drop down styles?

    Like in the drop down menu:
    Add column 1 (this will add this code)

    Cursor should be here for typing content in P

    After that pick from the drop down menu Add column 2

    For adding a second column of text next to the first one.
    I think it’s very difficult to split text into 2 columns at the content entry div if you don’t know how html works.

    I would like to know this for a client of mine who can only work with a WYSIWYG editor but would like to have an option to have two columns at the entry div.

    Sorry for my Poor English 🙁

    1. Hi B.J. yes you could add a div with the class class=”rgt-column” and another with class=”lft-column” then its just up to you when setting up the CSS to make this work as columns.  If you want to have the CSS to create the columns used within the editor you can add this little code and create a new style sheet with the columns code.

      function wps_add_editor_style() {
        add_editor_style( ‘editor-style-columns.css’ );
      }
      add_action( ‘after_setup_theme’, ‘wps_add_editor_style’ );This way they can see the content in multiple columns, you may need to fool around with this a bit to get it working the way you would like.

Añadir un comentario

Nos alegra que haya decidido dejar un comentario. Tenga en cuenta que todos los comentarios se moderan de acuerdo con nuestra política de privacidad , y que todos los enlaces son nofollow. NO utilice palabras clave en el campo del nombre. Tengamos una conversación personal y significativa.

WordPress Launch Checklist

La lista definitiva para lanzar WordPress

Hemos recopilado todos los elementos esenciales de la lista de comprobación para el lanzamiento de su próximo sitio web de WordPress en un práctico ebook.
Sí, envíeme el ¡gratuito!