Joomla & WordPress Tutorials, Info, Discussion, Tips | GavickPro Blog

WordPress Quick Tip #5 – digging into WordPress API

WordPress API includes features which can help you to avoid additional and unnecessary work on your site. Get to know these features. In this entry I will demonstrate certain API functions and classes I think are worth using.

__return_false and other

Occasionally you require a function which only return a single value – boolean, integer, etc. WordPress offers useful functions to do precisely this:

Keep these in mind the next time you need an example of how to disable some content using filters. Just use it in the following form:

add_filter('THE_FILTER_NAME', '__return_false');

instead of creating your own simple function.

selected, checked and disabled

These three functions are particularly useful if you are creating forms in WordPress. Instead of including IF statements in the form code, you need only use these functions. These functions each have the same syntax:

function_name($value, $value_to_compare = true, $echo = true);

$value is a value to check $value_to_compareis a value to compare with $value – if the values are the same then the selected, checked or disable attribute is added $echo – you can output the results directly or store in the other variable (if $echo = false)

So instead of using code like this:


<option value="1" <?php if ( $options['foo'] == 1 ) echo 'selected="selected"'; ?>>1</option>

you can use:


<option value="1" <?php selected( $options['foo'], 1 ); ?>>1</option>

HTTP API

WordPress has a rich HTTP API which should be used instead of external libraries like cURL. You can use the WP_Http class directly or use helper functions like wp_remote_get.

Remember that in some cases more specialized functions exists (i.e. fetch_feed). So, for example, for the RSS feeds you would make it easier on yourself to simply use this dedicated function.

The HTTP API is fully described here. As you can see, you can send using GET, POST, HEAD or custom HTTP requests.

In upcoming posts I will demosntrate other useful and interesting WordPress API functions and classes.

Share