menu

Sunday, 18 February 2018

How to create plugin in wordpress

How to create plugin in wordpress this very easy if you have some knowledge og php language. Now in this tutorial, you’ll learn how to create a complete WordPress plugin. The main reason to create a plugin is that it allows you to separate your code from the core code of WordPress. If anything will be wrong in your plugin, the rest of the site will run continue with same functionalty.

Step1. First of all create a folder and then create a single file with one line of content.
Folder Location  /wp-content/plugins/awtplugin
Create a file named awtplugin.php. Open the file in a text editor, and paste the following information.
<?php
   /*
   Plugin Name: Advanced Web Tutorial
   Plugin URI: http://advancedwebtutorial.com
   description: Create AWT and spread joy
   Version: 1.1
   Author: Mr. Atul Sharma
   Author URI: https://plus.google.com/u/0/112051757095691606029
   License: GPL2
   */
?>
Only the plugin’s name is required. But if you intend to distribute your plugin, 
you should add as much data as possible. 
 
WordPress offers three great hooks to help you take care of this:
  • register_activation_hook() This hook allows you to create a function that runs when your plugin is activated. It takes the path to your main plugin file as the first argument, and the function that you want to run as the second argument. You can use this to check the version of your plugin, do some upgrades between versions, check for the correct PHP version and so on.
  • register_deactivation_hook() The name says it all. This function works like its counterpart above, but it runs whenever your plugin is deactivated. I suggest using the next function when deleting data; use this one just for general housekeeping.
  • register_uninstall_hook() This function runs when the website administrator deletes your plugin in WordPress’ back end. This is a great way to remove data that has been lying around, such as
Step 2 :

/**
 * Adds a view to the post being viewed
 *
 * Finds the current views of a post and adds one to it by updating
 * the postmeta. The meta key used is "awtpop_views".
 *
 * @global object $post The post object
 * @return integer $new_views The number of views the post has
 *
 */
function awtpop_add_view() {
   if(is_single()) {
      global $post;
      $current_views = get_post_meta($post->ID, "awtpop_views", true);
      if(!isset($current_views) OR empty($current_views) OR !is_numeric($current_views) ) {
         $current_views = 0;
      }
      $new_views = $current_views + 1;
      update_post_meta($post->ID, "awtpop_views", $new_views);
      return $new_views;
   }
}

add_action("wp_head", "awtpop_add_view");

 /**
 * Retrieve the number of views for a post
 *
 * Finds the current views for a post, returning 0 if there are none
 *
 * @global object $post The post object
 * @return integer $current_views The number of views the post has
 *
 */
function awtpop_get_view_count() {
   global $post;
   $current_views = get_post_meta($post->ID, "awtpop_views", true);
   if(!isset($current_views) OR empty($current_views) OR !is_numeric($current_views) ) {
      $current_views = 0;
   }

   return $current_views;
}

/**
 * Displays a list of posts ordered by popularity
 *
 * Shows a simple list of post titles ordered by their view count
 *
 * @param integer $post_count The number of posts to show
 *
 */
 function awtpop_popularity_list($post_count = 10) {
  $args = array(
    "posts_per_page" => 10,
    "post_type" => "post",
    "post_status" => "publish",
    "meta_key" => "awtpop_views",
    "orderby" => "meta_value_num",
    "order" => "DESC"
  );

  $awtpop_list = new WP_Query($args);

  if($awtpop_list->have_posts()) { echo "<ul>"; }

  while ( $awtpop_list->have_posts() ) : $awtpop_list->the_post();
    echo '<li><a href="'.get_permalink($post->ID).'">'.the_title('', '', false).'</a></li>';
  endwhile;

  if($awtpop_list->have_posts()) { echo "</ul>";}
 }