- WordPress folder structure
- How to create a blank plugin
- How to create a static plugin
- How to create simple dynamic plugin passing URL parameters
- 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
- Under [plugins] folder create a folder for your blank plugin. This will be the name of your plugin
- 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.
-
Open up an editor and copy PHP code below and save.
<?php
/**
*Plugin Name: Write JSON
*Description: Writing JSON plugin
**/
?> - 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
- Open testPlugin.php file in the editor.
- A static plugin is a plugin do not accept any parameters.
- 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')
?> - 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
- Open testPlugin.php file in the editor.
- This plugin will add 2 number variables passed through URL parameters.
- If you copy and paste below code block to testPlugin.php file, this plugin will add the 2 numbers.
-
<?php
/**
*Plugin Name: test plugin
*Description: Writing test plugin
**/
function addNum()
{
$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
return $num1 + $num2;
}
add_shortcode('piAdd,'addNum')
?> - To use this plugin, paste &num1=3&num2=4 at the end of the page you want to use this plugin
- 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
- Open testPlugin.php file in the editor.
- This plugin will display BC cafeteria JSON menu and JSON parameters passed through shortcode parameters.
- If you copy and paste below code block to testPlugin.php file, this plugin will display JSON data.
-
<?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')
?> - 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 Plugin” Add yours →