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’t split a class across multiple files. You can create new classes in each of your additional files and have them extend your main class, but there’s a complexity overhead.

You also can’t include a file inside a class definition. You can include a file inside a class function, but that doesn’t achieve a class wide include.

An example plugin class


//**************************************
//**************************************
//********** OUR PLUGIN 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)
class MyPluginClassUniqueName
{

  //*********************************
  //*********************************
  //********** CONSTRUCTOR **********
  //*********************************
  //*********************************
  public function __construct()
  {

    //----- ADD JAVASCRIPT AND CSS FOR ADMIN SCREENS -----
    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

    //----- ADD JAVASCRIPT AND CSS FOR FRONT-END DISPLAY -----
    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
    
  }
  
  //********************************************************
  //********************************************************
  //********** ENQUEUE SCRIPTS AND STYLES - ADMIN **********
  //********************************************************
  //********************************************************
  // This is an example of enqueuing a Javascript file and a CSS file for use on the editor 
  public function OurEnqueueScriptsAdmin()
  {

    $Screen = get_current_screen();
    //if (($Screen->base == 'post') && ($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
    //{
    //  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)
    //  wp_enqueue_style('my-plugin-unique-name-my-css-file', plugins_url('css/my-css-file.css', __FILE__), null, '1.0');
    //}
  }

  //************************************************
  //************************************************
  //********** ENQUEUE SCRIPTS AND STYLES **********
  //************************************************
  //************************************************
  // This is an example of enqueuing a JavaScript file and a CSS file for use on the front end display
  public function OurEnqueueScripts()
  {
    //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)
    //wp_enqueue_style('my-plugin-unique-name-my-css-file', plugins_url('css/my-css-file.css', __FILE__), null, '1.0');
  }
  
  
} //class MyPluginClassUniqueName

//global $MyPluginClassUniqueName;              //<<Only needed if your want the plugins to be instantiated globally (referenced elsewhere)
$MyPluginClassUniqueName = new MyPluginClassUniqueName();      //Create an instance of our plugin class

Default visibility

If you omit public, private or protected or a function, ‘public’ is the default.

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *