Blog

WordPress Tutorial : Plugin Basics

March 19, 2019 by admin

In this tutorial we we learn basics of Writing a wordpress plugin.

A WordPress plugin is a PHP file with a WordPress plugin header comment. It’s highly recommended that you create a directory to hold your plugin so that all of your plugin’s files are neatly organised in one place.

In this article we will discuss in detail with following steps:

Step 1:

1.First you will need to install WordPress, you can follow wordpress installation tutorial here.

2.After wordpress installation. Goto to the WordPress installation’s wp-content directory.

3.Open the plugins directory.

4.Create a new directory and name it after the plugin (e.g. plugin-name).

5.Create a new PHP file (it’s also good to name this file after your plugin, e.g. plugin-name.php).

6.Open the File to Edit and write this code in header and save the File.

       

6.Open the File to Edit and write this code in header and save the File.

7.Now goto Plugins form left menu. This screen will list number of plugins installed. find "GBS DEMO PLUGIN" and activate.

You can also include further information in plugin header

       

What is: Hooks

In WordPress theme and development, Hooks are functions that can be applied to an Action or a Filter in WordPress. Actions and Filters in WordPress are functions that can be modified by theme and plugin developers to change the default WordPress functionality.

Functions used to modify Actions/Filters in WordPress can be hooked into WordPress. However, it is important to note that actions and filters are not the same thing. Now we will discuss and differentiate in detail WordPress Actions and filters.

Filters

Filters are functions that WordPress uses to pass data through. Passing data through filters allows developers to modify the default behavior of a specific function. Functions used to filter data are called hooks. Filters and Actions together allow developers great flexibility to modify default WordPress filters and actions and even create their own custom filters and actions so that other developers can extend their plugins or themes.

Filters are different than Actions. WordPress actions are executed at events like when a theme or plugin is activated, or when a post is published. Filters are used to filter output when it is sent to either database or to user browser.

Example of using a WordPress filter

       
function wpb_custom_excerpt( $output ) {
  if ( has_excerpt() && ! is_attachment() ) {
    $output .= wpb_continue_reading_link();
  }
  return $output;
}
add_filter( 'get_the_excerpt', 'wpb_custom_excerpt' );

The sample code above adds a function wpb_custom_excerpt to the filter get_the_excerpt.

Actions

An action is a function in WordPress code that is run at certain points throughout the WordPress core. In WordPress code there are numerous pre-defined actions or hooks that allow developers to add their own code at these points. These are a part of what makes WordPress so extensible and most plugins depend on actions for their operation.

o give you an example of how they can be used to add code to a website let us assume that you want to add a copyright notice to your footer. In order to do this you could modify your footer template directly. In some cases this would be preferable but many times it is much easier, and better practice, to hook your code to a predefined action that is already being executed in the footer. To do this you can add your copyright code into a function in your functions.php file. You can then add this function to an action that is in the spot where you would like your copyright code to be executed.

       
function copyright_notice() {
   echo "Copyright All Rights Reserved";
}
add_action('wp_footer','copyright_notice');

In this example, copyright_notice is an action hooked into wp_footer hook. The function copyright_notice will be executed whenever the wp_footer() hook appears in a WordPress theme code.

Leave a Reply

Your email address will not be published. Required fields are marked *