PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, August 26, 2022

[FIXED] How to restrict content from Users with Specific roles in Wordpress?

 August 26, 2022     php, wordpress     No comments   

Issue

i want to hide ads for logged in user (subscribers). im using a plugin called "private" that generates shortcodes. i found the code i should use but dont know how to put it in my single.php. My single post is custom made so i cant use the rich editor.

the one i want to use...

[private role=”subscriber”]Content goes here[/private]

and i want to put this code in the middle position of my short code.

<div id="M589567ScriptRootC925056"></div>
<script src="https://jsc.adskeeper.co.uk/a/n/anime-tv.fun.925056.js" async></script>

Solution

You do not have to use [private] short codes to hide divs or ads in your case from logged in users.

Just use WordPress native current_user_can() function to target specific user roles like this in PHP file

if (current_user_can('subscriber')) {

   //Hide something

} else {

   //Show Something

}

You can do something if you want to hide something from all logged in users

if (is_user_logged_in) {

   //Hide something

} else {

   //Show Something

}

This code goes into your working php file or active theme function.php



Answered By - Always Helping
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing