Friday, September 5, 2014

My First WordPress Plugin - Tutorial Part 2

2:55 AM

WordPress is a very flexible framework, simple to use, easy to understand and developer friendly environment. To create a plugin in WordPress basic understanding of hook and filter and action is needed.  This tutorial will guide you through basic knowledge of hooks and filter and action and focused more importantly on plugin to Custom Post Type and Custom Taxonomy.


In general, a hook is something that is particularly attached with another thing. As like that, hook defines the relation and dependency  between the functions and action or filter in WordPress.

  1. add_filter('the_content','helloworld');
  2. function helloworld($content){
  3. $content=$content."<br>Hello World. This is Semicolon Dev ";
  4. return $content;

Lets take the above example. Here, we have used a filter function to get the content and add certain text at the end.  Line 1 has a filter:  add_filter('first_argument','second_argument') ; The first argument is used to get the content/description of the post and second one helloworld is a call to a function. Here helloworld is a function hooked with the_content.

A hook is generally used to change the default nature of the WordPress. The above example is used to change the default behavior of normal posting to adding content to each post.

Action and Filter :

Action and Filter are alias; An action hook is used to perform certain action and insert code in the WordPress core while filer hook is used to perform certain task on the content and supposed to return a value and are associated with an action.

add_action($hook$function_to_add$priority$accepted_args );
add_filter$tag$function_to_add$priority$accepted_args ); 

Custom Post Type:

It is a WordPress linked word which is normally harder to understand. We have a admin menu-Post to the left so Custom post type is a method to create our own post type naming it, the way we prefer, providing the features we feel necessary.

function custom_post(){
$labels_post = array(
'name' =>_x( 'Products', 'post type general name' ),
'singular_name' =>_x( 'Product', 'post type singular name' ),
'add_new' =>_x( 'Add Product', 'product' ),
'all_items' =>__( 'Available Products' ),
'add_new_item' =>__( 'Add New Products' ),
'edit_item' =>__( 'Edit Product' ),
'new_item' =>__( 'New Product' ),
'view_item' =>__( 'View Products'),
'search_items' =>__( 'Search Products' ),
'menu_name' => 'Products'
$args= array(
'labels' => $labels_post,
'description' => 'Product Description',
'public' => true,
'supports'                => array( 'title', 'editor', 'thumbnail','comments' ),
'menu_position' => 10
register_post_type('name-of-the-post', $args);

Here, we have created a Product as a custom post type enabling the developer to customize the available field.

Custom Taxonomy:

Taxonomy is a method of grouping things under a particular heading as like category and tags which are used to bind the post with certain tags and under particular category. 

register_taxonomy'product_categories''name-of-the-post'$args );
The first argument is the name of the taxonomy, second one is the name of the post-type and last argument is an array which is used to define the necessary functions of the WordPress to be included.

The side figure shows that the tag field has been created where the capabilities to edit, add and remove can be defined during its creation.Here $args is similar to that of above $args on labels declaration but its characteristics is defined by other values;

'hierarchical'             => true,
'show_admin_column'           => true,
'show_ui'                              => true,
'rewrite'                   => array('slug' => 'product-category', 'with_front'                                                                           =>FALSE),
'capabilities'              => array('assign_terms' => 'edit_products',
                                                   'edit_terms'   => 'edit_products',
                                                   'manage_terms' => 'edit_others_products',
                                                   'delete_terms' => 'delete_others_products')
Meta Box:

If we are creating our custom type post then it is certain that we need extra information to be included with our post which can be done using meta-boxes.

Creating a meta box is a simple task of adding an action during the post creation and it is performed with the below code:

Using the hook add_meta_box , we are set to create a metabox and second argument is a function which contains the features like id,title and its parent post type of the meta box.

add_meta_box$id$title$callback$post_type$context,$priority$callback_args );

  • $callback is the  reference to a  function that determines the label and input types within the meta data.The reference function takes $post as argument and a nonce field is included which is required later for verification while saving the data. wp_nonce_field  has action name and nonce name.  
  • $post_type is the name of the custom post type.
Creating meta box does not mean WordPress is going to save the data. A separate action, to save the data in the meta box table of the wordpress, has to be defined as

In this case, the function takes $post_id as argument and it has to first verify the data with the nonce and update the post.

wp_verify_nonce($_POST['nonce-name'], 'action-name' )
$variable = $_POST['name-of-the-input-field'];
update_post_meta( $post_id, 'metabox-id', $variable );

Now, we have created a Custom Type Post and Custom Taxonomy and able to include extra field like product price using the meta-box.

Thanks for reading. Stay tuned for more posts in the series.

Written by

We are passionate programmers. Enjoying the rich platforms of Semicolon Family of Programming languages we are proud to call ourselves Semicolon Developers.


Post a Comment


© 2013 Echo "Semicolon Developers"; Kathmandu. All rights resevered @ Semicolon Developers Network Pvt. Ltd.. Designed by Templateism

Back To Top