Disable Shortcode usage for certain user roles

2019-08-01 15:06发布

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.

2条回答
三岁会撩人
2楼-- · 2019-08-01 15:26

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.

查看更多
Fickle 薄情
3楼-- · 2019-08-01 15:47

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');
查看更多
登录 后发表回答