Lesson 3 – The MainWP Development Add-on – AJAX
AJAX
class-mainwp-development-ajax.php
What’s this Class for?
This class is where all the AJAX process code will be stored.
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 before, 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_Ajax {
/**
* 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_Ajax
*/
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() {
add_action( 'admin_init', array( &$this, 'admin_init' ) );
}
?>admin_init()
This method will load the correct methods. Fires as an admin screen or script is being initialized.
<?php
public function admin_init() {
}ajax_do_something()
Write custom functions.