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.
- wp_register_script : to register your JavaScript code. It takes two parameters. One is scrpt name and another is the path to script file. It loads script file in html’s Javascript script tag
- wp_register_style : similar to wp_register_script function, it uses to load style sheet.
- add_action : to hook your action hook into hook list.
- do_action : to perform all hooks you add for particular action.
- add_filter : hook your filter into hook list.
- apply_filters : to execute all filters in hook list.
- add_shortcode : to add piece of code at any place in html design.
- add_menu_page / remove_menu_page : add or remove menus from menus list in admin panel.
- add_submenu_page / remove_submenu_page : add or remove submenus form menus list in admin panel.
- add_option / delete_option: to add or remove plugin configuration from option table.
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.