Posted on

Connecting your Plugin to MainWP

This is Part 2 (part 1) in a series of blog posts surrounding WordPress Plugins, MainWP Extensions, and putting it all together in a useful way. Once we’re done this series of blog posts, you’ll have a working plugin recognized by WordPress that is also recognized by MainWP as an extension. It will be able to interact with your WordPress installation as well as your MainWP plugin. Look for Part 1 in the series here.

This post is part of a tutorial on creating your own MainWP Extension

1. How to create a basic WordPress plugin
2. Turning that basic plugin into an MainWP Extension
3. Passing Information to the Child site
4. Extensions using Classes in MainWP
5. Monetizing your new Extension

Right now you should have a brand new, working WordPress plugin that is recognized by WordPress, is activated, and can store a variable inside of WordPress quite easily using a simple form. Now the plan is to get it to show up inside of MainWP as an extension, then take it one step further.

Showing your Plugin as an Extension

If you refer to the documentation for MainWP, it’s as simple as copying and pasting some code into your php file in order to get it to work. Lets do that now – a copy of the code is below:

$childKey = false;

add_filter('mainwp-getextensions', 'get_this_extension');
$mainWPActivated = apply_filters('mainwp-activated-check', false);

if ($mainWPActivated !== false) {
    activate_this_plugin();
} else {
    add_action('mainwp-activated', 'activate_this_plugin');
}

function get_this_extension($extensions) {
    $extensions[] = array('plugin' =>  __FILE__, 'callback' => 'my_plugin_extension_settings');
    return $extensions;
}

function my_plugin_extension_settings() {
    do_action('mainwp-pageheader-extensions', __FILE__);
    echo 'Your custom settings page';
    do_action('mainwp-pagefooter-extensions', __FILE__);
}

function activate_this_plugin() {
  global $childEnabled;
  $childEnabled = apply_filters('mainwp-extension-enabled-check', __FILE__);
  if (!$childEnabled) return;

  $childKey = $childEnabled['key'];
  
  //Code to initialize your plugin
}

Let’s break this down a bit for those new to MainWP, and take it right back to the beginning. You should have the MainWP plugin installed and activated, but you’re not required to do anything else over and above that. For the purposes of this post, we can use it without setting up any accounts or doing anything additional to it.

After MainWP is installed and activated, and you take the above code and place it inside your test.php file, you should see both the plugin activated inside of WordPress and the plugin listed as an extension inside of MainWP:

mainwpextensionpage

You can hit the enable button next to your extension and it will activate it. It will show up as a sub-menu item underneath the MainWP menu in the sidebar, and give you access to the settings page, which at this point is just a line of text, taken from the below function. This is what we will edit to switch over your WordPress plugin  settings page to a MainWP settings page.

function my_plugin_extension_settings() {
    do_action('mainwp-pageheader-extensions', __FILE__);
    echo 'Your custom settings page';
    do_action('mainwp-pagefooter-extensions', __FILE__);
}

 

settingspage

 

As you can see, you have a very basic settings page that doesn’t do a whole lot. Let’s take what we created in the previous post and switch it over.

Moving from WordPress management pages to MainWP

Find the function we used to create the settings page and rename it, then add the two actions that MainWP requires – one for the header wrap and one for the footer wrap of the plugin. It will look something like this:

function my_plugin_extension_settings() {
    do_action('mainwp-pageheader-extensions', __FILE__);
    $textvar = get_option('test_plugin_variable', 'hello world');
 
     if (isset($_POST['change-clicked'])) {
         update_option( 'test_plugin_variable', $_POST['footertext'] );
         $textvar = get_option('test_plugin_variable', 'hello world');
     } 
 
 ?>
 <div class="wrap">
 <h1>Footer Text</h1>
 <p>This simple plugin will output some text to the footer of your template. Change the text below:</p>
 <form action="<?php echo str_replace('%7E', '~', $_SERVER['REQUEST_URI']); ?>" method="post">
 Footer Text:<input type="text" value="<?php echo $textvar; ?>" name="footertext" placeholder="hello world"><br />
 <input name="change-clicked" type="hidden" value="1" />
 <input type="submit" value="Change Text" />
 </form>
 </div>
 <?
    do_action('mainwp-pagefooter-extensions', __FILE__);
}

Of course, this also means we are removing the WordPress settings page, so let’s go ahead and remove these lines that add the management page through an admin menu action, and the previous function that is no longer needed:

add_action('admin_menu', 'my_admin_menu');
function my_admin_menu () {
  add_management_page('Footer Text', 'Footer Text', 'manage_options', __FILE__, 'footer_text_admin_page');
}

function footer_text_admin_page () {
	   
}

Easy! All of your settings for the plugin are now easily shown inside of MainWP:

settingspart2

 

We should also take a quick run through of what the rest of the MainWP functions do:

$childKey = false; 

add_filter('mainwp-getextensions', 'get_this_extension'); 
// This is a filter similar to adding the management page in WordPress. It calls the function get_this_extension, which adds to the $extensions array. This array is a list of all of the extensions MainWP uses, and the functions that it has to call to show settings for them. In this case, the function is my_plugin_extension_settings.

$mainWPActivated = apply_filters('mainwp-activated-check', false);
//this variable checks to see if mainwp is activated, and by default it will return false unless it finds in WordPress that it's on.

if ($mainWPActivated !== false) {
    activate_this_plugin(); // If MainWP is activated, then call the function activate_this_plugin
} else {
    add_action('mainwp-activated', 'activate_this_plugin');
}
function get_this_extension($extensions) {
    $extensions[] = array('plugin' =>  __FILE__, 'callback' => 'my_plugin_extension_settings');
    //this just adds the plugin's extension settings page to the array $extensions so it knows what to call.
    return $extensions;
}

 

In the next post, we will dive into talking to the MainWP main plugin, and passing some information to your child sites. You can view the next post here.

Get MainWP News and Notifications

Brendan
Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.