93 Responses

  1. Thomas Clausen
    Thomas Clausen Published |

    This is brilliant Justin, thanks for sharing this.

    You write it’s for post and pages, but would it work for widgets as well?

    Reply
  2. Ryan
    Ryan Published |

    Great tutorial Justin. Thanks a bunch for all your contributions. Keep ‘em coming.

    Reply
  3. Many
    Many Published |

    I’ve really said it but surely shortcodes are lovely :D

    Reply
  4. Nice
    Nice Published |
    Reply
  5. The Frosty @WPCult
    The Frosty @WPCult Published |

    Wow I can see how this could be useful.

    Reply
  6. Dick
    Dick Published |

    This is GREAT, but will you write a followup on how to facilitate the user signup or point me to a good source for that? Thanks.

    Reply
  7. Kevin
    Kevin Published |

    I really like not having to depend on plugins. Thanks for posting this. I’m going to start using it right away.

    Reply
  8. Kat
    Kat Published |

    Hi Justin,

    Love this info – thanks so much.

    Inferring that you’re not planning on doing a tutorial on the member-joining part of this process, can you suggest a resource for that code?

    Thanks again,

    – Kat

    Reply
  9. Using WordPress as a members website « Logorrhea
  10. Using shortcodes to show members-only content — WPCult
  11. WordPress shortcode: Display content to registered users only- Pro Blog Tutorials
  12. bitkahuna
    bitkahuna Published |

    Dick – I use sidebar login and register plus plugins to allow users to register as ‘subscribers’ only. Works great.

    Justin, you da man!!!!

    Reply
  13. 10 incredibly cool WordPress shortcodes
  14. Jason
    Jason Published |

    I must be putting the code in the functions.php in the wrong place? I got it to hide content, but when a user goes to login there is an error:
    Warning: Cannot modify header information – headers already sent by (output started at /home1/capeknin/public_html/capek9cardio/wp-content/themes/CoolWater/functions.php:32) in /home1/capeknin/public_html/capek9cardio/wp-login.php on line 287

    Warning: Cannot modify header information – headers already sent by (output started at /home1/capeknin/public_html/capek9cardio/wp-content/themes/CoolWater/functions.php:32) in /home1/capeknin/public_html/capek9cardio/wp-login.php on line 299

    Reply
  15. Ditutu
    Ditutu Published |

    Great tutorial Justin , you are a great blogger . I’ve seen a lot of good posts wroted by you ,it’s impressive what you can do .

    Reply
  16. WPSavetnik - Zaštita sadržaja lozinkom
  17. Developing a user management plugin
  18. Ralf Skirr
    Ralf Skirr Published |

    Hi Justin,

    That’s an elegant solution, better than using plugins with huge overhead.

    Unfortunately I get the ‘Cannot modify header information’ error too.

    Any suggestion how to fix it?

    Thanks,
    Ralf

    Reply
  19. Ralf Skirr
    Ralf Skirr Published |

    Okay, I fixed the ‘Cannot modify header information’ error. For anybody who might have the same problem:

    Make sure there is no white space outside of the php start and end tags. While a blank line before the <?php start tag may look innocent, when processed by PHP, it will turn into an echo statement printing out a blank line. This is a common culprit.
    source:
    http://www.tech-recipes.com/rx/1489/solve-php-error-cannot-modify-header-information-headers-already-sent/

    Reply
  20. WordPress tips, hacks and tutorials » Nice2All.com » Content for Logged-In Users Only
  21. Lynne
    Lynne Published |

    The members only code snippet worked beautifully. Thanks.

    Reply
  22. Lynne
    Lynne Published |

    Interestingly if I use in a document, the members only code doesn’t work any more.

    Does anyone know if there’s a “fix” for this.

    Reply
  23. Lynne
    Lynne Published |

    Interestingly if I use “nextpage” in a document, the members only code doesn’t work any more.

    Does anyone know if there’s a “fix” for this.

    “Sorry for doubleposting, but the code for nextpage that I inserted didn’t show up.”

    Reply
  24. CG
    CG Published |

    i’d like to see this with a little button on the wysiwyg editor so that I can highlight my posts’s text that i want hidden… much like i would to bold some text etc.

    Reply
  25. John
    John Published |

    Is there any way I can get this to work with post excerpts?

    Right now, if a post is within the [member] tags, nothing shows up in the excerpts. I’d like it to say something like “This post is viewable to members only”. Love the tip on the shortcodes!!

    Reply
  26. Coafuri
    Coafuri Published |

    Hey, thanks for the code, I will just try this out.Definitely will be a great timesaver

    Reply
  27. 10 Useful WordPress Coding Techniques « Smashing Magazine
  28. Wordpress Blog Services - 10 Useful WordPress Coding Techniques
  29. 10 Useful WordPress Coding Techniques « Tech7.Net
  30. Web Design News » 10 Useful WordPress Coding Techniques
  31. 22 Latest Exceptional WordPress Hacks | EzTips | Wordpress Tips - Tutorials - Make Money Online
  32. mariusz
    mariusz Published |

    Fine, but if in content section you’d find a link to file?

    It would be accessfull for non-logged users if they knew the link to a file… how to solve that?

    Reply
  33. Enhance Your WordPress Themes With Shortcodes | Devlounge
  34. Luis Lopez Garay
    Luis Lopez Garay Published |

    That was just as usefull as I need. Some people needs to hide content to their guest visitors and this post is the solution I give to somebody who asks me that.

    Reply
  35. Sincere
    Sincere Published |

    I Love this post :P I was looking everywhere for this! But im having a problem, or maybe im doing it wrong…

    I did everything you described above, but when i view my page it shows the members tags [ ] in the brackets…and content is still visible. Imm working in wpmu/ BP does it still operate in mu nad bp? I assume it still would

    Any help would be greatly appreciated :P

    Reply
  36. 37 Cool Wordpress Hacks And Tutorials You Should Try |
  37. 37个Wordpress经典教程 | 尚7科技
    37个Wordpress经典教程 | 尚7科技 at |
  38. 37个Wordpress经典教程 | 第七日
    37个Wordpress经典教程 | 第七日 at |
  39. PHP-help » 22 Latest Exceptional WordPress Hacks
  40. Occasio
    Occasio Published |

    Wow this is going to allow me to completely redo my members section. I had been using forums as my member restricted section, this will be much better.

    Reply
  41. jane
    jane Published |

    hi

    can anyone let me know what code above i can use to hide specific contet if user logged in (loged in form) but another of user is logged in (hidden text)

    let say following senario:
    I have a chunk of text i want to hide, in its place i want to show log in form
    when user have logged in i want the form to be hidden and the chunk of text that were hidden to be shown

    any idea ?

    Reply
  42. stonecoldcnc
    stonecoldcnc Published |

    Hi Justin

    I’m using the Mystique Theme which contains your member shortcodes. Everything works ok and the code is an exact copy as above. I have only one little problem, I’m trying to run another shortcode inside yours which does not seem to work. Is that possible at all and if so, how?

    [visitor]
    [theme-my-login before_widget="" after_widget="" show_title="0" instance="tml-page"]
     [/visitor]
    
    [member]
    any other content
    [/member]
    Reply
  43. 7 Very Handy Shortcodes for WordPress | Devlounge
  44. sagive
    sagive Published |

    hey man.. thanks a lot!
    but can you help me a bit more?

    i want to show the shortcodes in a private page
    to the website owner and what ever i do
    to display the shortcode as a user manul failes

    no matter if i wrap it in or
    the actuall result of the shortcode apears
    instead of the explenation how to..

    how do you show [shortcode] without it actually
    activating the code? much like you did here

    p.s you should realy add a “notify me on new comments”
    so it would be easier to return to the page when respond published :)

    Reply
  45. Ryan
    Ryan Published |

    Justin, this code is just the best, thanks for sharing.

    I have now setup some roles, added the shortcode to my template, and now have a widget area only for members who are agents.

    Such a clean and simple solution.

    You’re an absolute legend, thanks for sharing.

    Reply
  46. Rohan
    Rohan Published |

    Nice idea, comes very handy at times.

    Do you have any idea for displaying certain widgets to logged in members only?

    Reply
  47. Enrico
    Enrico Published |

    Hi Justin, thanks for your great job.
    I found this old article that is very good for me.
    Only a question: is there a way to insert a shortcode (for example a form made by cforms or gravity) into the [member] shortcode? I tried by myself but it doesn’t work, wp prints the shortcode as is and not the form.

    Thank you very much
    Enrico

    Reply
  48. Enrico
    Enrico Published |

    Solved! I am not a programmer, but it seems to work.

    Changed:
    return $content;

    with:
    return do_shortcode($content);

    I hope could be useful for others!

    Reply
    1. khalid
      khalid Published |

      U r awesome. Do you know that ;)

      Reply
  49. Jon
    Jon Published |

    Not cool —

    Link removed by the administrator.

    Reply
  50. dave
    dave Published |

    Ian you just made me look like a coding god whilst on the phone to a client… searched for a solution to showing member content, cut and pasted your code into my functions.php, stuck the shortcode around a cform and hey presto… it worked. all in about 60 seconds. many thanks!

    Reply
    1. khalid
      khalid Published |

      Agree.. This makes me feel like I’m at the top of the coding Pyramid

      Reply
  51. Joakim Kristiansen
    Joakim Kristiansen Published |

    Thanks for the great tip Justin.

    It works great, but like stonecoldcnc above, I can’t get the Gravity Forms shortcode to work. Do you know a simple way to fix this?

    Thank,
    Joakim

    Reply
  52. 22 Latest Exceptional WordPress Hacks | Tutorials | instantShift
  53. Agnel Waghela
    Agnel Waghela Published |

    Thanks for this great tutorial.

    It has been very help full for me.
    Thanks a lot.

    Reply
  54. S.K
    S.K Published |

    Hi

    Excellent article.
    But to my horror, I find that your post’s content has been verbatim scraped and plagiarized in another blog where I landed after a search for the relevant keywords. I do not want to provide a direct link to the web page to avoid directing traffic to that site unwittingly! It is livexp dot net and the title is exactly this post’s title.
    Regards

    S.K

    Reply
  55. Cx Rana
    Cx Rana Published |

    great justin brother….i like ur every post.

    Reply
  56. 10 awesomely cool WordPress shortcodes! | Techewire
  57. 10 Awesome Shortcodes For Your WordPress Blog
  58. Code snipits to help ‘CMS’ your WordPress sites › Gold Coast Web Design › WordPress support and hosting › THRIVE
  59. azimmo
    azimmo Published |

    Perfect, but, i don’t uderstand why this text that appear for non registered users (This is members-only content.) have the pre and code tag HTML?

    Reply
  60. Rene
    Rene Published |

    Great! I have searched long after how it was possible. Have taken it in use and it works well.

    However, a small problem turned out

    On the pages I use the code I can not use other [code] of any kind. for example. [content mirror site = undefined record type = page item = 138]

    The code is seen simply as plain text on the sides, and not the expected function

    Do you have a solution to this

    Reply
  61. Ryu
    Ryu Published |

    Hi Justin,

    I was trying to make a shortcode that displays user profile information. I followed your tutorial on creating custom fields in user profile. I wanted to know how to display such custom themes inside the shortcode so they can be placed for memberonly content page.

    Your help will be much appreciated.

    Reply
  62. SM
    SM Published |

    Hi Justin, this is a great little shortcode, but for some reason, content within the [member] tags is showing up in the excerpts of my search results.

    If you check my site here: http://www.emaginetestbed.com/oicci/page/1/?s=Invest

    the posts with telephone numbers and addresses should all be hidden to non-members. On the actual page where this info is placed, the content within those tags is hidden.

    Any idea why this should happen and how I can fix it?

    Reply
  63. Jean
    Jean Published |

    For the “[member]” function, it would be great if we could set an optional custom message for visitors who are not logged in. So – there can be a default message, but anywhere you want to customize the message for that page/content, you can.

    Reply
  64. 22 Latest Exceptional WordPress Hacks | Template joomla, wordpress premium free , ebook
  65. Michael
    Michael Published |

    Thank you for this information however, I do have a question.

    Is there anyway to create a custom login page?

    Thanks in advance!

    Michael

    Reply
  66. Sahil Mepani
    Sahil Mepani Published |

    You just saved me couple of hours. Thanks a lot. Justin you ROCK

    Reply
  67. Jirka
    Jirka Published |

    Hi. I have a problem with shortcodes.
    I need put one shortcode ([contact form]) in to second shortcode ([member]).

    In oder words, i need only registered users can show contact form.
    Is it possible?

    Reply
  68. Royce
    Royce Published |

    Hi, this is great shortcode! One part I cannot seem to get to work is:

    return ‘Sorry, but you cannot access this content without…';

    It shows nothing. Any ideas?

    Reply
    1. Ørjan
      Ørjan Published |

      Royce, are you by any chance also using the Members plugin (also by Justin Tadlock)?

      I had exactly the same problem, until I realized that the mentioned plugin also uses the same shortcode [access].

      If you are using it, try renaming the shortcode name [access] in your own function to something else like [has_access], it worked for me!

      Reply
  69. David
    David Published |

    Fabulous…is it possible to add check boxes to select and toggle several separate elements of content so only those check boxes selected show only their corresponding content elements…?

    Reply
  70. Andres
    Andres Published |

    Great shortcode! For those of you who like me where wondering how to make the code work with a shortcode inside of it. Its really simple just change.

    return $content;

    to

    return do_shortcode($content);
    Reply
  71. Shortcode for Members Only Content | Footscray Maker Lab
  72. Tracey
    Tracey Published |

    Thanks for this and for the guides and articles you have written for magazines. All been so helpful over the years :-)

    Reply
  73. Пишем плагин для WordPress | Код это поэзия.
  74. Membuat Kontent Artikel Khusus Hanya Untuk Members Pada WordPress
  75. Homar
    Homar Published |

    Awesome!

    I was looking for a plugin, but found this and settled for it lol

    Works perfectly, thanks!

    Reply
  76. Nathan
    Nathan Published |

    Very useful, even four years later. Thank you for this!

    Reply
  77. Awesome Shortcodes For Your WordPress Blog | Welcome
  78. 22 Latest Exceptional WordPress Hacks | Brisk Blog
  79. loic
    loic Published |

    Very useful plugin.

    But I just notice that shortcode has a conflict with gallery shortcode. Actualli, it doest not display gallery but only the shortcode ([gallery columns="4" link="none" ids="265,268,599,598"]).

    Reply
  80. Martin
    Martin Published |

    Hi, i love your solution, but i got one problem.

    i want to run another shortcode inside [member][shortcode][/member].

    For some reason, it works on posts, but not on pages.

    On pages I see the inner shortcode as plaintext?!

    I would be glad, if you could help me out

    Thanks in advance

    Reply
  81. rahim
    rahim Published |

    Hi Guys,

    I am new to the world of wordpress shortcodes, I am trying to use this code snippet to hide some info to non-logged in users within a page/post and can not seem to get it working.

    the url where i am using the shortcode is: http://www.webtrendz.co.uk/client-demo/assist/services/

    i have added the php snippet into functions.php but for some reason its not converting the shortcode into a tag.

    i actually see the the shortcode in the browser like [member]rahim[\member]

    Please can someone put me in the right direction?

    sorry if this is a proper newby question.

    Reply
    1. David
      David Published |

      make sure you’re logged out when viewing it.

      Reply
  82. David
    David Published |

    This is great, thank you for posting this!!!!!!

    Reply
  83. max
    max Published |

    hi Justin,

    i’m using your brilliant tips in conjuction with Shortcode Exec PHP to dislay content according to user status.

    the problem i’m having is that nesting other shortcodes within doesn’t work.

    this is the function (in Shortcode Exec PHP):

    if ( is_user_logged_in() && !is_null( $content ) )

    return $content;

    return ”;

    and this is what i’d put on a page:

    [is_logged_in]

    the content here

    [/is_logged_in]

    this works just fine. but if i put:

    [is_logged_in]

    the content here

    [some_other_shortcode]

    [/is_logged_in]

    the nested shortcode doesn’t get executed.

    is there a way to sort this out? do i have to parse $content in the function looking for shortcodes, perhaps? or is there an easy way to make the code recognise shortcodes?

    Reply
  84. Salim
    Salim Published |

    Good work Justin. Thank you :)

    Reply
  85. Hamza pk
    Hamza pk Published |

    Thanks a lot sir, i have reduced server load using short-code for login users…Thanks again.

    Reply

Leave a Reply

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution.

Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.

css.php