Building WordPress Plugin Development

Webbing Wednesdays Week #6

Building WordPress Plugin


Plugins are the heart of WordPress. It enables the website to create the features or modify them without changing the core files of the WordPress. It is advisable to create a plugin to enhance the WordPress features and take maximum advantage of this amazing leverage WordPress offers.

Here is a basic definition of WordPress Plugin:

A Plugin is a set of functions written in PHP to add specific feature(s) or service(s) to the website. A Plugin is seamlessly integrated with WordPress and its application interface offers access points and methods to access the same. Are you looking to enhance your WordPress website or a blog to have certain feature as a requirement? Well, WordPress Plugin repository is the first thing one should check before building up the plugin. If close match is found it is advisable to modify that plugin to match the requirements, however if not, just follow the guidelines of this article which will certainly offer control over building the WordPress Plugin. In order to explain how WordPress Plugin should be built, it is imperative to understand the folder structure of WordPress.

WP Folder Structure and Where to create Plugin

WordPress has many folders like wp-admin, wp-content etc. wp-content folder has a subdirectory called plugins. This is where the plugin should reside. The plugins of less complexity and the ones, which requires only one file, can remain in that folder. However to build more complicated plugins, it is important to create a folder named after your plugin name. This folder can contain CSS, JS and HTML along with the PHP functions and coding structure that has to be integrated. Not only this, if this plugin is planned to be made available to developers, it will be a very good practice to attach a read-me.txt file in order for developers to understand programming structure and the definitions of the functions. The file should include the author name, along with the revision details, URL, Name of the plugin and the licensing terms. The question arises on how WordPress understands that a new plugin has been created?

How to Define a Plugin

Any developer needs to define the plugin for WordPress to recodnize it and allow it to be installed, removed and activated. In order to achieve this, the main file should contain the following details:

Plugin Name: Name Of The Plugin
Plugin URI: http://plugin url
Description: A brief description of the Plugin.
Version: The Plugin's Version Number, e.g.: 1.0
Author: Name Of The Plugin Author
Author URI: http://plugin uri 
License: A "Slug" license name e.g. GPL2

The above code is highly self-explanatory. One should replace each line of the code to the correct and relevant details. WordPress however only understand the Name of the Plugin. Once that is defined WordPress will definitely consider the file as a plugin.

WordPress Plugin Hooks

Is it necessary to create all the functionalities of the plugin? The answer is No, you can certainly leverage the functionalities of some other plugins installed in WordPress. For achieving this WordPress plugin needs to be connected to one or more WP Plugins which is called “Hooks”. The way Plugin hooks work is that at various times while WordPress is running, WordPress checks to see if any Plugins have registered functions to run at that instance. If any plugin is supposed to run, it starts running. Needless to say that; the execution of these plugins would certainly change the behavior of WordPress as per the requirement. These Hooks are of two types. Action and Filters.


Action offers a way for any function to let the other function hook into itself so that at any specific instance the additional code can run. Actions are counter part to filters. The way to call them is by using the function add_action() function and passing two parameters: First one is the name of the action and second one is the commands of execution which means the functions needs to be hooked. For example,

add_action( 'init', 'do_some_stuff' );

Above example can be understood as, call the function do_some_stuff() when WordPress fires the init action.

function search_filter( $query ) {
    if ( ! is_admin() && $query->is_main_query() ) {
        if ( $query->is_search ) {
            $query->set( 'post_type', array( 'post', 'movie' ) );
add_action( 'pre_get_posts', 'search_filter' );


Filters provide a way to modify a certain piece of data using one function for the other one. Filters can be called by using add_filter() function. The function needs two parameters: Name of the filter you want to hook into, and the name of your callback function that will be executed when the filter runs. For example,

add_filter( 'techtic_title', 'modify_the_title' );

The above piece of code will call the modify_the_filter() function when WP applies the techtic_title filter.

<?php function add_post_signature( $content ) {
    $content .= '<p>This text appears at the bottom of each post</p>';
    return $content;
add_filter( 'the_content', 'add_post_signature' ); 


Plugin Activation deactivation hook:

The activation and deactivation hooks provide a way to perform actions when a plugin is either activated or deactivated.

register_activation_hook( __FILE__, 'pluginprefix_function_to_run' );
register_deactivation_hook( __FILE__, 'pluginprefix_function_to_run' );

The first parameter of each function points to the file where the authorship and plugin name is defined. These two functions are usually defined in the main file itself, however it is important to understand that if these functions are defined in some other files then the first parameter needs to be modified to point to the main file.


How would you call a shortcode into the page or a post or anywhere? Well WordPress offers an API called Shortcode API, a simple set of functions to create the macro code for executing it in post or page content. Minimal example or rather a code of PHP required to create a Shortcode:

function foobar_func( $atts ){
	return "foo and bar";
add_shortcode( 'foobar', 'foobar_func' );


Plugin Name: Custom Post Type Widget
Plugin URI: 
Description: Manage custom  post type list Option  from admin.
Version: 1.00
Author: Ketan Umretiya
Author URI:

class cpt_widget extends WP_Widget 
 	function __construct() 
			// Base ID of your widget
			// Widget name will appear in UI
			__('Custom Post Type Widget', 'cpt_widget_domain'),
			// Widget description
			array( 'description' => __( 'Add Custome post type slug here', 'cpt_widget_domain' ), )
	// Creating widget front-end	// This is where the action happens
	public function widget( $args, $instance ) 
		$title = apply_filters( 'widget_title', $instance['title'] );
    	$slug = apply_filters( 'widget_slug', $instance['slug'] );
    	$perpage = apply_filters( 'widget_perpage', $instance['perpage'] );
    	$listtype = apply_filters( 'widget_listtype', $instance['listtype'] );
    	$imgsize = apply_filters( 'widget_listtype', $instance['imgsize'] );
		// before and after widget arguments are defined by themes
		echo $args['before_widget'];
		if ( ! empty( $title ) )
			echo $args['before_title'] . $title . $args['after_title'];
		//Print post type list here
		//	echo __( 'Ketan -->>>>Hello, World!', 'cpt_widget_domain' );
		echo '<div class="cpt_widget">';
		$temp = $wp_query; 
		$wp_query = null; 
		$wp_query = new WP_Query();  
	 	if($wp_query->have_posts()) {
      		if($listtype == 'ulli') {
        		echo '<ul clas="wcp_list">';
      		if($listtype == 'simple') {
        		echo '<div class="ptaglist">';
			while ($wp_query->have_posts()) : $wp_query->the_post(); 
            	$imag =get_the_post_thumbnail($wp_query->ID,$instance['imgsize']);              
                //use for display ul li ************************************************************
                if($listtype == 'ulli') {
                	echo '<li>';
                    <a href="<?php the_permalink(); ?>">
					if($imag) {
                    	echo '<span class="image_list">'.$imag.'</span>';
                    echo '<span class="title_list">'.get_the_title().'</span>';
                    echo '</a>';
                    echo '<p><span class="content_list">'.substr(get_the_excerpt(), 0,100).'</span></p>'; 
                    echo '</li>';
                 //use for display simple list   ***************************************************** 
				if($listtype == 'simple') {
					echo '<div class="wcp_grdlist">';
						<a href="<?php the_permalink(); ?>" >
					if($imag) { echo '<span class="image_grid">'.$imag.'</span>'; }
					echo '<span class="title_grid">'.get_the_title().'</span>';
					echo '</a>';
					echo '<p><span class="content_grid">'.substr(get_the_excerpt(), 0,25).'</span></p>'; 
					echo '</div>';
                //use for display table tr list   *********************************************
                <!-- LOOP: Usual Post Template Stuff Here-->
			<?php endwhile; 
			if($listtype == 'ulli') {
				echo '</ul>';
			if($listtype == 'simple') {
				echo '</div>';				
			echo '<p>No Post Found</p>';
		echo '</div>';         
		//close post type list print 
		echo $args['after_widget'];
	// Widget Backend
	public function form( $instance ) 
		if ( isset( $instance[ 'title' ] ) ) {
			$title = $instance[ 'title' ];
		} else {
			$title = __( 'New title', 'cpt_widget_domain' );
		if ( isset( $instance[ 'slug' ] ) ) {
			$slug = $instance[ 'slug' ];
		if ( isset( $instance[ 'perpage' ] ) ) {
			$perpage = $instance[ 'perpage' ];
		if ( isset( $instance[ 'listtype' ] ) ) {
			$listtype = $instance[ 'listtype' ];
		if ( isset( $instance[ 'imgsize' ] ) ) {
			$imgsize = $instance[ 'imgsize' ];

		// Widget admin form
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="				<?php echo esc_attr( $title ); ?>" />
			<label for="<?php echo $this->get_field_id( 'slug' ); ?>"><?php _e( 'Select Post Type:' ); ?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'slug' ); ?>" name="<?php echo $this->get_field_name( 'slug' ); ?>
" type="text" value="<?php echo esc_attr( $slug ); ?>" />
			<select class="widefat" id="<?php echo $this->get_field_id( 'slug' ); ?>" name="<?php echo $this->get_field_name( 'slug' ); ?>">
				$post_types = get_post_types( '', 'names' );     
				foreach ( $post_types as $post_type ) {
					<option <?php if($instance[ 'slug' ] == $post_type){echo 'selected="selected"'; } ?> value="<?php echo $post_type; ?>"><?php echo $post_type; ?></option>';
				<?php  }
			<label for="<?php echo $this->get_field_id( 'perpage' ); ?>"><?php _e( 'Display Post No:' ); ?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'perpage' ); ?>" name="<?php echo $this->get_field_name( 'perpage' ); ?>" type="text" value="				<?php echo esc_attr( $perpage ); ?>" />
			<label for="<?php echo $this->get_field_id( 'perpage' ); ?>"><?php _e( 'Select List Type' ); ?></label>    
			<select class="widefat" id="<?php echo $this->get_field_id( 'listtype' ); ?>" name="<?php echo $this->get_field_name( 'listtype' ); ?>">
				<option <?php if($instance[ 'listtype' ] == 'ulli'){echo 'selected="selected"'; } ?> value="ulli">List View </option>
				<option <?php if($instance[ 'listtype' ] == 'simple'){echo 'selected="selected"'; } ?> value="simple">Grid view</option>
			<label for="<?php echo $this->get_field_id( 'imgsize' ); ?>"><?php _e( 'Select Image Size' ); ?></label>    
			<select class="widefat" id="<?php echo $this->get_field_id( 'imgsize' ); ?>" name="<?php echo $this->get_field_name( 'imgsize' ); ?>">
				<option <?php if($instance[ 'imgsize' ] == 'thumbnail'){echo 'selected="selected"'; } ?> value="thumbnail">150X150 </option>
				<option <?php if($instance[ 'imgsize' ] == 'medium'){echo 'selected="selected"'; } ?> value="medium">300X300</option>
				<option <?php if($instance[ 'imgsize' ] == 'large'){echo 'selected="selected"'; } ?> value="large">640x640</option>
				<option <?php if($instance[ 'imgsize' ] == 'full'){echo 'selected="selected"'; } ?> value="full">Full</option>
	// Updating widget replacing old instances with new
	public function update( $new_instance, $old_instance ) 
		$instance = array();
		$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
		$instance['slug'] = ( ! empty( $new_instance['slug'] ) ) ? strip_tags( $new_instance['slug'] ) : '';
		$instance['perpage'] = ( ! empty( $new_instance['perpage'] ) ) ? strip_tags( $new_instance['perpage'] ) : '';
		$instance['listtype'] = ( ! empty( $new_instance['listtype'] ) ) ? strip_tags( $new_instance['listtype'] ) : '';
		$instance['imgsize'] = ( ! empty( $new_instance['imgsize'] ) ) ? strip_tags( $new_instance['imgsize'] ) : '';
		return $instance;
} // Class cpt_widget ends here
// Register and load the widget
function cpt_load_widget() {
	register_widget( 'cpt_widget' );
add_action( 'widgets_init', 'cpt_load_widget' );



2 thoughts on “Building WordPress Plugin Development

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s