Categories
php Plugin wordpress

WordPress Basic Plugin

Last updated on April 9th, 2021 at 09:25

Below is a very simple working example of a plugin. Create a directory in your plugins directory, create a php file and copy the below into it.

Then from the wordpress plugins menu simply activate it.

Just know that this plugin does absolutely nothing other than to show the basic setup, including public and protected variables.

<?php
/*
	Plugin Name: Plugin Demo
	Plugin URI: https://southcoasthosting.com/
	Description: Just a plugin demo to show how to set up the class and add an admin menu item
	Author: Gavin Simpson
	Version: 1.0
	Author URI: https://southcoasthosting.com
*/

class plugindemo
{
	protected $namespace;
	public $posttype="plugindemo";
	public static function init()
	{
		$class = __CLASS__;
		new $class;
	}
	public function __construct()
	{
		$this->namespace=test;
		add_action( 'admin_menu', array($this,'my_plugin_menu'),10,0);
	}
	public function test_function($namespace)
	{
		$this->namespace=$namespace;
	}
	function my_plugin_menu()
	{
		
		if (current_user_can('administrator'))
		{
			add_menu_page( 'SCHS Menu', 'SCHS Menu', 'manage_options', 'schs-menu', array($this,'my_plugin_menu_page_callback'),null,1);
			add_submenu_page( 'schs-menu', 'SCHS Submenu', 'SCHS Submenu', 'manage_options', 'schs-submenu', array($this,'schs_submenu_page_callback'));
		}
	}
	function my_plugin_menu_page_callback()
	{
		echo "<div class='wrap'>";
		echo "<h1>".esc_html(get_admin_page_title())."</h1>";
		echo "</div>";
	}
	function schs_submenu_page_callback()
	{
		echo "<div class='wrap'>";
		echo "<h1>".esc_html(get_admin_page_title())."</h1>";
		echo "</div>";		
	}
}

add_action('init',array('plugindemo','init'));
?>