Be careful with PHP empty when using it in Wordpress shortcodes

You can often find examples on web resources for studying and developing plugins for Wordpress that suggest using PHP empty to check shortcode attributes. But let's see how this function works and what errors can occur if it is used incorrectly.

Features of the empty () function that you need to know about

According to the manual on php.net, this function checks if the variable is empty.

If the variable does not exist, then empty () will not throw an error. For example, let the variable $ foo not be set:

if( empty($foo) ){ echo "variable is empty"; } // "variable is empty" 

Therefore, an additional check is needed with isset ():

 $foo = 1; if( isset($foo) && !empty($foo) ){ echo "variable = ".$foo; } //  "variable = 1" 

The values ​​that PHP empty () considers empty are:

  • "" (strings - strlen ('') == 0);
  • 0 (integers - (int) 0);
  • 0.0 (floating point numbers - (float) 0.0);
  • "0" (lines - strlen ("0") == 1);
  • NULL;
  • FALSE;
  • array () (empty arrays - count (array ()) == 0).
 $string_1 = ''; echo strlen($string_1); // 0 if( empty( $string_1 ) ){ echo 'string_1 is empty'; } //  "string_1 is empty" $string_2 = '0'; echo strlen($string_2); // 1 if( empty( $string_2 ) ){ echo 'string_2 is empty'; } //  "string_2 is empty" 

A string ($ string_2) of one character length with a string zero ('0') is also considered empty () by the empty function.

But the line with one space does not consider empty:

 $string_3 = ' '; echo strlen($string_3); // 1 if( empty( $string_3 ) ){ echo 'string_3 is empty'; } //    

Let's look at the practical use of empty PHP and see how this function can lead to unexpected results.

Using empty () to validate shortcode attributes in Wordpress plugins

For example, you are developing a plugin for Wordpress, in which articles from different authors will be displayed using shortcodes. Each author’s account page will show his own articles ("self") and articles of other authors ("another") for the last month. Using the shortcode, you can control the number of articles displayed. By default, if the attributes "self" and "another" are not set, we will display, for example, 25 own posts and 15 posts of other authors.

 [author_posts] 

To change the value, add other quantities to the shortcode attributes:

 [author_posts self="10" another="5" ] 

Let's see what is in the plugin code:

 <?php /** * @package author_posts */ /* * Plugin Name: Author posts * Description: Author posts * Version: 1.0.0 * Author: V.Taran */ function posts_shortcode( $atts ) { $atts = shortcode_atts( array( 'self' => '', 'another' => '', ), $atts, 'author_posts' ); if(!empty($atts['self'])){ echo $atts['self']." post(s)"; }else{ echo "25 self posts"; } if(!empty($atts['another'])){ echo $atts['another']." post(s)"; }else{ echo "15 posts of another authors"; } } add_shortcode( 'author_posts', 'posts_shortcode' ); 

Here we simply print the data that the user passed through the shortcode. As a result, we will see 10 of our own posts and 5 of other authors.

Wordpress Shortcode Output

If we don’t want to see, for example, posts of other authors, we will write “another = 0” in the shortcode and posts of other authors will not be displayed. But no! The PHP empty () function considers the string "0" as an empty value, and instead of the "0" posts of other authors, it displays the default value, i.e. 15 entries instead of zero.

In other words, these two entries are similar:

 [author_posts another="0" ] [author_posts ] 

For the shortcode to work correctly, change the condition:

 $is_null = (int)(-1); 'another' => $is_null, if($atts['another'] > -1 ){ echo $atts['another']." post(s)"; }else{ echo "15 posts of another authors"; } 

If the "another" attribute is not set, then by default let it be (-1). Then in the condition we assume that the attribute is given if it is greater than (-1).

So this shortcode:

 [author_posts another="0" ] 

now everything displays correctly to us, i.e. by default 25 articles of the author (since the attribute "self" is not specified) and not a single article of other authors.

Features of php empty to be considered in Wordpress shortcodes

All code:

 <?php /** * @package author_posts */ /* * Plugin Name: Author posts * Description: Author posts * Version: 1.0.0 * Author: V.Taran */ function posts_shortcode( $atts ) { $is_null = (int)(-1); $atts = shortcode_atts( array( 'self' => '', 'another' => $is_null, ), $atts, 'author_posts' ); if(!empty($atts['self'])){ echo $atts['self']." post(s)<br>"; }else{ echo "25 self posts<br>"; } if($atts['another'] > -1 ){ echo $atts['another']." post(s)<br>"; }else{ echo "15 posts of another authors<br>"; } } add_shortcode( 'author_posts', 'posts_shortcode' ); 

You can come up with your own condition, it is important that you see the features of the empty () function and do not make a mistake when using it. Put this file in the Wordpress folder "/ wp_content / plugins /" and activate the plugin in the admin panel, it will work.

Options for replacing empty () with other PHP functions

To understand how to replace empty (), you need to know what we have, for example:

  • if it is an array (array ()), then it is better to check it with the functions count or sizeof;
  • if the variable can be false, then use if (false == $ var);
  • if the variable is not set, then using isset or is_null ();
  • numbers will use is_numeric ().

Many options can be found to make your program work as accurately as possible, write your own function, which will analyze the data as you need.

We found that empty PHP is a function that checks if a variable is empty. You need to remember what variable values ​​are defined as zero if you use it in your code, so you don’t have to look for why the program is not working properly.

Source: https://habr.com/ru/post/K16353/


All Articles