我有一个WordPress多站点设置,我试图在一些HTML标签上保存带有数据属性的页面内容。超级用户可以毫无问题地保存,但是当管理员或较低角色保存时,它会删除标签中的数据属性。有没有办法允许其他用户角色在html中保存数据属性?
为了清楚起见,它不是被剥离的html标签本身,而是数据属性,如下所示:
<p data-item="1">String</p>
以上内容保存为:
<p>String</p>
这也不是一个小问题,我在WYSIWYG和源视图之间来回切换它并保持不变,只有当我保存页面时才会被删除,并且仅适用于低于超级用户的用户多站点。
任何帮助表示赞赏,谢谢!
您遇到的问题是unfiltered_html
功能。如果您阅读该codex链接,您会注意到以下内容:
注意:在WordPress Multisite中,只有超级管理员具有
unfiltered_html
功能。
要解决这个问题,你需要将unfiltered_html
功能添加到administrator
角色中。如果您不知道如何继续阅读:
你需要使用add_cap()
函数。像下面这样的东西就足够了。如果整个网络使用一个主题,则可以将以下代码粘贴到活动主题的functions.php
文件中。
否则,您可能想要使用Must-Use Plugin - 基本上创建一个像custom-functions.php
这样的文件,将以下代码粘贴到其中,并将其放入/wp-content/mu-plugins/
(如果它尚不存在则创建它)。这将使它成为一个“必须使用的插件”,无论如何都会被加载,并且无法激活/停用。
function so_51604149_add_cap(){
$role = get_role( 'administrator' );
if( $role ){
$role->add_cap( 'unfiltered_html' );
}
}
add_action( 'init', 'so_51604149_add_cap' );
或者,有一个可以提供帮助的“用户权限”和“用户角色”类型插件。您的要点就是Super Admins是WordPress MultiSite上唯一具有unfiltered_html
功能的角色。
Xhynk带我到这个解决方案,谢谢!
不推荐使用unfiltered_html功能,因此为了模拟该功能的功能,我在代码中添加了以下内容:
add_action( 'init', 'kses_unfiltered_html' );
function kses_unfiltered_html() {
$user = wp_get_current_user();
if ( current_user_can('edit_pages') )
kses_remove_filters();
}
这就是我所需要的,它允许任何可以编辑页面的用户保存未经过滤的内容。其他人可能需要在edit_pages的位置发挥作用,例如:
add_action( 'init', 'kses_unfiltered_html' );
function kses_unfiltered_html() {
$user = wp_get_current_user();
if ( current_user_can('administrator') )
kses_remove_filters();
}
这个过滤器对我有用:
// Add the unfiltered_html capability back in to WordPress 3.0 multisite. o(8MNTW9B2WUi(ITf8N&0rc$
function allow_unfiltered_html_multisite( $caps, $cap, $user_id, $args ) {
if ( $user_id !== 0 && $cap === 'unfiltered_html' ) {
$user_meta = get_userdata($user_id);
if ( in_array( 'administrator', $user_meta->roles, true ) ) {
// Re-add the cap
unset( $caps );
$caps[] = $cap;
}
}
return $caps;
}
add_filter('map_meta_cap', 'allow_unfiltered_html_multisite', 10, 4 );```