Disable Shortcode usage for certain user roles

你说的曾经没有我的故事 提交于 2019-12-11 05:15:26

问题


I have a guest posting plugin on my Wordpress site and want to disable usage of shortcodes for certain users roles (subscribers for example). I need this for security reasons mostly.


回答1:


Assume that you have shortcode,

function myshortcode(){

$user = wp_get_current_user();
if ( !in_array( 'author', (array) $user->roles ) ) {
    //Run shortcode
}

}

add_shortcode('myshortcode','myshortcode');



回答2:


You can use the strip_shortcodes() function, you can use it as a filter to strip shortcodes from your desired content:

function example_remove_shortcode( $content ) {
  $content = strip_shortcodes( $content );
  return $content;
}
add_filter( 'the_content', 'example_remove_shortcode' );

or

echo strip_shortcodes( $my_customly_created_content );

in where you want to show the content.



来源:https://stackoverflow.com/questions/40979291/disable-shortcode-usage-for-certain-user-roles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!