0

I have the following in my functions.php:

add_filter( 'wp_editor_settings', function( $settings ){
  if( get_post_type() == 'attachment'){
    $settings = [
      'wpautop'       => true,
      'textarea_name' => 'content',
      'textarea_rows' => 10,
      'media_buttons' => false,
      'tinymce' => true,
    ];
    return $settings;
 }
});

And this does add the visual tab when I go to edit media in the Wordpress admin. However, once I save my changes, and then go back to edit media and save again, it converts all the html to encoded html entities.

For example,

<h2>Another H2</h2>

becomes

&lt;h2&gt;Another H2&lt;/h2&gt;

Which prints the html on the front end. So I must be missing some essential piece that preserves html. What to I need to add to be able to use the visual tab when editing media descriptions?

This post is almost a correct answer but it doesn't account for this HTML issue.

1 Answer 1

2

So I must be missing some essential piece that preserves html.

No, your code is OK.

However, I would require the second parameter ($editor_id) and check whether its value is attachment_content which is the editor ID for the media description textarea on the "Edit Media" page. I also wouldn't add the 'textarea_name' => 'content' part.

once I save my changes, and then go back to edit media and save again, it converts all the html to encoded html entities

Yes, and it happens because WordPress applies format_to_edit() (see source on Trac) and then format_for_editor() (see source on Trac), and these functions both use htmlspecialchars(), therefore the HTML would be escaped twice.

So for example, <h2> becomes &amp;lt;h2&amp;gt; which in the Text/HTML mode of the editor becomes &lt;h2&gt; and then in the Visual mode appears as <h2>.

So how to fix the issue?

Unfortunately, as of WordPress 5.8, there is no hook for bypassing/disabling the first or second HTML escaping, but you can try the following trick which disables the second escaping by unhooking format_for_editor() from the the_editor_content filter if the current editor ID is attachment_content:

function my_fix_the_editor_content_double_escaping( $content ) {
    remove_filter( 'the_editor_content', 'format_for_editor' );

    // * We're not actually modifying the content, but only adding the above code.
    return $content;
}

// * Use this instead of the code you have in the question.
add_filter( 'wp_editor_settings', function ( $settings, $editor_id ) {
    // Check if the current page is the "Edit Media" page (at wp-admin/post.php), and if
    // so, we customize the editor settings.
    if ( is_admin() && 'attachment' === get_current_screen()->id &&
        'attachment_content' === $editor_id
    ) {
        // Change only what need to be changed.
        $settings['wpautop']       = true;
        $settings['textarea_rows'] = 10;
        $settings['media_buttons'] = false;
        $settings['tinymce']       = true;

        add_filter( 'the_editor_content', 'my_fix_the_editor_content_double_escaping', 1 );
    }

    return $settings; // Note: ALWAYS return it!
}, 10, 2 );

So I hope that helps and don't worry, WordPress will automatically re-hook format_for_editor() back onto the_editor_content, therefore no need to manually re-hook it.

1
  • 1
    Excellent, thorough answer that worked perfectly. Thank you.
    – 3x5
    Commented Jul 29, 2021 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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