Creating a custom plugin in wordpress

Posted By : Ishaan Madan | 31-Mar-2018

 

Introduction
 
In this blog we will learn how to create a custom plugin in wordpress. A plugin in wordpress is a set of re-usable code which helps us with any specific task. For instance, creating custom taxanomies, creating custom theme options etc.
I This blog we will aim to create a custom plugin which upon activation will automatically create a "custom post type" named "sample-post-type" in wordpress dashboard.
 
Essentials Steps.
 
A very basic plugin in wordpress comprises of a single php file which loads all the necessary functionality onto the dashboard over some default wordpress hook.
 
1. To Allow the wordpress to recognize our plugin we would need a .php file. As per the wordpress standards the file shoud be located at "wp-content/plugins/<plugin-directory>". In our example we will be creating "sample-post-type.php" in "sample-post-type" directory inside "wp-content/plugins" folder.
 
2. Upon creation, add the following code to the php file which allows wordpress to identify the plugin.
 
<?php
/**
* Plugin Name: Custom Sample Post Type
* Plugin URI: https://www.sample.com
* Description: A custom sample post type built for example.
* Version: 1.0 

**/

The above mentioned code should be enough to display our plugin inside the "Plugins" tab in the dashboard.
 
 
 
3. Now, to provide our plugin with the functionality to add custom post type we will add the following code to our php file over "init" hook using "add_action" method.
 
 
// Register the Custom Sample Post Type
 
function register_cpt() {
 
    $labels array(
        'name' => _x( 'Sample Post Type''sample_post_type' ),
        'singular_name' => _x( 'Sample Post Type''sample_post_type' ),
        'add_new' => _x( 'Add New''sample_post_type' ),
        'add_new_item' => _x( 'Add New Sample Post Type''sample_post_type' ),
        'edit_item' => _x( 'Edit Sample Post Type''sample_post_type' ),
        'new_item' => _x( 'New Sample Post Type''sample_post_type' ),
        'view_item' => _x( 'View Sample Post Type''sample_post_type' ),
        'search_items' => _x( 'Search Sample Post Type''sample_post_type' ),
        'not_found' => _x( 'No Sample Post Type found''sample_post_type' ),
        'not_found_in_trash' => _x( 'No Sample Post Type found in Trash''sample_post_type' ),
        'parent_item_colon' => _x( 'Parent Sample Post Type:''sample_post_type' ),
        'menu_name' => _x( 'Sample Post Type''sample_post_type' ),
    );
 
    $args array(
        'labels' => $labels,
        'hierarchical' => true,
        'description' => 'Sample Post Type filterable by genre',
        'supports' => array'title''editor''author''thumbnail''trackbacks''custom-fields''comments''revisions''page-attributes' ),
        'taxonomies' => array'genres' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'menu_icon' => 'dashicons-format-audio',
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'
    );
 
    register_post_type( 'sample_post_type'$args );
}
 
add_action( 'init''register_cpt' );
 

The above code upon activating the plugin will lead to the creation of a new custom post type in the dashboard as shown below.

 

 
Thanks
Request for Proposal

Recaptcha is required.

Sending message..