kostek00 on "Hide certain tags from users"

تعرفه تبلیغات در سایت

آرشیو مطالب

امکانات وب

You're pretty close, a logic adjustment is all that's needed I think. A few things to make note of first though. While it's possible to selectively load CSS files based on user role, please do remember that when you hide content using CSS, that content is still accessible to restricted users by them simply looking at the page's source HTML. I imagine this is not an issue with tags and you are more concerned with appearance than securing information.

CSS stylesheets should be enqueued using wp_enqueue_style(). Thus you can do variations of something like this:
if ( ! current_user_can('publish_posts')) wp_enqueue_style('hide_tags', plugins_url( 'hide-tags.css', __FILE__ ));

It's a little tricky, but it's even possible to have a PHP based stylesheet that dynamically outputs certain rules based on some arbitrary criteria such as user capability.

If you're managing the output, you could set things up so the CSS properly handles separators as well as certain tags. But if you're managing output, you may as well manage the tags too and not fuss with elaborate CSS schemes. Then you also avoid any possible security issues from hiding certain content. Much better to not output it instead of hiding it!

When coding something that involves user permissions of some sort, avoid thinking in terms of roles. You really should focus on specific capabilities. Roles are really a variable container of capabilities. It's possible a particular admin role might not be able to do something because another plugin has removed that capability, or a subscriber can actually do something that is not normally allowed. Of course on your own site you know if these variations exist or not. Still, it's a good idea to think in terms of capabilities instead of roles. In some cases doing so might simplify the code's logic because one capability could span multiple roles.

In your 'get_the_tags' callback you are simply using is_super_admin(), which isn't addressing what happens with regular admins. I suggest you find a related capability that users you want to see extra tags have and users who shouldn't see them don't have. 'publish_posts' perhaps? You can actually create custom capabilities and assign them to whatever role or even to individual users, giving you very fine grained control on who sees what.

When dealing with capabilities we use current_user_can() to check if the user has a particular capability or not. So if 'publish_posts' is the right capability for your situation, you should replace this:

if ( !is_super_admin() )

with this:

if ( current user_can('publish_posts'))
   return $tags; //unchanged

Now for the output. Oddly, the_tags() function has nothing to do with the 'get_the_tags' filter. Each function has it's own filter and there is no cross talk between them. Since you have an effective filter with 'get_the_tags' and filtering 'the_tags' requires a different, more fragile approach, we should work with get_the_tags(). This involves different logic than when the_tags() is used.

After we use get_the_tags(), we need to do a foreach loop to construct a simple array of just tag names because what's returned by get_the_tags() is way more data than we need. Your filter callback is similar but different. It's rebuilding the entire array of tag objects except for those two tags. You now want to build an array of just $tag->name values. You'll see why in a bit.

First we need to output the translatable label. When we use various translation functions, we should try to avoid HTML and just translate the actual text. Your label code should be more like this:
echo '<p class="taxonomy"><span class="tag-title">'. __('Tags', 'warp') . ':</span> ';

Next we output the tags separated by pipes. We have the array of tag names. One's inclination is to loop through the array and output each name, but there's a better way -- use implode() to glue the array elements together with ' | ', then echo out the returned string. Finally, output the closing </p>.

Given all those tidbits and hints I think you'll be able to piece something together.

نویسنده : استخدام کار بازدید : 157 تاريخ : جمعه 12 شهريور 1395 ساعت: 1:44
برچسب‌ها :