I found a great post this week which shows how to easily create an if-else shortcode in WordPress.
[if is_user_logged_in] You are already logged in. [else] Display registration form. [/if]
A PHP callable must be used in [if] condition. These callables support parameters.
[if is_singular book] Show related books [/if]
This is incredibly useful.
Before, if I wanted to show different content based on the result of a function that returns a boolean, I would have to write a new shortcode to wrap it (or use a shortcodes plugin).
What if you need to check more than one condition?
In my case, I needed to check two conditions:
1. Is user a “Member”
2. Is user logged in
I needed support for if-elseif-else statements.
If a member was logged in I needed to show, “You are already a member”.
If a user was logged in, I needed to show a Member signup form.
Otherwise, I needed to show a different Member signup form.
I modified the code in the article above and found a solution for doing if-elseif-else conditions.
I changed the code to return a string (error message) instead of an Exception. A thrown Exception will not allow you to access the WordPress editor, if a non-callable is used in the shortcode. We need access to the editor to resolve this.
Install the plugin
Clone this repo to your /wp-content/plugins/ folder.
git clone https://github.com/n8kowald/if-elseif-else-shortcode
This allows you to use the following shortcode in your WordPress content:
[if is_admin] Hi God, you're up late [elseif is_user_logged_in] Hi Zero Cool, welcome back [else] Welcome to the website. Please register to continue. [/if]
We can use multiple elseif statements for an unlimited amount of conditions.
[if is_admin] Hi Mr The Plague. [elseif is_user_logged_in] Hi Zero Cool, hack the planet! [elseif is_single] Pool on the roof must have a leak. [else] Welcome an0n. Please look around but don't steal my fries. [/if]
You can use if on its own
[if is_admin] Hi Mr The Plague. [/if]
You can use if-else without the elseif
[if is_admin] Hi Mr The Plague. [else] Welcome an0n. Please look around but don't steal my fries. [/if]
Allowed callables
The following callables are enabled by default
comments_open is_404 is_admin is_archive is_author is_category is_day is_feed is_front_page is_home is_month is_page is_search is_single is_singular is_sticky is_super_admin is_tag is_tax is_time is_user_logged_in is_year pings_open
Adding callables
To allow other callables you can use the if_elseif_else_shortcode_allowed_callables filter.
<?php add_filter( 'if_elseif_else_shortcode_allowed_callables', function( $whitelist ) { $whitelist[] = 'your_callable_here'; return $whitelist; });
Supported callable types
- Function names: is_user_logged_in
- Static class method calls (>=PHP 5.2.3): MyClass::myCallbackMethod
[if is_user_logged_in] Hello user [elseif User::is_member_logged_in] Hello member [else] Hello, please log in [/if]
Note: in the above example, you would need to add User::is_member_logged_in to the allowed callables list.
Passing parameters to callables
Parameters are passed as space separated strings
[if is_singular books] Related books here. [/if]
If your callable accepts multiple parameters
<?php function is_garfield( $animal, $colour ) { return $animal === 'cat' && $colour === 'orange'; }
[if is_garfield cat orange] Yes, this is garfield. [/if]
Testing
If you want to simplify the main function or fix a bug, the plugin includes WordPress tests for your refactored code.
Install PHPUnit
composer global require phpunit/phpunit
Install the WordPress testing library and database
Run this from the plugin directory.
Replace mysql_username and mysql_password with your MySQL username and password:
./bin/install-wp-tests.sh wordpress_tests mysql_username mysql_password
You can now run tests with
phpunit