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.

How to Setup WordPress in your Local Machine

In order to set up WordPress in your local machine, you need

  1. Install WAMP (Windows) or MAMP (Mac) to install Apache, MySQL and PHP in your local machine
  2. Using PhpMyAdmin, create a database for WordPress 
  3. Download & copy the latest WordPress to WWW root folder, and start WordPress installation

1. Install WAMP (Windows) or MAMP (Mac) to install Apache, MySQL, and PHP on your local machine

Download WAMP (Windows) https://www.wampserver.com/en/download-wampserver-64bits/ 
or MAMP (Mac) https://www.mamp.info/en/downloads/
These 2 downloads will install Apache, MySQL, and PHP. When downloading MAMP, please remember not to start and using MAMP Pro, which is a paid version. WAMP 

2. Using PhpMyAdmin, create a database for WordPress 

Once MAMP or WAMP is downloaded and installed, start PhpMyAdmin
      

Create a database named wordpress, and set up a new user account with [Grant all privileges on database wordperss database]. Keep database name, user name, password handy for WP installation.

     

3. Download & copy the latest WordPress to WWW root folder, and start WordPress installation

In Mac WWW root is Applications > MAMP > htdocs
In Windows c:\WAMP64\www\
Create a folder under the www root folder called [Wordpress] and copy downloaded WP files from https://wordpress.org/download/

Once WP files are in your local WWW root, start installing WP by 
localhost/wordpress/ and follow short instruction to enter MySQL database and enter credentials.

For more information on WP installation, 
https://wordpress.org/support/article/how-to-install-wordpress/

 

 

 

 

 

 

Usage of server-side programming languages for websites

Source: https://w3techs.com/technologies/overview/programming_language
 
This diagram shows the percentages of websites using various server-side programming languages. See technologies overview for explanations on the methodologies used in the surveys. Our reports are updated daily.

How to read the diagram:
PHP is used by 79.1% of all the websites whose server-side programming language we know.

PHP
 
79.1%
ASP.NET
 
10.6%
Java
 
3.5%
Ruby
 
3.0%
static files
 
1.8%
Scala
 
1.6%
Python
 
1.3%
JavaScript
 
0.8%
ColdFusion
 
0.4%
Perl
 
0.2%
Erlang
 
0.1%
  W3Techs.com, 17 January 2020
Percentages of websites using various server-side programming languages
Note: a website may use more than one server-side programming language

The following server-side programming languages are used by less than 0.1% of the websites