I've been reviewing tutorials on creating blocks for Gutenberg but I am unclear how to handle a particular use case - conditional blocks.

I am looking at creating a custom post type for which I will register my own block type. These blocks will only be displayed if certain conditions are true. These conditions will be either boolean flags or integer comparisons (the values coming from wither custom user variables (meta) or session values).

If all conditions are true the block should be rendered but if one or more are false then (obviously) nothing is shown.

I cannot quite get my head around where I would place the logic for this. Admittedly my grasp of this new Gutenberg system is a bit shaky which is probably why I need some assistance.

For example:

<p logic="IF(is_logged_in,SHOW,HIDE)">My wonderful secret bit just for members.</p>
  • Do you mean a block which is already in the post content, or instead in the appender list, or a block which is part of the page template? If you could give the exact case it might be easier to figure out a solution. – Alvaro Nov 30 '18 at 16:08
  • Just a block which is part of the post content. For example IF (SomeCondition!=TRUE): Don't show this paragraph. – Matthew Brown aka Lord Matt Dec 7 '18 at 4:35
  • 1
    Maybe this approach can help? – birgire Jun 10 at 11:25
  • 1
    I think that will do nicely. – Matthew Brown aka Lord Matt Jun 10 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.