最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Publishing post strips custom html element when user is not admin

programmeradmin8浏览0评论

This phenomenon occurred after updating Wordpress from 4.9.10 to 5.1.1. I'm using the plugins TinyMCE Advanced, Classic Editor and Advanced Custom Fields PRO. The wordpress installation has the roles admin and testrole.

I'm logged in as testrole and insert the custom html element <p><new-page></new-page></p> in a custom field of type WYSIWYG editor in a post and publish the post by clicking publish. <p><new-page></new-page></p> is replaced with <p></p>.

When logged in as admin, the replacement does not happen.

How can I prevent wordpress from replacing <p><new-page></new-page></p> with <p></p> when logged in as testrole?

This phenomenon occurred after updating Wordpress from 4.9.10 to 5.1.1. I'm using the plugins TinyMCE Advanced, Classic Editor and Advanced Custom Fields PRO. The wordpress installation has the roles admin and testrole.

I'm logged in as testrole and insert the custom html element <p><new-page></new-page></p> in a custom field of type WYSIWYG editor in a post and publish the post by clicking publish. <p><new-page></new-page></p> is replaced with <p></p>.

When logged in as admin, the replacement does not happen.

How can I prevent wordpress from replacing <p><new-page></new-page></p> with <p></p> when logged in as testrole?

Share Improve this question asked Apr 11, 2019 at 11:33 Osvald LauritsOsvald Laurits 1337 bronze badges 1
  • This is happening because WP checks any pasted markup and removes anything it considers potentially unsafe, unless the user is an administrator. If you're using the Classic Editor, one option would be to create a shortcode that outputs your <new-page> element. If you're using the Block Editor, you could create a custom block. – WebElaine Commented Apr 11, 2019 at 14:27
Add a comment  | 

2 Answers 2

Reset to default 0

To allow your specific <new-page> element in the Classic Editor, you can add a filter so WP recognizes it as an allowed tag:

// Step one - add to the allowed tags
add_action('init', function() {
    // Access the global allowed tags array
    global $allowedtags;
    // Add new-page as an allowed tag
    $allowedtags['new-page'] = array();
    // If you ever use attributes such as <new-page href="url" title="My Title">
    // Then you'll need to include those within the array like so:
    // $allowedtags['new-page'] = array('href' => array(), 'title' => array());
});

// Step two - add to allowed tags in TinyMCE (the editor)
add_filter('tiny_mce_before_init', function($a) {
    $a['extended_valid_elements'] = 'new-page';
    // Again if you ever need attributes, you'll specify those here
    // $a['extended_valid_elements'] = 'new-page[href|title]';
    return $a;
});

Since this code is specifically for TinyMCE you'll have to test to determine whether it works with the Block Editor, or only Classic. If it doesn't work with your editor, you also have the option of creating a shortcode or a block that outputs the tag in question.

Append the following code to wp-config.php

define('CUSTOM_TAGS', true );
$allowedposttags = array();
$allowedtags = array();
$allowedentitynames = array();

Copy the values for $allowedposttags, $allowedtags and $allowedentitynames from web/wp-includes/kses.php and assign them to the variables $allowedposttags, $allowedtags and $allowedentitynames in wp-config.php . Add 'new-page' => array() as an element to the array which is assigned to $allowedposttags.

发布评论

评论列表(0)

  1. 暂无评论