Since I know that I will be making more plugins for the rest of my life, I decided to create a class that does the basic task that most of my plugins need, namely: adding scripts and styles to the dashboard pates and/or website pages.

<?php

/**
 * Plugin Name: __plugin_name__ 
 * Description: __plugin_description__ 
 * Author: EdesaC 
 * Version: 1.0.0 
 **/ 

class PluginClass {
	protected $_plugin_url; 
	protected $_plugin_folder; 
	
	public function __construct($file) { 
		if (!$file) { 
			throw new Error('Missing 1 argument'); 
		}
		
		$this->_plugin_url = plugin_dir_url($file);
		$this->_plugin_folder = basename(dirname($file));

		add_action('admin_enqueue_scripts', array($this, 'addAdminScripts'));
		add_action('wp_footer', array($this, 'addWebsiteScripts'));
	}

	/**
	* adds scripts and css to website pages
	* @action wp_footer
	*/
	function addWebsiteScripts() {
		wp_enqueue_script($this->_plugin_folder . '-wp-scripts', $this->_plugin_url . '/scripts/wp-scripts.js');
		wp_enqueue_style($this->_plugin_folder . '-wp-styles', $this->_plugin_url . '/styles/wp-styles.css');
	}

	/**
	* adds scripts and css to admin pages
	* @action admin_enqueue_scripts
	*/
	function addAdminScripts() {
		wp_enqueue_media();
		wp_enqueue_script('jquery');
		wp_enqueue_script($this->_plugin_folder . '-admin-scripts', $this->_plugin_url . 'scripts/admin-scripts.js');
		wp_enqueue_style($this->_plugin_folder . '-admin-styles', $this->_plugin_url . 'styles/admin-styles.css');
	}
} // end of class

Copy the Plugin Class from WDT-Framework

Leave a Reply