Creating Your First Plugin Using an External API

@ericnkatz

GET Started


Plugin Structure

 <?php
/**
 * Plugin Name: Name Of The Plugin
 * Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
 * 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://URI_Of_The_Plugin_Author
 * License: A "Slug" license name e.g. GPL2
 */


Actions & Filters

  <?php 
  //do something when.... wp_head runs
  add_action('wp_head','plugin_hook_head');
  function plugin_hook_head()
  {
      $output="<script>alert('BOOM HEADSHOT!');</script>";
      
      echo $output;
  }
  ?>
  <?php 
  //when the_content runs... manipulate the output
  add_filter( 'the_content', 'plugin_filter_content' ); 
    
  function plugin_filter_content( $content ) {
      return $content . ' The End';
  }
  ?>

Actions

let us actively target when our function should run during the WordPress instantiation, these functions should be behavior based.

Filters

let us passively add functionality to existing WordPress functions whenever they run. These will take content and return content.

-VS-

External API

Application Programming Interface 

  In the context of web development, an API is typically defined as a set of Hypertext Transfer Protocol (HTTP) request messages, along with a definition of the structure of response messages, which is usually in an Extensible Markup Language (XML) or JavaScript Object Notation (JSON) format.

wikipedia.org/wiki/Application_programming_interface#Web_APIs




TLDR;

APIs are how different environments can send and retrieve information from one another.


And by using external APIs we can pull dynamic information and content from other sources into our WordPress sites!


Choose your api

Next steps is to get comfortable with the API you choose. Every API will have some documentation and important rules to follow. 

APIs may have rate limits.

APIs may  require OAUTH.

APIs may  require API Keys.

APIS may require ... etc.

APIGEE



Options Page

An Options page is a great place to store 
3rd Party API credentials.

<?php
    add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function);
?>

<?php
    add_action('admin_menu', 'amazon_products_menu');

    function amazon_products_menu() {
    
        add_options_page( 'Amazon Products', 'Amazon Products', 'manage_options', 'amazon-products', 'amazon_products_page');
    }

?>
<?php
    add_action('admin_menu', 'amazon_products_menu');

    function amazon_products_menu() {
    
        add_options_page( 'Amazon Products', 'Amazon Products', 'manage_options', 'amazon-products', '
amazon_products_page
');
    }

?>
 <?php
    function amazon_products_page() { ?>
        <div class="wrap">           <h2>Amazon Products Options</h2>            <p>Yay!!</p>        </div>    <?php    }

OPTIONS PAGE

Settings API









<?php 
    function amazon_products_page() { ?>
      <div class="wrap">
        <h2>Amazon Products Options</h2>
        <form method="POST" action="options.php">
           <?php 
           settings_fields( 'amazon_product_options' );
           do_settings_sections( 'amazon-products' );
           submit_button();
           ?>
		</form>
    </div>
<?php
}
<?php 
    function amazon_products_page() { ?>
      <div class="wrap">
        <h2>Amazon Products Options</h2>
        <form method="POST" action="options.php">
           <?php 
           settings_fields( 'amazon_product_options' );
           do_settings_sections( 'amazon-products' );
           submit_button();
           ?>
		</form>
    </div>
<?php
}
<?php 
    function amazon_products_page() { ?>
      <div class="wrap">
        <h2>Amazon Products Options</h2>
        <form method="POST" action="options.php">
           <?php 
           settings_fields( 'amazon_product_options' );
           do_settings_sections( 'amazon-products' );
           submit_button();
           ?>
		</form>
    </div>
<?php
}
<?php

add_action( 'admin_init', 'amazon_product_settings' );

function amazon_product_settings() {
    register_setting( 'amazon_product_options', 'amazon_product_country' ); 
    add_settings_section( 'amazon_required_section', 'Amazon Settings', 'amazon_section_text_callback', 'amazon-products' );
    add_settings_field( 'amazon_fields', 'Country', 'amazon_fields_callback', 'amazon-products', 'amazon_required_section' );

}  




API WRAPPERS ARE AWESOME


PHP API Wrappers  let us manipulate and interact with an external API by adding an abstraction layer that is easy to use.

If you plan on using a popular API, look around and see if anyone has already created and shared an API wrapper on Github, Bitbucket, etc.

 

Your First WordPress Plugin and Using an External API

By Eric Katz

Your First WordPress Plugin and Using an External API

Introduction to WordPress Plugin API and live coding implementation of connecting to an external API service (Amazon Products API).

  • 2,750