{"id":3333,"date":"2020-09-19T11:00:24","date_gmt":"2020-09-19T10:00:24","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=3333"},"modified":"2022-02-17T07:13:47","modified_gmt":"2022-02-17T07:13:47","slug":"plugin-class","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/wordpress\/plugins\/create-your-own-plugin\/plugin-class","title":{"rendered":"Plugin class"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Creating a plugin inside a class<\/h4>\n\n\n\n<p>By using a class, everything inside the class does not need globally unique naming prefixes, solving clashing issues with other plugins, themes and wordpress itself.<\/p>\n\n\n\n<p><strong><em>BUT, in PHP a class has to be contained in a single file, you can&#8217;t split a class across multiple files.<\/em><\/strong>  You can create new classes in each of your additional files and have them extend your main class, but there&#8217;s a complexity overhead.<\/p>\n\n\n\n<p>You also can&#8217;t include a file inside a class definition.  You can include a file inside a class function, but that doesn&#8217;t achieve a class wide include.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">An example plugin class<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/**************************************\n\/\/**************************************\n\/\/********** OUR PLUGIN CLASS **********\n\/\/**************************************\n\/\/**************************************\n\/\/(By using a class, everything inside the class does not need globally unique naming prefixes, solving clashing issues with other plugins, themes and wordpress itself)\nclass MyPluginClassUniqueName\n{\n\n  \/\/*********************************\n  \/\/*********************************\n  \/\/********** CONSTRUCTOR **********\n  \/\/*********************************\n  \/\/*********************************\n  public function __construct()\n  {\n\n    \/\/----- ADD JAVASCRIPT AND CSS FOR ADMIN SCREENS -----\n    add_action('admin_enqueue_scripts', array($this,'OurEnqueueScriptsAdmin'));       \/\/Array used with $this as there isn't an instantiated object of the class yet when the server hits this code\n\n    \/\/----- ADD JAVASCRIPT AND CSS FOR FRONT-END DISPLAY -----\n    add_action('wp_enqueue_scripts', array($this,'OurEnqueueScripts'));       \/\/Array used with $this as there isn't an instantiated object of the class yet when the server hits this code\n    \n  }\n  \n  \/\/********************************************************\n  \/\/********************************************************\n  \/\/********** ENQUEUE SCRIPTS AND STYLES - ADMIN **********\n  \/\/********************************************************\n  \/\/********************************************************\n  \/\/ This is an example of enqueuing a Javascript file and a CSS file for use on the editor \n  public function OurEnqueueScriptsAdmin()\n  {\n\n    $Screen = get_current_screen();\n    \/\/if (($Screen->base == 'post') &amp;&amp; ($Screen->post_type == 'my_custom_post_type'))      \/\/Only load the files on the relevant screen, e.g. the editor for a \"my_custom_post_type\" custom post type\n    \/\/{\n    \/\/  wp_enqueue_script('my-plugin-unique-name-my-js-file', plugins_url('js\/my-js-file.js', __FILE__), array('jquery'), '1.0', true);   \/\/Make sure you are including the relevant dependencies (e.g. jquery)\n    \/\/  wp_enqueue_style('my-plugin-unique-name-my-css-file', plugins_url('css\/my-css-file.css', __FILE__), null, '1.0');\n    \/\/}\n  }\n\n  \/\/************************************************\n  \/\/************************************************\n  \/\/********** ENQUEUE SCRIPTS AND STYLES **********\n  \/\/************************************************\n  \/\/************************************************\n  \/\/ This is an example of enqueuing a JavaScript file and a CSS file for use on the front end display\n  public function OurEnqueueScripts()\n  {\n    \/\/wp_enqueue_script('my-plugin-unique-name-my-js-file', plugins_url('js\/my-js-file.js', __FILE__), array('jquery'), '1.0', true);   \/\/Make sure you are including the relevant dependencies (e.g. jquery)\n    \/\/wp_enqueue_style('my-plugin-unique-name-my-css-file', plugins_url('css\/my-css-file.css', __FILE__), null, '1.0');\n  }\n  \n  \n} \/\/class MyPluginClassUniqueName\n\n\/\/global $MyPluginClassUniqueName;              \/\/&lt;&lt;Only needed if your want the plugins to be instantiated globally (referenced elsewhere)\n$MyPluginClassUniqueName = new MyPluginClassUniqueName();      \/\/Create an instance of our plugin class\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Default visibility<\/h4>\n\n\n\n<p>If you omit public, private or protected or a function, &#8216;public&#8217; is the default.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating a plugin inside a class By using a class, everything inside the class does not need globally unique naming prefixes, solving clashing issues with other plugins, themes and wordpress itself. BUT, in PHP a class has to be contained in a single file, you can&#8217;t split a class across multiple files. You can create [&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-3333","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\/3333","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=3333"}],"version-history":[{"count":4,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3333\/revisions"}],"predecessor-version":[{"id":3337,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3333\/revisions\/3337"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=3333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=3333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=3333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}