{"id":27623,"date":"2010-01-15T11:00:35","date_gmt":"2010-01-15T16:00:35","guid":{"rendered":"http:\/\/wpmu.org\/?p=27623"},"modified":"2013-06-12T15:23:37","modified_gmt":"2013-06-12T19:23:37","slug":"how-to-show-or-hide-content-in-wordpress-wpmu-based-on-user-authentication-roles-and-capabilities","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/how-to-show-or-hide-content-in-wordpress-wpmu-based-on-user-authentication-roles-and-capabilities\/","title":{"rendered":"How to Show or Hide Content in WordPress \/ WPMU Based on User Authentication, Roles, and Capabilities"},"content":{"rendered":"<p>Here is a handy little code snippet that falls into the basic category for WordPress but can prove to be extremely useful when customizing your installation.<\/p>\n<p>Whether you&#8217;re working with WordPress, WPMU or BuddyPress, these little tips will give you greater flexibility in building your community.<\/p>\n<pre>&lt;?php if (is_user_logged_in() ) { \/\/only logged in user can see this ?&gt;\r\nI love green tea!\r\n&lt;?php } ?&gt;<\/pre>\n<p>You can also selectively show content using role-based permissions, as detailed in the WordPress codex section on <a href=\"http:\/\/codex.wordpress.org\/Roles_and_Capabilities\" target=\"_blank\">roles and capabilities<\/a>. For example, if you want to show a specific menu to your admins and editors only but show a different menu for everyone else, you can do something like this:<\/p>\n<pre>&lt;?php if ( current_user_can( 'delete_others_posts' ) ) { \/\/only admins and editors can see this ?&gt;\r\n&lt;ul&gt;\r\n \t&lt;li&gt;Secret Menu Item&lt;\/li&gt;\r\n&lt;\/ul&gt;\r\n&lt;ul&gt;\r\n \t&lt;li&gt;Other Restricted Link&lt;\/li&gt;\r\n&lt;\/ul&gt;\r\n&lt;?php } else { ?&gt;\r\n&lt;ul&gt;\r\n \t&lt;li&gt;General Menu&lt;\/li&gt;\r\n&lt;\/ul&gt;\r\n&lt;ul&gt;\r\n \t&lt;li&gt;Anyone Can See This&lt;\/li&gt;\r\n&lt;\/ul&gt;\r\n&lt;?php } ?&gt;<\/pre>\n<p>These are a starting point so that you can write your own. There are some plugins that accomplish these things. However, sometimes you cannot find a plugin that is specific enough to do what you need and you only want it applied in one area. That&#8217;s when these quick snippets will come in handy.<\/p>\n<h3>Other possible uses might include:<\/h3>\n<ul>\n<li>Hide signup information from logged-in users and re-appropriate that space on the page targeted to your subscribers.<\/li>\n<li>Sell and display ads targeted to your casual visitors or to your registered users.<\/li>\n<li>Show or hide widgetized areas in your sidebar based on if the user is logged in or not.<\/li>\n<li>Display alternate footers, sidebars, headers, page elements, etc. instead of having to create a custom template for each instance.<\/li>\n<li>Reserve certain content for registered users only, ie &#8220;Members only downloads&#8221; or restricted galleries.<\/li>\n<li>Attach it to your &#8220;Read More&#8221; links to get users hooked before requiring registration.<\/li>\n<li>Create a separate menu bar for your clients for payment forms for registered users, etc.<\/li>\n<li>If you&#8217;re using BuddyPress, you could tweak it to selectively target profile areas to be private unless the user is logged in.<\/li>\n<\/ul>\n<p>If you use your imagination, I&#8217;m sure that you can find unlimited ways to show and hide content. In this age of social networking, users&#8217; attention spans are decreasing and they will often click away from sites that instantly offer too much cluttered information.<\/p>\n<p>One of the most important reasons to show and hide information based on a user&#8217;s role is to conserve the real estate on the page and strategically target the information on your site to registered or non-registered users.<\/p>\n<p>These simple snippets are a good foundation for how to use BuddyPress conditional tags, for which we will provide an introduction next week. Stay tuned!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is a handy little code snippet that falls into the basic category for WordPress but can prove to be extremely useful when customizing your installation. Whether you&#8217;re working with WordPress, WPMU or BuddyPress, these little tips will give you greater flexibility in building your community. &lt;?php if (is_user_logged_in() ) { \/\/only logged in user [&hellip;]<\/p>\n","protected":false},"author":4099,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[1,235],"tags":[],"tutorials_categories":[],"class_list":["post-27623","post","type-post","status-publish","format-standard","hentry","category-news-community","category-misc"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/27623","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/4099"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=27623"}],"version-history":[{"count":6,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/27623\/revisions"}],"predecessor-version":[{"id":183939,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/27623\/revisions\/183939"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=27623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=27623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=27623"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=27623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}