How to Create WordPress Plugin

  1. WordPress folder structure
  2. How to create a blank plugin
  3. How to create a static plugin
  4. How to create simple dynamic plugin passing URL parameters
  5. How to create complex plugin passing parameters through the WP shortcode array

1. WordPress folder structure

WordPress folder structure seems complicated. The good news is the ONLY folder you want to work on is the [wp-content] folder. Under that folder, [plugins] & [themes] folders are the only folders you want to touch to minimize potential issues.

2. How to create a blank plugin

  1. Under [plugins] folder create a folder for your blank plugin. This will be the name of your plugin
  2. Inside the plugin folder, you just created, create a PHP file with the exact same name. For example, if you created a folder named [testPlugin], you should create testPlugin.php inside that folder.
  3. Open up an editor and copy PHP code below and save.
    <?php
    /**
    *Plugin Name: Write JSON
    *Description: Writing JSON plugin
    **/
    ?>
  4. Open up your WP-Admin page (localhost/wordpress/wp-admin/) and click [Plugins] menu. You will see the blank plugin named [testPlugin] listed. You can activate it.

3. How to create a static plugin

  1. Open testPlugin.php file in the editor.
  2. A static plugin is a plugin do not accept any parameters.
  3. If you copy and paste below code block to testPlugin.php file, this plugin will write HTML code assigned in $str variable.
    
    <?php
    /**
    *Plugin Name: test plugin
    *Description: Writing test plugin
    addddadsa**/
    function writePoem()
    {
    $str = "Go placidly amid the noise and the haste, and remember what peace there may be in silence. <be>As far as possible, without surrender, be on good terms with all persons.";
    return $str;
    }
    add_shortcode('piPoem','writePoem')
    ?>
  4. The way you use this plugin is simple. In an editor (classic or Gutenberg), just enter [testPlugin] shortcode anywhere in the editor, and the poem will display.

4. How to create a simple dynamic plugin passing URL parameters

  1. Open testPlugin.php file in the editor.
  2. This plugin will add 2 number variables passed through URL parameters.
  3. If you copy and paste below code block to testPlugin.php file, this plugin will add the 2 numbers.
  4. 
    <?php
    /**
    *Plugin Name: test plugin
    *Description: Writing test plugin
    **/
    function addNum()
    {
    $num1 = $_GET['num1'];
    $num2 = $_GET['num2'];
    return $num1 + $num2;
    }
    add_shortcode('piAdd,'addNum')
    ?>
  5. To use this plugin, paste &num1=3&num2=4 at the end of the page you want to use this plugin
  6. In the editor (classic or Gutenberg), just enter [testPlugin] shortcode anywhere in the editor, and the 2 numbers passed through the variables will be added.

4. How to create complex plugin passing parameters through the WP shortcode array

  1. Open testPlugin.php file in the editor.
  2. This plugin will display BC cafeteria JSON menu and JSON parameters passed through shortcode parameters.
  3. If you copy and paste below code block to testPlugin.php file, this plugin will display JSON data.
  4. 
    <?php
    /**
    *Plugin Name: Write JSON
    *Description: Writing JSON plugin
    **/
    function writeJson($attr)
    {
    $args = shortcode_atts( array(
    'jsonis' => ''
    ), $attr);
    $json=file_get_contents($args['jsonis']);
    $ojb = json_decode($json, TRUE);
    $str = "";
    $loc = "";
    foreach($ojb as $key => $value)
    {
    if ($loc <> $value['Location_Name'])
    {
    $str .= "<b>".$value['Location_Name']."</b><br>";
    }
    $str .= $value['Ingredient_List']."<p>";
    $loc = $value['Location_Name'];
    }return $str;
    }add_shortcode('piJson','writeJson')
    ?>
  5. In the editor (classic or Gutenberg), just enter [piJson jsonis=’https://web.bc.edu/dining/menu/todayMenu_PROD.json’] shortcode anywhere in the editor, and the 2 parameters and the plugin will display the JSON data.

0 comments on “How to Create WordPress PluginAdd yours →

Leave a Reply

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