{"id":3312,"date":"2020-09-19T08:59:34","date_gmt":"2020-09-19T07:59:34","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=3312"},"modified":"2022-02-17T07:13:47","modified_gmt":"2022-02-17T07:13:47","slug":"create-new-plugin","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/wordpress\/plugins\/create-your-own-plugin\/create-new-plugin","title":{"rendered":".Create New Plugin"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">The basics<\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>Decide on a name for your plugin, this must be globally unique.<\/li><li>Create a folder in the wp-content\/plugins\/ directory using your plugin name.<br>Use lowercase and replace any spaces with hyphens.<\/li><li>Create a file in the folder with the exact same name as your plugin folder, with the &#8220;.php&#8221; extension.<br>This is the master file for your plugin (like the functions.php file for a child theme)<\/li><li>Paste the following into the .PHP file and edit as needed:<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/**\n* Plugin Name: My Plugin\n* Plugin URI: https:\/\/mywebsite.com\/\n* Description: My plugin description\n* Version: 1.0\n* Author: My Name\n* Author URI: http:\/\/mywebsite.com\/\n**\/<\/code><\/pre>\n\n\n\n<p>Upload it an you will see the plugin in your wordpress sites Plugins page.<br>Activate it.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Adding a stylesheet<\/h5>\n\n\n\n<ul class=\"wp-block-list\"><li>Add a file &#8220;css\/my-plugin-name-style.css&#8221; (in a subfolder folder names css).<br>To cause it to be used add this to the top of your main plugin php file: <\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/*************************************\n\/\/*************************************\n\/\/********** ENQUEUE SCRIPTS **********\n\/\/*************************************\n\/\/*************************************\nadd_action( 'wp_enqueue_scripts', 'odac_enqueue_scripts' );\nfunction odac_enqueue_scripts()\n{\n    wp_enqueue_style( 'my-plugin-name-style',  plugin_dir_url( __FILE__ ) . '\/css\/my-plugin-name-style.css' );                      \n}<\/code><\/pre>\n\n\n\n<p>(The first argument is the handle and must be unique, so just name it the same as your file).<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Adding php files<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>include( plugin_dir_path( __FILE__ ) . 'includes\/my-file-name.php' );<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Common Plugin Folder Structure<\/h4>\n\n\n\n<p>You can have whatever folders you want in our plugin, but the following is a fairly typical approach many developers use:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>&#8216;css&#8217; or &#8216;styles&#8217; for stylesheets<\/li><li>&#8216;scripts&#8217; for JavaScript<\/li><li>&#8216;includes&#8217; for include files<\/li><li>&#8216;templates&#8217; for template files that your plugin outputs<\/li><li>&#8216;assets&#8217; for media and other asset files<\/li><li>&#8216;i18n&#8217; for internationalisation files<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Naming within a plugin<\/h4>\n\n\n\n<p>By default, all variables, functions and classes are defined in the global namespace.  This is how your plugin is able to override variables, functions and classes set by another plugin and vice-versa.<\/p>\n\n\n\n<p>Variables that are defined inside of functions or classes are not affected by this.<\/p>\n\n\n\n<p>So to deal with this you must name all of your functions and global variables with a globally unique name, typically by using a prefix.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Name collision issues workarounds<\/h5>\n\n\n\n<p>Using a prefix for all of you function names can be a massive pain.  A simple solution is to use a <a href=\"\/wordpress\/plugins\/create-your-own-plugin\/plugin-namespace\">namespace for your plugin<\/a>.  <\/p>\n\n\n\n<p>Another more complex approach is to <a href=\"\/wordpress\/plugins\/create-your-own-plugin\/plugin-class\">use classes<\/a>, as everything inside a class is not part of the global namespace.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The basics Decide on a name for your plugin, this must be globally unique. Create a folder in the wp-content\/plugins\/ directory using your plugin name.Use lowercase and replace any spaces with hyphens. Create a file in the folder with the exact same name as your plugin folder, with the &#8220;.php&#8221; extension.This is the master file [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[251],"tags":[],"class_list":["post-3312","post","type-post","status-publish","format-standard","hentry","category-create-your-own-plugin"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3312","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=3312"}],"version-history":[{"count":16,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3312\/revisions"}],"predecessor-version":[{"id":3348,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3312\/revisions\/3348"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=3312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=3312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=3312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}