• Hello everyone!
    I use WP-Syntax plugin to hightlight code.
    But wordpress WYSIWYG-editor delete line attribute of
    <pre> tag needed to show lines.
    For example
    <pre lang="LANGUAGE" line="1">
    It replace with
    <pre lang="LANGUAGE">
    Sorry for my English.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi,

    You can try adding this to the top of wp-content/plugins/wp-syntax/wp-syntax.php (although ideally you might want to make this a separate plugin so the changes don’t get deleted next time wp-syntax gets updated):

    // Allow 'line', 'escaped' and 'highlight' attributes on <pre> in TinyMCE
    function tinymce_add_preAttr( $arr = array() ) {
      $extra = 'pre[lang|line|escaped|style|width|highlight]';
    
      if ( isset( $arr['extended_valid_elements'] ) ) {
        // append to existing value
        $arr['extended_valid_elements'] .= ',' . $extra;
      } else {
        // set the value
        $arr['extended_valid_elements'] = $extra;
      }
    
      return $arr;
    }
    
    add_filter('tiny_mce_before_init','tinymce_add_preAttr');

    Next time you load the WYSIWYG-editor, it should accept the necessary attributes on the “pre” tag.

    ( Credits: adapted from https://wordpress.stackexchange.com/questions/14363/switching-from-html-to-visual-editor-and-back-completely-strips-page-contents/14408#14408 )

    Nice. I was going to say maybe wp-syntax could be modified to parse the class attribute for settings, but this is much better!

    Question: does this replace the list of valid attributes, or just hint as to what is allowed beyond the norm. I only ask because I noticed you didn’t have “class” in the list.

    Hi,
    I never noticed before this but yes, according to this (and my own testing seems to confirm it is true) it does _replace_ the list of valid attributes.

    You will need to add the attributes you want allowed on “pre” in the code above (ex. for “class”: $extra = ‘pre[class|lang|line|escaped|style|width|highlight]’;) or just use “*” if you want to allow all attributes (ex: $extra = ‘pre[*]’;)

    For reference, by default “pre” gets its list of valid attributes from the “default rule set” defined here

    Edit: more specifically, the default attributes for “pre” seem to come from the “@” rule which states: “[id|class|style|title|dir<ltr?rtl|lang|xml::lang|onclick|ondblclick|”
    + “onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|”
    + “onkeydown|onkeyup]”

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WordPress WYSIWYG delete "line" attribute’ is closed to new replies.