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>";}
 }


 

Saturday 10 February 2018

how to get all share link of dropbox folder

Dropbox

Request and response formats

In general, the Dropbox API uses HTTP POST requests with JSON arguments and JSON responses. Request authentication is via OAuth 2.0 using the Authorization request header or authorization URL parameter.


/*// Step 1. For Call HTTP Autherization .
https://www.dropbox.com/oauth2/authorize?client_id=XXXXXXXXX&response_type=token&state=ahometech&require_role=personal&disable_signup=false&force_reauthentication=false&redirect_uri=http://localhost/mydropbox/xyz.php

After that you will get this responce
http://localhost/mydropbox/list.php#access_token=wGRn6y-Q5kAAAAAAAAAAKB3okg7UNewre-UAjwPOu2opZNv1NXQp-N9dZK_032B_&token_type=bearer&state=ahometech&uid=729287204&account_id=dbid%3AAAAQnPFMxiNicQYa-1fVYKYp9HgwKa-JZOY

// Step 2. This local Path is set in my API http://localhost/mydropbox/xyz.php
// Step 3.  You can use according to your need */




// List contents of home directory

    if(isset($_GET["access_token"])&& !empty($_GET["access_token"])){

    $auth_token = $_GET["access_token"];
    $root_path = '/math/';
    // create curl resource
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_URL, "https://api.dropboxapi.com/2/files/list_folder");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $auth_token, 'Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('path'=> $root_path,'recursive'=> true,'include_media_info'=> false,'include_deleted'=> false,'include_has_explicit_shared_members'=> false,'include_mounted_folders'=> true)));
    $result = curl_exec($ch);
    //print_r($result);
    $array = json_decode(trim($result), TRUE);
    //echo "<pre>";print_r($array);exit;
    curl_close($ch);
    }