Strip shortcodes for all user roles except administrator - wordpress

I'm looking for a solution that will strip shortcodes for all wordpress user roles except administrator and which ever role i select. A solution similar to the one on the below link, It was never confirmed to work and i don't know which one works.
Disable Shortcode usage for certain user roles
Thanks,

You should combine both answers:
function remove_shortcode_for_user_roles( $content ) {
$user = wp_get_current_user();
if ( in_array( 'administrator', (array) $user->roles ) == false &&
in_array( 'other_role', (array) $user->roles ) == false ) {
$content = strip_shortcodes( $content );
}
return $content;
}
add_filter( 'the_content', 'remove_shortcode_for_user_roles' );
"other_role" would be that other role you also want to exclude from the shortcode stripping.

Related

How to Show Different Menus to Different User Roles in Woocommerce

I want to hide Downloads from Woocommerce menu when user type is Artist.
Add this code to your theme's 'functions.php'.
function custom_my_account_menu_items( $items ) {
if( is_user_logged_in() ) {
$user = wp_get_current_user();
$role = ( array ) $user->roles;
if($role[0]=="artist") // change role name if different
unset($items['downloads']);
}
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items' );

Wordpress - Make it so that non-admin users can't see posts with a specific custom post status

What hooks would I use for the functions file in order to make it so that all non-admin users can't see all posts with a specific custom post_status in the wp-admin back-end. BUT it is still able to be queried and looped through the WordPress post loop?
With pre_get_posts you should be able to get started (to hide posts from the admin screen). You may also want to check the post type, etc.
function filter_posts( $wp_query ) {
if ( is_admin() ) {
$user = wp_get_current_user();
$post_status = 'draft';
if ( ! in_array( 'administrator', $user->roles ) ) {
$wp_query->set( 'post_status', $post_status );
}
}
}
add_action( 'pre_get_posts', 'filter_posts', 10 );
To disallow users to edit posts with that specific status, you should do:
function restrict_post_editing(){
global $post;
$post_status = 'draft';
if ( get_post_status( $post ) == $post_status ) {
$user = wp_get_current_user();
if ( ! in_array( 'administrator', $user->roles ) ) {
do_action('admin_page_access_denied');
wp_die( __('You cannot modify or delete this entry.') );
exit;
}
}
}
add_action('edit_post', 'restrict_post_editing', 10, 1);
add_action('wp_trash_post', 'restrict_post_editing', 10, 1);
add_action('before_delete_post', 'restrict_post_editing', 10, 1);

Disable visual editor based on role?

I want to disable visual editor form 'author' role form my wordpress site.
Author only see text editor when write a post.
You can use the user_can_richedit filter to tell WP to disable rich editor for author only through this code (put this inside your functions.php file):
$user = wp_get_current_user();
if ( in_array( 'author', (array) $user->roles ) ) {
add_filter( 'user_can_richedit' , '__return_false', 50 );
}
I make following function it's work.
function cp_disable_editor()
{
if( current_user_can( 'author' ) )
{
$user_ID = get_current_user_id();
update_user_meta( $user_ID, 'rich_editing', false );
}
}
add_action('admin_init','cp_disable_editor');

Conditional Tag - woocommerce customer - logged in - wordpress

I'm looking for the conditional tag (or code / wordpress) to check if a visitor is logged in to the 'basic' Customer role of Woocommerce.
if ( is_user_logged_in() only checks if the visitor is logged in... so this needs te be more specific but I don't know how...
thanks
As current_user_can( 'customer' ) is unreliable, you could roll your own function, as suggested here. In functions.php:
function so19916370_get_user_role( $user_id = 0 )
{
$user = ( $user_id ) ? get_userdata( $user_id ) : wp_get_current_user();
return current( $user->roles );
}
Usage, checking the ID of the current user:
if( 'customer' == so19916370_get_user_role( get_current_user_id() ) )
{
echo 'you are a customer';
}

Display role of administrator next to the username in WordPress

Is there a way to display an extra text next to the Username if the person has the administrator role?
I just want to display the extra text for this role only, what hook could I use for that?
Yes, you can use the filter the_author, like so:
add_filter( 'the_author', 'author_admin_so_14367149' );
function author_admin_so_14367149( $display_name )
{
// Block the filter in the dashboard (admin area)
if( is_admin() )
return $display_name;
global $authordata;
// User has administrator role, add string to display name
if( in_array( 'administrator', $authordata->roles ) )
$display_name = $display_name . ' (admin)';
return $display_name;
}
For the comments section, there's another filter and the checks are different:
add_filter( 'get_comment_author', 'comment_author_admin_so_14367149' );
function comment_author_admin_so_14367149( $author )
{
// Block the filter in the dashboard (admin area)
if( is_admin() )
return $author;
$user = get_user_by( 'login', $author );
// User has administrator role, add string to display name
if( in_array( 'administrator', $user->roles ) )
$author = $author . ' (admin)';
return $author;
}

Resources