Some useful functions for WordPress plugins development

A glance on wordpress plugins

WordPress is one of the popular CMS. I like it due to its simplicity. A person who does not know PHP programming can customized WordPress highly, from back-end. Along With, WordPress has lots of PHP classes and functions for PHP developer to show their magic.

WordPress is not an object-oriented paradigm. You occasionally can find classes but it does not have true concepts of object-oriented programming. You often just call functions to get your way. However, I believe, this thing make development in WordPress faster. Although, it lacks the organization of related methods and functionalities but still it is a charm for developers due to its ease.

Programming level customization normally done through plugins or you can say it is the best way to customize WordPress. There are tons of article on web about how to create plugin. In my article I will not focus on how plugin is developed but I will listed some useful functions for its development

WordPress plugins are normally easy to install. You just have to paste your plugin in wp-content/plugins folder and activate it through admin panel and it starts to work. Also WordPress has mechanism that help you to create plugin without changing the code for core libraries. They call it hooks. To know about hooks , read my another article, WordPress hooks – filters and actions.

Your plugin could be small as a single file and could be large as a folder contain lots of php, javascript, CSS files and images. All you have to do is to put following header, in a file you want to make entry point for your plugin. This file will be called on every page load in WordPress

/*
 *
 * 	Plugin Name: Greyinfotech Job Portal
 * 	Plugin URI: greyinfotech.com
 * 	Description: Job Manager for all jobs related operations.
 * 	Version: 1.0
 * 	Author: Junaid Hassan
 * 	Author URI: junaidhassanalvi.Wordpress.com/
 *
 */

All information is optional except Plugin Name. From here, WordPress recognizes that this is entry point for a plugin.

Function often used in WordPress plugins

Every plugin must contain two functions. One for activation, one for deactivation. You hook these two functions, to be called when plugin is activated or deactivated through admin panel as follow

register_activation_hook(__FILE__, 'activate');
register_deactivation_hook(__FILE__, deactivate');

Note that, these two functions must be in the same plugin file, which you define as an entry point. Following are few functions, commonly used in plugin.

In WordPress you have global array $wp_filter, this array can be manipulated to see all hooks added so far.

add_filter and add_action is used to add functionality into system without effecting core APIs and add_shortcode is to add something to html design at specific place. Following is the example of short code

add_shortcode('my_shotcodes', 'my_shotcodes_func');

With the help of add_shortcode you can add shortcode for my_shotcodes tag.. In html where you want to add your desire html in the post, you add following

[my _shotcodes ]

or

[my _shotcodes param1=”register” param2=”form”]

Above are two examples for shotcodes, both will execute function my_shotcodes_func and replace my_shotcodes tag with the response return by the my_shotcodes_func. Second example above also passing two parameters (param1 & param2) to my_shotcodes_func. The function will get these in a form of array. Param1 and param2 will be the keys of array.

Functions could be used, as per need

If you are creating your own tables in plugin, you have to use dbDelta. dbDelta is very handy as it Identifies difference between new and old version changes and update accordingly.

If you have your own custom roles then you have to use add_role / remove_role / get_role to add/ delete and retrieve information regarding the roles. Also with custom role, custom capabilities could be added to the system. add_cap / remove_cap used for this purpose.

Something you need to add a static page or post to show on the front-end. Here, wp_insert_post / wp_delete_post is very handy. You have to define attributes for a post in any array and pass to the wp_insert_post function. Following is an example

$_p = array();
$_p['post_title'] = __('Job listing');
$_p['post_status'] = 'publish';
$_p['post_type'] = 'page';
$_p['comment_status'] = 'closed';
$_p['ping_status'] = 'closed';
$_p['post_category'] = array(1);
$the_page_id = wp_insert_post($_p);

Above are the most common functions I have seen, to be used in plugins so far. However, thousands of plugins are available and every developer has its own approach. But it is always better to go for the best practices for any framework, also it is always better to comment and test your code thoroughly before publish.

WordPress hooks – filters and actions

A glance on WordPress hooks – filters and actions

WordPress has become far more popular than anyother blogging framework in a very short period of time. With the time, along with the popularity, it also extends. Tons of plugins are available on WordPress’ website.

WordPress provides very handy mechanism to extend functionality of wordpress with out messing-up with its core libraries. This thing is very helpful when you try to upgrade the version, as you do not have to remember what you changed. The objective to makes new changes separated from original code is achieved by a mechanism known as Hooks in WordPress terminology.

What is hook ?

There are two types of hooks. We will talk about later but first, what is hook? Named suggest, to hook something, to attach something, to tag something. Of course, it is used to attach some extra functionality with WordPress’ core behavior for any event. Instead of, introducing new code in core libraries of WordPress we write code separately in a file (that could be your plugin file in plugins folder) and attach the reference or hook the reference to that code in WordPress global hook list and call that code where you want it to be executed. Looks complicated? But it is not.

Types of hooks – action and filter

As I said, there are two types of hooks, Number one is filter and second one is action. Filter is when you want to modify something; it could be anything, any variable, any text which is going to be echoed. On the other side, action is when you want to run something entirely new. In other words, you passed some value to filter and it changes that value and pass back to you while to action you just call and it will perform something.

Ok, let understand filter first with example, we have two magic function add_filter and apply_filters

function modify_data(data) {
   return $data." Is passed on ".date('l jS \of F Y h:i:s A');
}

add_filter("my_hook","modify_data");

First argument is a unique identifier for your filter. You will call it through this identifier. Second is the name of function that we created. add_filter also has 2 other parameters, priority that describe the sequence in which all functions hooked with a single hook will be executed. Default value for priority is 10 but you can change as say. And second one is number of arguments passed to filter.

Ok, now simply where you want to execute that filter just call it with apply_filters like this

$data = apply_filters(("my_hook", $data );

As you can figure out the above statement, it is quite self-explanatory.

Similarly for action but here we want to start another process to do something. add_action and do_action are magic functions.

function email_friends( $post_ID )
{
   $friends = 'bob@example.org, susie@example.org';
   wp_mail( $friends, "sally's blog updated", 'I just put something on my blog: http://blog.example.com' );

   return $post_ID;
}
add_action('publish_post', 'email_friends');

Now you can call your action

do_action('publish_post' , 12);

You call the action by providing its unique identifier and the parameter required by function.

In the example for filter, we created our own hook that is my_hook but in action’s example we used a hook that is defined by WordPress already.
It elaborates that WordPress has defined many hooks for their system and if you exactly know what hook is required or where you want your code to be executed you can use pre-defined hook or you always free to create new one.

List of all hooks available on WordPress webiste