Most useful WordPress functions you should know

WordPress started as just a blogging system in 2003 with a bit of code just to simplify the blog writing, but has evolved to be used as full content management system and so much more through the thousands of plugins and widgets and themes. During my research and learning WordPress I stumbled upon several really useful WordPress functions that I want to share with you.

WordPress Transient API

The Transients API in WordPress is a simple method for storing cached data in the database. It allows us to take resource-intensive queries and store them in temporary caches to improve performance. Transients are temporary, meaning that after a specified amount of time, the cached data will be deleted and re-cached, or updated.
Functions:

  1. get_transient(string $transient)– Used to retrieve the cached data
  2. set_transient ( string $transient, mixed $value, int $expiration ) – Used to store data in a cache
  3. delete_transient (string $transient) – Used to delete cached data

 WordPress HTTP API

HTTP stands for Hypertext Transfer Protocol and is the foundational communication protocol for the entire Internet. Even if this is your first experience with HTTP it’s likely that you probably understand more than you realize. At its most basic level, HTTP works like this: There are many different methods to send HTTP requests in PHP. The purpose of the WordPress HTTP API is to support as many of those methods as possible and use the one that is the most suitable for the particular request. The WordPress HTTP API can also be used to communicate and interact with with other APIs like the Twitter API or the Google Maps API.
Functions:

  1. wp_remote_get (string $url, array $args = array())– Retrieves a URL using the GET HTTP method.
  2. wp_remote_post ( string $url, array $args = array())– Retrieves a URL using the POST HTTP method.
  3. wp_remote_head ( string $url, array $args = array()) – Retrieves a URL using the HEAD HTTP method.
  4. wp_remote_request() – Retrieves a URL using either the default GET or a custom HTTP method (should be caps) that you specify.

WordPress Shortcode API

The Shortcode API is a simple set of functions for creating WordPress shortcodes for use in posts and pages. The API enables plugin developers to create special kinds of content (e.g. forms, content generators) that users can attach to certain pages by adding the corresponding short code into the page text.

Functions:

  1. add_shortcode ( string $tag, callable $func)
  2. do_shortcode(string $content, bool $ignore_html = false)

WordPress “Cron Jobs”

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.
Function:

  1. wp_schedule_event (int $timestamp, string $recurrence, string $hook, array $args = array())

mail function
wordpress allow you to send email with headers and attachments as well.
Function:

  1. wp_mail ( string|array $to, string $subject, string $message, string|array $headers = ”, string|array $attachments = array())

Comment functions

There are many cases where you need to display the commnets outside the comment loops. WordPress easily allow you to do this thing such as you want to load comments using ajax for some section of website.
Function:

  1. get_comments ( string|array $args = ” )
  2. wp_insert_comment (array $commentdata)

WordPress post function:

You may also face situation where you need to add wordpress post using ajax from example you implement frontend post where you need WordPress function
which used to create post using WordPress function.
Function:

  1. wp_insert_post (array $postarr, bool $wp_error = false)

RSS Feed with WordPress
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.
Function:

  1. fetch_feed(mixed $url)

Human Readable Time Format
The most common way is to display lates on your website is human readable format such as “1 hour”, “5 mins”, “2 days”. The wordpress allow you to do this by using
human_time_diff() function.

Function:

  1. human_time_diff (int $from, int $to = ”)

String validations
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.
You have to remove any slashes from PHP’s magic quotes before you call this function.
Function:

  1. wp_kses( string $string, array $allowed_html, array $allowed_protocols = array())

WordPress Object Cache:

Object cache can be used for caching data such as the result of complex database queries. you care for theme and plugin performance you should definitley read the wordpress codex entry for this topic!
Functions:

  1. wp_cache_add(int|string $key, mixed $data, string $group = ”, int $expire )
  2. wp_cache_set (int|string $key, mixed $data, string $group = ”, int $expire )
  3. wp_cache_get(int|string $key, string $group = ”, bool $force = false, $found = null )
  4. wp_cache_delete ( int|string $key, string $group = ”)
  5. wp_cache_replace ( int|string $key, mixed $data, string $group = ”, int $expire )
  6. wp_cache_flush()

Add a Comment

Your email address will not be published. Required fields are marked *