Skip to content
GPLWP Guides

How to display content conditionally in WordPress

Content Time About 20 minutes 6 steps Updated 25 Oct 2026

CONTENT
The short answer

For simple rules, use WordPress conditional tags or current_user_can() in a child theme or custom shortcode. For editor-managed content based on roles, devices, locations, dates or several conditions together, use a visibility plugin because WordPress does not provide those controls in the editor by default.

The route
  1. 01Define the display rule
  2. 02Use a built-in condition
  3. 03Keep the code in the right place
  4. 04Use a shortcode for editor content
  5. 05Check your page builder settings
  6. 06Use WP Brain for complex rules

What you need

  • Administrator or developer access to the WordPress site
  • A child theme or safe code-snippet method for custom PHP
  • A test account for each user state you need to check

Define the display rule

Write down exactly who should see the content and where. Examples include visitors who are not logged in, users with a particular capability, people viewing one page, or everyone during a date range.

Keep security-sensitive content separate from ordinary personalisation. Hiding a callout is not the same as protecting a download, account page or paid material.

Use a built-in condition

For simple template changes, add the content inside a PHP condition in the relevant template or template part. WordPress provides conditions such as is_page(), is_single(), is_front_page() and is_user_logged_in(). For permissions, use current_user_can() rather than relying only on a displayed role name.

For example, a template can check if ( is_user_logged_in() ) before outputting a member message, or check if ( is_page( 'contact' ) ) before showing a page-specific block. Combine conditions with PHP operators such as && and ||.

Keep the code in the right place

Put template-specific logic in the template or block pattern that outputs the content. If you put a conditional tag directly in functions.php, it can run before WordPress has determined the current page, so the condition may fail. WordPress recommends placing the check in a template or running it later through an action or filter.

Use a child theme or a code-snippet tool rather than editing a parent theme. Test the front end while logged out, logged in as a normal user and logged in as an administrator.

Use a shortcode for editor content

If editors need to place conditional text inside posts or pages, create or install a shortcode that wraps the content. A shortcode can return the enclosed content only when its condition is true; otherwise it should return an empty string or an alternative message.

This route is suitable for a small number of stable rules, but shortcode attributes are easy to mistype and the shortcode must be processed in that editor or page builder. Support threads commonly trace missing output to an incorrect role slug, caching, or a builder that is not processing the shortcode.

Check your page builder settings

Many page builders include display conditions for individual sections or widgets. Look for settings named Display conditions, Visibility, Responsive conditions, Login status or Role. These are convenient for visual content, but the exact controls depend on the builder and may not cover location, date ranges or nested rules. Elementor, for example, documents conditions based on login status and user role.

Do not assume that hiding an element in the browser is access control. For private information, restrict the page, file or server-side response as well as hiding the visual element.

Use WP Brain for complex rules

For content managed by non-technical editors, WP Brain - WordPress Logic Controller is the fast route. It provides no-code rules for conditions such as user role, device, location and date, and supports multiple operators and nested condition groups.

Create a rule for the block or content area, choose the conditions, select whether all or any conditions must match, then preview it with representative accounts and devices. This is a better fit than hand-written template checks when the same site needs several combinations of visibility rules.

The fast route

Let WP Brain do it

Advanced no-code rules for showing content by role, device, location, date, and more; ideal for complex visibility logic.

Get WP Brain

Sources

  1. developer.wordpress.org /themes/classic-themes/basics/conditional-tags/?utm_source=o…
  2. wordpress.org /support/topic/role-based-shortcodes-not-working/?utm_source…
  3. elementor.com /help/show-and-hide-elements-on-a-page-display-conditions/?u…

Questions

Can WordPress display content only to logged-in users?
Yes, WordPress can do this with the built-in <code>is_user_logged_in()</code> function. Put the check around the output in a template, or use a shortcode or page-builder visibility setting for editor content. Remember that this hides the content from the page output but does not automatically protect files, APIs or other URLs that reveal the same information.
How do I show content to one WordPress user role?
Use a capability check with <code>current_user_can()</code> in PHP, or use a role-based visibility feature supplied by a membership or visibility tool. Capabilities are generally safer than hard-coding a role name because roles can be renamed or configured differently. If a shortcode shows nothing, check the exact role or capability identifier, not just the label shown in the dashboard.
Why does my conditional tag always return false?
The condition may be running before WordPress has identified the current query, especially if it is placed directly in <code>functions.php</code>. Put it in the template that renders the content, or run it later through an appropriate action or filter. Also check that you are using the right condition: <code>is_front_page()</code> and <code>is_home()</code> are not interchangeable.
Can I display content by country, device or date without coding?
Not with WordPress core alone. A page builder may handle basic device or login rules, but country, date windows and combinations such as role plus device usually require a visibility plugin or custom development. A tool such as WP Brain is the faster option when editors need to create and change those rules without editing PHP.
Is conditionally hidden content private?
No. Conditional display is not automatically access control. It can keep a promotion or navigation link out of the page for certain visitors, but sensitive text may still be exposed through an API response, cached page, attachment URL or another template. Protect private content at the page, membership, file or server level as well as controlling what is rendered.