0

I have a site with two type of user role, administrator and author.

I have a dive class name "site-shop-wrapper" which I want to show only to posts added by the administrator and hide it for all other posts added by authors.

So basically I need a function to check the post author role and if it was not an administrator, then remove the div by class name.

I am new to WP and don't know much about it, any help with bee appreciated.

2
  • Can you please share your existing query/loop? Commented Nov 15, 2016 at 14:43
  • Hi @megmorsie please refer to my answer.
    – Yama
    Commented Nov 16, 2016 at 9:56

3 Answers 3

0

Basically you are asking a function to check the post author role. You can use user_can() function.

if(user_can('administrator'))
{
     //Write your code if the admin
}

current_user_can() : https://codex.wordpress.org/Function_Reference/current_user_can

user_can() : https://codex.wordpress.org/Function_Reference/user_can

1
  • Thanks for reply, This is not what I am looking for. I want a function to check the post author role and remove div with class name site-shop-wrappe if post author role is not an administrator ?
    – Yama
    Commented Nov 13, 2016 at 19:52
0

Finally, I decided to use if author_can function. However, I believe there is a better way of doing this. add_action('wp_footer', 'remove_buybutton_from_non_admin'); function remove_buybutton_from_non_admin(){ if (author_can($post->ID, 'activate_plugins')) { ?> <script> var appBanners = document.getElementsByClassName('shop-wrapper'), i; for (i = 0; i < appBanners.length; i += 1) { appBanners[i].style.visibility="show"; } </script> <?php } else { ?> <script> var appBanners = document.getElementsByClassName('shop-wrapper'), i; for (i = 0; i < appBanners.length; i += 1) { appBanners[i].style.visibility="hidden"; } </script> <?php } }

0

This is not that difficult to achieve. First, get an array of all admins:

$admins = get_users (array ( 'role' => 'admin'));

This returns an array of user objects. You will need an array of admin ids, so you'll have to extract those:

$admin_ids = array();
foreach ($admins as $admin) {$admin_ids[] = $admin->ID;}

Next, see if the current user is in the $admins list and act accordingly

$author_id = get_the_author_meta( 'ID' );
if (in_array ($author_id, $admin_ids))
  ...do admin stuff...
else
  ...do non admin stuff...;

Note: I didn't test this code, some debugging may be necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.