{"id":2271,"date":"2019-06-29T12:23:45","date_gmt":"2019-06-29T11:23:45","guid":{"rendered":"https:\/\/www.nathankowald.com\/blog\/?p=2271"},"modified":"2022-03-08T23:52:44","modified_gmt":"2022-03-08T23:52:44","slug":"wordpress-shortcode-if-elseif-else-statements","status":"publish","type":"post","link":"https:\/\/www.nathankowald.com\/blog\/2019\/06\/wordpress-shortcode-if-elseif-else-statements\/","title":{"rendered":"WordPress shortcode: if-elseif-else statements"},"content":{"rendered":"\r\n<p class=\"wp-block-paragraph\">I found a <a href=\"https:\/\/level7systems.co.uk\/wordpress-shortcodes-else-statement\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"great blog post (opens in a new tab)\">great post<\/a> which shows how to easily use an if-else shortcode in WordPress.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">[if is_user_logged_in]\r\n    You are already logged in.\r\n[else]\r\n    Display registration form.\r\n[\/if]<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">A <a href=\"https:\/\/www.php.net\/manual\/en\/language.types.callable.php\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"PHP callable (opens in a new tab)\">callable<\/a> must be used in the [if] condition.<\/p>\r\n<p>In the above example, <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/is_user_logged_in\/\" target=\"_blank\" rel=\"nofollow noopener\">is_user_logged_in<\/a> is the callable. This is a built in WordPress function to check whether the current visitor is a logged in user. It&#8217;s a shortcode equivalent of this PHP code:<\/p>\r\n<pre class=\"wp-block-preformatted\">&lt;?php<br \/>if ( is_user_logged_in() ) {\r\n    echo \"You are already logged in.\";\r\n} else {\r\n    echo \"Display registration form.\";\r\n}\r\n<\/pre>\r\n<p>The callable you use will likely be the name of a WordPress function, or function added by a plugin. It can also be an <span class=\"type\">object<\/span> method or a static class method.<br \/><br \/>Callables support parameters, just like functions.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">[if is_singular book]\r\n    Show related books\r\n[\/if]\r\n<\/pre>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">What if you need to check more than one condition?<\/h2>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">I needed to check <em>two<\/em> conditions:<br \/>1. Is user a &#8220;Member&#8221;<br \/>2. Is user logged in to WordPress<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>I needed support for if-elseif-else statements.<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">If a user was a <em>member<\/em>\u00a0I needed to show, &#8220;You are already a member&#8221;.<br \/>If a <em>user<\/em> was logged in, I needed to show a signup form. <br \/>Otherwise, I needed to show a <em>different<\/em>\u00a0signup form.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">I modified the code in the article above and found a solution for doing if-elseif-else conditions.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">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.<\/p>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\">Install the plugin<\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Clone <a href=\"https:\/\/github.com\/n8kowald\/if-elseif-else-shortcode\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"this repo (opens in a new tab)\">the repo<\/a> to your <strong>\/wp-content\/plugins\/<\/strong> folder and the activate the plugin.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">git clone https:\/\/github.com\/n8kowald\/if-elseif-else-shortcode<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">This allows you to use the following shortcode:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">[if is_admin]\r\n    Hi God, you're up late\r\n[elseif is_user_logged_in]\r\n    Hi Zero Cool, welcome back\r\n[else]\r\n    Welcome to the website. Please register to continue.\r\n[\/if]<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">We can use multiple <strong>elseif<\/strong> statements for an unlimited amount of conditions.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">[if is_admin]\r\n    Hi Mr The Plague.\r\n[elseif is_user_logged_in]\r\n    Hi Zero Cool, hack the planet!\r\n[elseif is_single]\r\n   Pool on the roof must have a leak.\r\n[else]\r\n    Welcome an0n. Please look around but don't steal my fries.\r\n[\/if]<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">You can use <strong>if<\/strong> on its own<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">[if is_admin]\r\n    Hi Mr The Plague.\r\n[\/if]<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">You can use <strong>if-else<\/strong> without the <strong>elseif<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">[if is_admin]\r\n    Hi Mr The Plague.\r\n[else]\r\n    Welcome an0n. Please look around but don't steal my fries.\r\n[\/if]<\/pre>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\">Allowed callables<\/h3>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The following callable functions are allowed by default:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">comments_open<br \/>get_field\r\nis_404\r\nis_admin\r\nis_archive\r\nis_author\r\nis_category\r\nis_day\r\nis_feed\r\nis_front_page\r\nis_home\r\nis_month\r\nis_page\r\nis_search\r\nis_single\r\nis_singular\r\nis_sticky\r\nis_super_admin\r\nis_tag\r\nis_tax\r\nis_time\r\nis_user_logged_in\r\nis_year\r\npings_open<\/pre>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">Adding callables<\/h2>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">To allow other callables you must use the <strong>if_elseif_else_shortcode_allowed_callables<\/strong> filter.<br \/><br \/>This &#8220;allow other functions&#8221; filter should be added to <a href=\"https:\/\/kinsta.com\/blog\/wordpress-hooks\/#where-to-register-hooks-and-their-functions\" target=\"_blank\" rel=\"nofollow noopener\">wherever you put your hooks<\/a>. That could be in:<\/p>\r\n<ul>\r\n<li>a custom plugin: <strong>wp-content\/plugins\/plugin\/plugin.php<\/strong><\/li>\r\n<li>a child theme&#8217;s functions.php: <strong>wp-content\/themes\/theme-child\/functions.php<\/strong><\/li>\r\n<li>or a theme&#8217;s functions.php: <strong>wp-content\/themes\/theme\/functions.php<\/strong><\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">&lt;?php\r\nadd_filter( 'if_elseif_else_shortcode_allowed_callables', function( $whitelist ) {\r\n    $whitelist[] = 'your_callable_here';\r\n \u00a0\u00a0\r\n    return $whitelist;\r\n}); <\/pre>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\">Supported callable types<\/h3>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>Function names: <strong>is_user_logged_in<\/strong><\/li>\r\n<li>Static\u00a0class\u00a0method\u00a0calls\u00a0(&gt;=PHP\u00a05.2.3): <strong>MyClass::myCallbackMethod<\/strong><\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">[if is_user_logged_in]\r\n    Hello user\r\n[elseif User::is_member_logged_in]\r\n    Hello member\r\n[else]\r\n    Hello, please log in\r\n[\/if]<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> in the above example, you would need to add <strong>User::is_member_logged_in<\/strong> to the allowed callables list.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\">Passing parameters to callables<\/h3>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Parameters are passed as space separated strings<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">[if is_singular books]\r\n \u00a0 \u00a0Related books here.\r\n[\/if] <\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">If your callable accepts multiple parameters<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">&lt;?php\r\nfunction is_garfield( $animal, $colour ) {\r\n    return $animal === 'cat' &amp;&amp; $colour === 'orange';\r\n}<\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">[if is_garfield cat orange]\r\n \u00a0 \u00a0 Yes, this is garfield.\r\n[\/if]<\/pre>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\">Testing<\/h3>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">If you want to simplify the main function or fix a bug, the <a href=\"https:\/\/github.com\/n8kowald\/if-elseif-else-shortcode\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"created a plugin (opens in a new tab)\">plugin<\/a> includes WordPress tests for refactored code.<\/p>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\">Install PHPUnit and PHPUnit Polyfills<\/h4>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">composer install<\/pre>\r\n\r\n\r\n\r\n<h4 class=\"wp-block-heading\">Install the WordPress testing library and database<\/h4>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Run this from the plugin directory. <br \/>Replace <strong>mysql_username<\/strong> and <strong>mysql_password<\/strong> with your MySQL username and password:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">.\/bin\/install-wp-tests.sh wordpress_tests mysql_username mysql_password<\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">You can then run tests with:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">vendor\/bin\/phpunit<\/pre>\r\n","protected":false},"excerpt":{"rendered":"<p>I found a great post which shows how to easily use an if-else shortcode in WordPress. [if is_user_logged_in] You are already logged in. [else] Display registration form. [\/if] A callable must be used in the [if] condition. In the above example, is_user_logged_in is the callable. This is a built in WordPress function to check whether [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[7,33],"tags":[],"class_list":["post-2271","post","type-post","status-publish","format-standard","hentry","category-web-development","category-wordpress"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/posts\/2271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/comments?post=2271"}],"version-history":[{"count":64,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/posts\/2271\/revisions"}],"predecessor-version":[{"id":2838,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/posts\/2271\/revisions\/2838"}],"wp:attachment":[{"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/media?parent=2271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/categories?post=2271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nathankowald.com\/blog\/wp-json\/wp\/v2\/tags?post=2271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}