← All posts

Conditional Gutenberg Block with ACF: Show Content by PHP Condition

Hiding a section from logged-out visitors, or showing it only under some other rule, usually means installing another conditional-content plugin. In this video we build a Conditions wrapper block with an ACF block in Scripts Organizer. You type a PHP condition such as is_user_logged_in() into the block, and anything you put inside it only shows when that condition is true. Visitors who don't meet the condition see a fallback message instead. It takes one small template and two fields, and you don't need React or a build step.

Create the ACF block in Scripts Organizer

You want one block that can wrap any other blocks. Then the condition logic lives in one place, and you don't have to repeat it on every page.

  1. Go to Scripts Organizer → ACF Blocks and add a new block.
  2. In Edit ACF Block Title, name it (we used "Conditions").
  3. Set a Dashicon Name. We used randomize. The List of dashicons link opens the full icon list.
  4. Set Category to Widgets.
  5. Turn on Advanced Mode (Supports), then turn on JSX (for inner blocks). This switch lets the block wrap other blocks with <InnerBlocks />.
  6. Publish the block.

Add the fields with the Fields shortcut

The block needs two inputs: the condition to check and the message to show when the check fails. You can create them without leaving the block editor.

After the first publish, click Fields in the top bar. It opens ACF Field Groups in a panel next to your code. You could also go through Custom Fields in the WordPress admin, but the shortcut is faster once you're used to it.

  1. Click Add New and name the field group after the block.
  2. Add a Text field with the field name condition. This is where you'll type the PHP condition.
  3. Add a second field with the field name message. You could use Text, but we set the Field Type to Text Area so the message has more room.
  4. In the location rules, pick Block and select your Conditions block.
  5. Click Save Changes, then Update the block.

Write the PHP template

The template does all the work. If the condition field is empty, it shows the inner blocks as usual. If the field has a condition, the template evaluates it. When the condition is true, the inner blocks render. When it's false, the message field renders instead. Here's the code from the PHP tab (it's also shared as a gist in the video description):

<?php if ( !empty(get_field('condition')) ): // Check if condition is applied ?>

    <?php // Display if condition is valid
        $condition = get_field('condition');
        if(eval("return $condition;")): ?>

        <InnerBlocks />

    <?php else: // If condition is not valid display message ?>

        <h2><?php the_field('message'); ?></h2>

    <?php endif; // end of if field_name logic ?>

<?php else: // If condition is not set display content ?>

    <InnerBlocks />

<?php endif; // end of if field_name logic ?>

The block uses eval(), so whatever anyone types into the Condition field runs as PHP. Only give access to this block to people you would trust to edit your theme code.

Use the block on a page and test it

Now check that logged-out visitors really see something different from logged-in users.

  1. Create a page (we called it "Test condition") and add the Conditions block.
  2. Put your protected content inside it. We added a paragraph: "This is an important content".
  3. With the block selected, set Condition to is_user_logged_in().
  4. Set Message to something like "You need to be logged in to see the content".
  5. Publish, then view the page.

When you're logged in, you see the content. In an incognito window, the content is replaced by the message.

Extend the fallback message

A plain message tells visitors what's missing. A link gives them somewhere to go next. The message part is ordinary markup, so you can change it in the template. We changed the heading to h1 and added a link below it:

<h1><?php the_field('message'); ?></h1>
<a href="#">Create account</a>

Replace # with your login or registration URL.

Swap in any condition

The block evaluates whatever PHP expression you type, so logged-in checks are only one use. You can check for a mobile device or any other built-in WordPress conditional. You can also write your own function and call it from the Condition field. The same block handles all of these, so you only need one plugin instead of several.

Who is this for

  • WordPress developers who build with Gutenberg and want members-only or conditional sections without installing another plugin.
  • Scripts Organizer users with the ACF Gutenberg Addon who want a practical example of a block that wraps other blocks with InnerBlocks.
  • Site builders who want to control visibility per section of a page, using conditions they already know from WordPress.

FAQ

How do I show Gutenberg content only to logged-in users? Wrap the content in the Conditions block and set its Condition field to is_user_logged_in(). Logged-in users see the inner blocks. Everyone else sees the text from the Message field.

What happens if I leave the Condition field empty? The template checks for an empty condition first. If there isn't one, the inner blocks render for everyone, like a normal group.

Can I use conditions other than logged-in status? Yes. The field accepts any PHP expression that returns true or false. That could be a mobile check, another WordPress conditional, or a custom function you wrote.

Why does the block need JSX (for inner blocks) turned on? The block wraps other blocks through <InnerBlocks />. Turning on JSX (for inner blocks) under Advanced Mode (Supports) is what lets you place blocks inside it in the editor.

Is there a faster way to add ACF fields to a block? Yes. Click the Fields shortcut in the Scripts Organizer block editor. It opens ACF Field Groups right next to your code, so you don't have to go through Custom Fields in the admin.

Get Scripts Organizer → dplugins.com