Certified Wordpress Developer Functions API and documentation

Functions API and documentation
 


he WordPress API stands for the WordPress Application Programming Interface. It can be separated into multiple API sections / topics. Each covers the functions involved in and use of a given set of functionality. Together they form what might be called the WordPress API, which is the plugin/theme/add-on interface created by the entire WordPress project.

If you've read through all of these you should have a good sense of how to extend WordPress through Plugins.

  • Plugin API - Hooks, Actions, and Filters to use in your Plugins (version 2.1; has links to older version articles)
  • Shortcode API - A tutorial and reference for the shortcode API (new in version 2.5)
  • Dashboard Widgets API - A reference with examples for adding new widgets to the admin dashboard.
  • Settings API - A reference with examples for adding new settings to existing settings screens.
  • Options API - Details about the generic option storage system.
  • Transients API - Details about the temporary/time-constrained data storage system.
  • Widgets API - A reference with examples for creating widgets for use in sidebars.
  • Quicktags API - A reference for adding buttons to the HTML editor.
  • Rewrite API - Details about the URL rewriting API.

Few APIs are discussed

WordPress Transient API
set_transient(), get_transient(), delete_transient()

This is a function set very similar to get_options() and update_options() which helps to easily store and retrieve data in your options database table. The big difference here is that you also pass a time parameter which acts as expiration date for the database entry.

Once the time has expired the data is removed. This is especially useful if you want to cache data or query results for a short amount of time. An example would be a twitter widget which retrieves data from the twitter api, but since twitter is down pretty often it would be wise to save that data for a few minutes. That also would speed up the site for subsqequent page requests since you don’t need to connect to an external API.

It works pretty easy: the function set_transient accepts 3 parameters:

set_transient($transient, $value, $expiration);

so saving a value to your database for an hour would look like this:

set_transient('the_name', $special_query_results, 60*60);

getting the value like this:

$value = get_transient('the_name');

https://codex.wordpress.org/Transients_API


WordPress “Cron Jobs”
wp_schedule_event(time(), 'hourly', 'my_schedule_hook');

Schedules a hook which will be executed by the WordPress actions core on a specific interval, specified by you. The action will trigger when someone visits your WordPress site, if the scheduled time has passed.

So if you want to execute some code on a regular base, like checking an RSS Feed, doing a database backup or reseting database values this function will do it for you. Unfortunatley its not as easy to use as transient but here is a short article on how it is done:
https://themocracy.com/2010/02/wp-cron-automating-scheduling/
https://codex.wordpress.org/Function_Reference/wp_schedule_event


WordPress HTTP API
wp_remote_get( $url, $args = array() );

An easy to use function if you want to retrieve the content of a Webpage. The function stores the result within an easily accessible array. Not only do you get the result content of the webpage, you also get header informations and response codes. This function is also capable of retrieving feed content which also makes it useful if you want to create a twitter plugin or an rss reader with wordpress.

https://codex.wordpress.org/HTTP_API
https://codex.wordpress.org/Function_API/wp_remote_get


Easily Fetch an RSS Feed with WordPress
$feed = fetch_feed( $uri );

fetch_feed is another simple wordpress method to get feed content. It also has the added benefit that it uses the SimplePie and FeedCache functionality for retrieval and parsing and automatic caching.

https://codex.wordpress.org/Function_Reference/fetch_feed


WordPress mail function
wp_mail()
wp_mail( $to, $subject, $message, $headers, $attachments );

Example:

$to = '[email protected]';
$subject = 'Hello Kriesi!';
$message = 'This message was sent by wordpress'

$mail = wp_mail($to, $subject, $message);

if($mail) echo 'Mail delivered';

An incredible useful and exceptional easy to use function that also allows you to send headers and attachments, allows plain text sending and html messages and quite a few other options!

https://codex.wordpress.org/Function_Reference/wp_mail


WordPress Human Time
human_time_diff( $from, $to )

A feature to mimic twitter like time display. Instead of outputting the time in a basic format you can display it like this:

Kriesi posted 13 hours ago

https://codex.wordpress.org/Function_Reference/human_time_diff

 

WordPress get comments
get_comments()

Sometimes its neccessary to retrieve the comments outside of the comments loop like I did with the Avia Feedback Box Plugin, where comments are displayed via ajax. This function helps you in doing so ;)

https://codex.wordpress.org/Function_Reference/get_comments
WordPress validation of strings

wp_kses($string, $allowed_html, $allowed_protocols);

wp_kses is a very usefull function when it comes to sanitizing untrusted user input. This function makes sure that only the allowed HTML element names, attribute names and attribute values plus only sane HTML entities will occur in $string.

https://codex.wordpress.org/Function_Reference/wp_kses

 

WordPress text transformation
wptexturize()

A text transformation function that converts commonly used strings into the typographical correct signs. Used for dashes, ellipsis etc and will also add typograpic quotes to certain phrases.

https://codex.wordpress.org/Function_Reference/wptexturize

wpautop()

The last text transformation tool on the list is used by wordpress to ad

tags to a string by replacing double
tags

https://codex.wordpress.org/Function_Reference/wpautop
WordPress Shortcode API

add_shortcode(), do_shortcode()

add_shortcode() is an easy way to create macros for your post content. for example lets say you want to wrap some content inside the post area within a div with some additional classes and ids that allows you to create multiple columns. you could of course just switch to the html editor and enter

Content goes here


Easier and more convenient especially if you are dealing with customers who dont know html would be a shortcode solution within your functions.php file:

function column_shortcode( $atts, $content = null ) {
   return '
';
}

add_shortcode('one_third_column', 'column_shortcode');

You could then use this shortcode in your post content:

[one_third_column]Content goes here[/one_third_column]

If you would like to nest shortcodes within each other you would need to make sure to add the do_shortcode method to your function like this:

column_shortcode( $atts, $content = null ) {
   return '
';
}

https://codex.wordpress.org/Shortcode_API


Create WordPress post with a php function:
wp_insert_post()

This function inserts posts pages and custom post types in the database. It sanitizes variables, does some checks, fills in missing variables like date/time, etc. very handy when you need to create and insert posts by user input. This function fits perfectly if you have a front end form for example and users can suggest posts.

https://codex.wordpress.org/Function_Reference/wp_insert_post

 

Create WordPress comment with a php function:
wp_insert_comment()

Similar to wp_insert_posts this function inserts a comment. Might also come in handy ;)

https://codex.wordpress.org/Function_Reference/wp_insert_comment

 

WordPress Object Cache:
wp_cache_add(),  wp_cache_set(),  wp_cache_get(),  wp_cache_delete, wp_cache_replace(), wp_cache_flush

WP_Object_Cache is WordPress’ class for caching data which may be computationally expensive to regenerate, such as the result of complex database queries. If you care for theme and plugin performance you should definitley read the wordpress codex entry for this topic!

https://codex.wordpress.org/Function_Reference/WP_Cache

 

Kill wordpress execution:
wp_die()

An appropriate end for this post: the wp_die function

wp_die kills the WordPress execution and display HTML message with error message.

A call to this function complements the die() PHP function. The difference is that HTML will be displayed to the user. It is recommended to use this function only when the execution should not continue any further.

https://codex.wordpress.org/Function_Reference/wp_die

 For Support