Lesson 3 – The MainWP Development Add-on – Widgets
Widgets
class-mainwp-development-widget.php
What’s this Class for?
This class is where all the UI code & any form handlers will go for the Add-on widgts.
Namespaces
Each PHP file with a MainWP add-on will need a unique namespace added to the top of each file just under the opening.
<?php
namespace MainWP\Extensions\Development;Class Declaration
Just like above you will want to rename your class declaration by replacing the word “Development” with your add-ons name – everything else may stay the same here:
<?php
class MainWP_Development_Widget {
/**
* Static variable to hold the single instance of the class.
*
* @static
*
* @var mixed Default null
*/
static $instance = null;
public static $instance = null;
/**
* Get Instance
*
* Creates a public static instance of this class file.
*
* @static
*
* @return MainWP_Development_Widget
*/
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
}Class Methods
__construct()
PHP allows developers to declare constructor methods for classes. Classes that have a constructor method call this method on each newly created object, so it is suitable for any initialization that the object may need before it is used.
This is where we will load all of our actions, and filters & Initiate the rest of our add-ons class files.
<?php
public function __construct() {
}
?>render_metabox()
This method will load the correct widget depending on which page the user is on.
<?php
public function render_metabox() {
if ( ! isset( $_GET['page'] ) || 'managesites' == $_GET['page'] ) {
$this->render_site_overview_metabox();
} else {
$this->render_general_overview_widget();
}
}render_site_overview_metaboxl()
This method is where the Widget UI would go for the Overview Page.
render_site_overview_widget()
This method is used to display the main add-ons page tabs.
<?php
public function render_general_overview_widget() {
// Widget content here
}
public static function render_site_overview_widget() {
$site_id = isset( $_GET['dashboard'] ) ? $_GET['dashboard'] : 0;
if ( empty( $site_id ) ) {
return;
}
// Widget content here
}
?>