{"id":108,"date":"2020-11-09T03:42:20","date_gmt":"2020-11-09T03:42:20","guid":{"rendered":"http:\/\/sites.bc.edu\/sonn\/?p=108"},"modified":"2023-03-27T15:57:37","modified_gmt":"2023-03-27T15:57:37","slug":"how-to-create-wordpress-plugin","status":"publish","type":"post","link":"https:\/\/sites.bc.edu\/sonn\/2020\/11\/09\/how-to-create-wordpress-plugin\/","title":{"rendered":"How to Create WordPress Plugin"},"content":{"rendered":"\n<ol class=\"wp-block-list\">\n<li>WordPress folder structure<\/li>\n\n\n\n<li>How to create a blank plugin<\/li>\n\n\n\n<li>How to create a static plugin<\/li>\n\n\n\n<li>How to create simple dynamic plugin passing URL parameters<\/li>\n\n\n\n<li>How to create complex plugin passing parameters through the WP shortcode array<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">1. WordPress folder structure<\/h4>\n\n\n\n<p>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] &amp; [themes] folders are the only folders you want to touch to minimize potential issues.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. How to create a blank plugin<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Under [plugins] folder create a folder for your blank plugin. This will be the name of your plugin<\/li>\n\n\n\n<li>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.<\/li>\n\n\n\n<li>\nOpen up an editor and copy PHP code below and save.\n<pre class=\"wp-block-code\"><code>&lt;?php<br>\/**<br>*Plugin Name: Write JSON<br>*Description: Writing JSON plugin<br>**\/<br>?&gt;\n<\/code><\/pre><\/li>\n\n\n\n<li>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.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">3. How to create a static plugin<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open testPlugin.php file in the editor.<\/li>\n\n\n\n<li>A static plugin is a plugin do not accept any parameters.<\/li>\n\n\n\n<li>If you copy and paste below code block to testPlugin.php file, this plugin will write HTML code assigned in $str variable.\n<pre class=\"wp-block-code\"><code>\n&lt;?php<br>\/**<br>*Plugin Name: test plugin<br>*Description: Writing test plugin<br>addddadsa**\/<br>function writePoem()<br>{<br>$str = \"<span class=\"bold\">Go placidly <\/span>amid the noise and the haste, and remember what peace there may be in silence. &lt;be&gt;As far as possible, without surrender, be on good terms with all persons.\";<br>return $str;<br>}<br>add_shortcode('piPoem','writePoem')<br>?&gt;\n<\/code><\/pre>\n<\/li>\n\n\n\n<li>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.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">4. How to create a simple dynamic plugin passing URL parameters<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open testPlugin.php file in the editor.<\/li>\n\n\n\n<li>This plugin will add 2 number variables passed through URL parameters.<\/li>\n\n\n\n<li>If you copy and paste below code block to testPlugin.php file, this plugin will add the 2 numbers.<\/li>\n\n\n\n<li>\n<pre class=\"wp-block-code\"><code>\n&lt;?php<br>\/**<br>*Plugin Name: test plugin<br>*Description: Writing test plugin<br>**\/<br>function addNum()<br>{<br>$num1 = $_GET['num1'];<br>$num2 = $_GET['num2'];<br>return $num1 + $num2;<br>}<br>add_shortcode('piAdd,'addNum')<br>?&gt;\n<\/code><\/pre>\n<\/li>\n\n\n\n<li>To use this plugin, paste &amp;num1=3&amp;num2=4 at the end of the page you want to use this plugin<\/li>\n\n\n\n<li>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.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">4. How to create complex plugin passing parameters through the WP shortcode array<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open testPlugin.php file in the editor.<\/li>\n\n\n\n<li>This plugin will display BC cafeteria JSON menu and JSON parameters passed through shortcode parameters.<\/li>\n\n\n\n<li>If you copy and paste below code block to testPlugin.php file, this plugin will display JSON data.<\/li>\n\n\n\n<li>\n<pre class=\"wp-block-code\"><code>\n&lt;?php<br>\/**<br>*Plugin Name: Write JSON<br>*Description: Writing JSON plugin<br>**\/<br>function writeJson($attr)<br>{<br>$args = shortcode_atts( array(<br>'jsonis' =&gt; ''<br>), $attr);<br>$json=file_get_contents($args['jsonis']);<br>$ojb = json_decode($json, TRUE);<br>$str = \"\";<br>$loc = \"\";<br>foreach($ojb as $key =&gt; $value)<br>{<br>if ($loc &lt;&gt; $value['Location_Name'])<br>{<br>$str .= \"&lt;b&gt;\".$value['Location_Name'].\"&lt;\/b&gt;&lt;br&gt;\";<br>}<br>$str .= $value['Ingredient_List'].\"&lt;p&gt;\";<br>$loc = $value['Location_Name'];<br>}return $str;<br>}add_shortcode('piJson','writeJson')<br>?&gt;\n<\/code><\/pre>\n<\/li>\n\n\n\n<li>In the editor (classic or Gutenberg), just enter [piJson jsonis=&#8217;https:\/\/web.bc.edu\/dining\/menu\/todayMenu_PROD.json&#8217;] shortcode anywhere in the editor, and the 2 parameters and the plugin will display the JSON data.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<div class=\"entry-summary\">\n1. WordPress folder structure WordPress folder structure seems complicated. The good news&hellip;\n<\/div>\n<div class=\"link-more\"><a href=\"https:\/\/sites.bc.edu\/sonn\/2020\/11\/09\/how-to-create-wordpress-plugin\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &ldquo;How to Create WordPress Plugin&rdquo;<\/span>&hellip;<\/a><\/div>\n","protected":false},"author":131979,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-108","post","type-post","status-publish","format-standard","hentry","category-coding","entry"],"_links":{"self":[{"href":"https:\/\/sites.bc.edu\/sonn\/wp-json\/wp\/v2\/posts\/108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sites.bc.edu\/sonn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sites.bc.edu\/sonn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sites.bc.edu\/sonn\/wp-json\/wp\/v2\/users\/131979"}],"replies":[{"embeddable":true,"href":"https:\/\/sites.bc.edu\/sonn\/wp-json\/wp\/v2\/comments?post=108"}],"version-history":[{"count":8,"href":"https:\/\/sites.bc.edu\/sonn\/wp-json\/wp\/v2\/posts\/108\/revisions"}],"predecessor-version":[{"id":275,"href":"https:\/\/sites.bc.edu\/sonn\/wp-json\/wp\/v2\/posts\/108\/revisions\/275"}],"wp:attachment":[{"href":"https:\/\/sites.bc.edu\/sonn\/wp-json\/wp\/v2\/media?parent=108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sites.bc.edu\/sonn\/wp-json\/wp\/v2\/categories?post=108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sites.bc.edu\/sonn\/wp-json\/wp\/v2\/tags?post=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}