Web Dev Tuts

/ Stuffs About Website Development /

Menu
  • Home
  • Wp Plugins
  • How To
  • FAQ
    • GIT
    • JQuery
    • PHP
    • WooCommerce
    • WordPress
  • Contact Us
Menu

Plugin Class

Posted on July 31, 2016June 24, 2018 by admin

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

Category: My Notes

Leave a Reply Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to add wp_editor Dynamically
  • How to Hide Certain Pages from Certain Users (WP Dashboard)
  • How to Set Up Gmail SMTP
  • How to Set Up Virtual Host in Localhost & Xampp
  • How to Determine the Current WordPress Version Used
  • AngularJs
  • CSS
  • For Me Only
  • geo location api
  • GIT
  • GoDaddy
  • Hosting
  • How To
  • Javascript
  • JQuery
  • Magento
  • My Notes
  • PHP
  • Private
  • Sass
  • WooCommerce
  • Wordpress
  • Xampp
© 2023 Web Dev Tuts | Powered by Minimalist Blog WordPress Theme