• Resolved trickz100

    (@trickz100)


    Hello,

    I’ve been searching the interwebs for solutions on how to display a shortcode only when a specific role is logged in, but I can’t seem to find a solution.

    I want the shortcode to display for Subscribers only, and not for anyone else, including those not logged in.

    [shortcode]Content Here[/shortcode]

    I simply want to hide [shortcode][/shortcode], keeping ‘Content Here‘.

    Any help would be greatly appreciated!

Viewing 15 replies - 1 through 15 (of 15 total)
  • bernbe01

    (@bernbe01)

    hey

    cool customization you’re applying! I like that thought process on the content perms

    so the only way i can think of doing this is to use a plugin that allows you to have multiple role-based-content-areas per page then i’d make one area with the shortcode you want, and another area for the roles of the users that didn’t match.

    more like an if / else

    i’m not super familiar with this plugin, but it would basically have shortcodes to identify the content areas, and then your shortcodes within. something like:

    [O_U user_role="Administrator"][shortcode]Content Here[/shortcode][/O_U]
    
    [O_U user_role="Author,Editor"]Content Here[/O_U]
    
    [O_U logged_status="out"]Guest Content Here[/O_U]

    What do think? Worth trying?

    bernbe01

    (@bernbe01)

    i forgot the link to the plugin i was talking about ??

    Thread Starter trickz100

    (@trickz100)

    Hello,

    Thank you for your time to help out, but sadly it isn’t working on my website.

    I’ll try to be more specific if I can;

    I am using two plugins in each post that use shortcodes.

    1) Download Manager
    2) Advertising

    The outcome: [pre_ad adid=”2957″][wpdm_package id=’23947′][/pre_ad]

    Now, [wpdm_package id=’23947′] is “the content” and I need to hide [pre_ad adid=”2957″][/pre_ad] for everyone, except Subscribers.

    I have tried a few plugins that involve wrapping these with yet another shortcode, but sadly it doesn’t seem to be working ??

    bernbe01

    (@bernbe01)

    ok, no problem

    is this something you’d be open to doing in the theme’s template file?

    what is your theme? do you have a child theme currently?

    and are the nest shortcodes that you are currently using functioning as expected?

    thanks!

    Thread Starter trickz100

    (@trickz100)

    Hello,

    Yes I’d be happy to modify theme files, I am using a different theme than the default WordPress themes.

    are the nest shortcodes that you are currently using functioning as expected?

    – I assume so, not sure.

    bernbe01

    (@bernbe01)

    on the nested shortcode question, i am curious if when you put this:

    [pre_ad adid="2957"][wpdm_package id='23947'][/pre_ad]

    into the content if it renders or not. please let me know

    it might be ok that it isn’t a default theme. which one is it?

    Thread Starter trickz100

    (@trickz100)

    if it renders or not

    – I am unsure how to check that :/

    bernbe01

    (@bernbe01)

    thats no problem

    when you were getting the sample code to show me that snippet, what area of wordpress did you extract it from? may I please have a link to the page as well?

    Thread Starter trickz100

    (@trickz100)

    It’s using two plugins, “WordPress Download Manager” and “WP Pre Advertisement”

    The shortcodes were taken after generating download packages and advert packages.

    Is there not a way of using Strip Shortcodes?

    I am putting the shortcodes into every post. (I havn’t added the advert code yet as I want to show it only to certain users, once the issue is solved, i’ll then add them to the over 2800 posts there are on the website)

    My website is https://www.astateoftrance.co.uk

    bernbe01

    (@bernbe01)

    strip shortcodes is a good idea, but i think it actually would remove both layers of shortcodes leaving us with no content either

    one more plugin method to try if you are up to it?

    nested shortcodes plugin will let us try these codes out (be sure to edit the roles to your liking) so please install that and then try the following out on one of the posts (or a new, non-browseable post):

    [O_U user_role="Author,Editor"][wpdm_package id='23947'][/O_U]
    
    [O_U logged_status="out"][pre_ad adid="2957"][wpdm_package id='23947'][/pre_ad][/O_U]

    if that doesn’t work:

    do you already have a child theme or have you been customizing the raw theme files? child theme will allow you to safely update the theme from envato

    please create a new page/post template based on the template in use by the posts you want to affect currently (you maye have to connect via ftp/panel/ssh to see the contents of that file)

    after that create a pastebin of this template and update this thread with the link

    i may need some more info then, depends what we see ??

    bernbe01

    (@bernbe01)

    i have to jet for a while. if i can i’ll get back on it this eve. i’ll check back tomorrow at the latest though, ok?

    Thread Starter trickz100

    (@trickz100)

    Thanks for the info! I’ll look into it ??

    Thread Starter trickz100

    (@trickz100)

    Hello again,

    Ok after a few hours playing with code and searching around online, I managed to come up with a working code to fit my needs!

    // REMOVE ADVERT SHORTCODE FOR EVERYONE EXCEPT SUBSCRIBERS //
    
    if( current_user_can('administrator') || current_user_can('premium_member') || !is_user_logged_in() ) {
    function remove_advert_first_part($content) {
    	return str_replace('[pre_ad adid="2957"]', '', $content);
    	}
    add_filter( 'the_content', 'remove_advert_first_part', 6);
    }
    
    if( current_user_can('administrator') || current_user_can('premium_member') || !is_user_logged_in() ) {
    function remove_advert_second_part($content) {
        return str_replace('[/pre_ad]', '', $content);
    }
    add_filter( 'the_content', 'remove_advert_second_part', 6);
    }

    The above code will remove [pre_ad adid=”2957″][/pre_ad] for Administrators, Premium Members and those who are not logged in. It also keeps the content in between.

    I’m not sure if you can place two parts of a shortcode within one function or not, but since I couldn’t figure that part out, I made two functions, one to removed the first part of the shortcode, and one to remove the second and it works just as well.

    Thanks very much for your time and help still, it really means a lot and I hope this code can be of some use to others!

    -Trickz100

    bernbe01

    (@bernbe01)

    very nice work dude!!!!

    I’m stoked to see your solution! thank you for posting it back, hopefully it’ll help someone else out someday too

    feel free to mark this thread as resolved if you have no more questions as well

    take care!

    Thread Starter trickz100

    (@trickz100)

    Thanks dude, all the best ??

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Display Shortcode for Specific Roles’ is closed to new replies.