MainWP Community

MainWP extension tutorial question

Greetings. I have been working with your sample tutorial building a plug-in.

Everything is going Ok. It works and activates, but when I click any of the generated site links on the page I get: “Sorry, you are not allowed to access this page.”

I assume it’s a permissions issue, but not sure what to do to fix it.

Thanks in advance.

Hi Dwayne, thanks for reaching out.

Can you please send us more details? Screenshot maybe so I can understand the problem better.

Sure, Ive been following your extension tutorial. Right now the code is identical to your example. Click on any of the open location links and it goes to a redirecting screen, followed by the message: “Sorry, you are not allowed to access this page.”

I pasted the PHP code from the section of the sample project below, just in case you need it.

                    //Display number of your child sites
                    ?>
                    <p><?php echo __('Number of Managed Sites: ') . count($websites); ?></p>
                    <div id="mainwp_example_links">
                        <?php
                        //Display a list of your child site with a secure link to child site WP Admin (login not required)
                        foreach($websites as $site) { 
                            // Create link to open $location on child site
                            $open_url = "admin.php?page=SiteOpen&newWindow=yes&websiteid=". $site['id'] . "&location=" . base64_encode($location) ;
                            echo $site['name'] .": ";
                            ?>
                            <a href="<?php echo $open_url; ?>" class="queue" target="_blank"><?php _e("Open location", "mainwp");?></a>.                        
                            <br/>              
                        <?php            
                        }
                        ?>

Ultimately I’m planning on building a custom ad extension to access the advertising panels in the sites to simplify management for the client.

Thanks in advance,
-D

Hi Dwane, can you send me your full code via private message so I can check it out?

I’m happy to post the test version here if it assists the community and gets rid of the damn error message. :slight_smile:

<?php
/*
Plugin Name: Test Plugin
Plugin URI: https://mainwp.com
Description: Test Plugin - we want to launch in-site admin links from this dashboard.
Version: 0.9.3
Author: Dwayne Josephson
Author URI: https://josephsonconsulting.com/
Icon URI: 
*/
class MainWPExampleExtension
{
  
    public function __construct()
	{		
		add_filter('mainwp-getsubpages-sites', array(&$this, 'managesites_subpage' ), 10, 1 );
	}

	function managesites_subpage( $subPage ) {		

		$subPage[] = array(
			'title'                 => 'Advertising Extension',
			'slug'                     => 'ExampleExtension',
			'sitetab'             => true,
			'menu_hidden'     => true,
			'callback'             => array( 'MainWPExampleExtension', 'renderPage' ),
		);

		return $subPage;
	}

    /*
    * Create your extension page
    */

    public static function renderPage() {
        global $mainWPExampleExtensionActivator;

        // Fetch all child-sites 
        $websites = apply_filters('mainwp-getsites', $mainWPExampleExtensionActivator->getChildFile(), $mainWPExampleExtensionActivator->getChildKey(), null);              
        
        // Location to open on child site
        $location = "admin.php?page=mainwp_child_tab";                 
        
        if (is_array($websites)) {
            ?>      
            <div class="postbox">
                <div class="inside">
                    <p><?php _e('KEM Advertising plug-in. This extension provides for centralized Advertising management'); ?></p>
                    <p><?php echo __('For more details, please visit us at ') .'<a href="https://josephsonconsulting.com/" target="_blank">'. __('Josephson Consulting') . '</a>' . ' website.'; ?></p>
                </div>
            </div>
            <div class="postbox">
                <h3 class="mainwp_box_title"><?php _e('Get Connected Advertising Sites'); ?></h3>
                <div class="inside">
                    <em><?php _e('List all connected advertising sites and link to open the child site'); ?></em>
                    <?php
                    //Display number of your child sites
                    ?>
                    <p><?php echo __('Number of Managed Sites: ') . count($websites); ?></p>
                    <div id="mainwp_example_links">
                        <?php
                        //Display a list of your child site with a secure link to child site WP Admin (login not required)
                        foreach($websites as $site) { 
                            // Create link to open $location on child site
                            $open_url = "admin.php?page=SiteOpen&newWindow=yes&websiteid=". $site['id'] . "&location=" . base64_encode($location) ;
                            echo $site['name'] .": ";
                            ?>
                            <a href="<?php echo $open_url; ?>" class="queue" target="_blank"><?php _e("Open location", "mainwp");?></a>.                        
                            <br/>              
                        <?php            
                        }
                        ?>
                    </div>  
                </div>
            </div>
            
            <div class="postbox">
                <h3 class="mainwp_box_title"><?php _e('Get Active Ads From Child Sites'); ?></h3>
                <div class="inside">
                    <em><?php _e('List all active Ads on your child site'); ?></em>           
                        <?php $site = $websites[0]; ?>
                        <p><?php _e('Get posts from child site:'); ?> <?php echo $site['url'] ?> <a href="admin.php?page=Extensions-Mainwp-Example-Extension&siteid=<?php echo $site['id']; ?>" class="button button-primary"><?php _e('Click Here!'); ?></a></p>
                        <?php            
                        if (isset($_GET['siteid']) && !empty($_GET['siteid'])) {
                            $websiteId = $_GET['siteid'];         
                            //fetch information of one child-site                 
                            $website = apply_filters('mainwp-getsites', $mainWPExampleExtensionActivator->getChildFile(), $mainWPExampleExtensionActivator->getChildKey(), $websiteId);            
                            if ($website && is_array($website)) {
                                $website = current($website);
                            }  

                            if (!$website) {
                                echo "<p><strong>ERROR</strong>: ". __('Child Site Not Found') ."</p>";
                            } else {

                                // Example to call function get_all_posts on child-plugin to get posts on child site
                                $post_data = array(               
                                    'status' => 'publish',
                                    'maxRecords' => 10
                                );

                                // hook to call the function get_all_posts
                                $information = apply_filters('mainwp_fetchurlauthed', $mainWPExampleExtensionActivator->getChildFile(), $mainWPExampleExtensionActivator->getChildKey(), $websiteId, 'get_all_posts', $post_data);			                            
                                
                                if (is_array($information)) {
                                    if (isset($information['error'])) {
                                        echo "<p><strong>ERROR</strong>: " . $information['error'] . "</p>";
                                    } else {                                                 
                                        echo "<h3>"._('List of posts: ')."</h3>";
                                        echo "<ol>";
                                        foreach($information as $post) {
                                            echo "<li>";
                                            echo $post['title'];
                                            ?>                        
                                            <a href="<?php echo $website['url'] . (substr($website['url'], -1) != '/' ? '/' : '') . '?p=' . $post['id']; ?>"
                                               target="_blank" 
                                               title="View '<?php echo $post['title']; ?>'" 
                                               rel="permalink"><?php _e('View Post'); ?></a>
                                            <?php 
                                            echo "</li>";
                                        }
                                        echo "</ol>";
                                    }
                                }                  
                            }
                        } 
                    
                    } else {
                            echo "Child Sites Not Found";
                    }
                    ?>
                </div>
            </div>
        <?php
    }    
}


/*
* Activator Class is used for extension activation and deactivation
*/

class MainWPExampleExtensionActivator
{
    protected $mainwpMainActivated = false;
    protected $childEnabled = false;
    protected $childKey = false;
    protected $childFile;
	protected $plugin_handle = 'mainwp-example-extension';
	

    public function __construct()
    {
        $this->childFile = __FILE__;
        add_filter('mainwp-getextensions', array(&$this, 'get_this_extension'));
        
        // This filter will return true if the main plugin is activated
        $this->mainwpMainActivated = apply_filters('mainwp-activated-check', false);

        if ($this->mainwpMainActivated !== false)
        {
            $this->activate_this_plugin();
        }
        else
        {
            //Because sometimes our main plugin is activated after the extension plugin is activated we also have a second step, 
            //listening to the 'mainwp-activated' action. This action is triggered by MainWP after initialisation. 
            add_action('mainwp-activated', array(&$this, 'activate_this_plugin'));
        }        
        add_action('admin_notices', array(&$this, 'mainwp_error_notice'));
    }

    function get_this_extension($pArray)
    {
        $pArray[] = array('plugin' => __FILE__,  'api' => $this->plugin_handle, 'mainwp' => false, 'callback' => array(&$this, 'settings'));
        return $pArray;
    }

    function settings()
    {
        //The "mainwp-pageheader-extensions" action is used to render the tabs on the Extensions screen. 
        //It's used together with mainwp-pagefooter-extensions and mainwp-getextensions
        do_action('mainwp-pageheader-extensions', __FILE__);
        if ($this->childEnabled)
        {
            MainWPExampleExtension::renderPage();
        }
        else
        {
            ?><div class="mainwp_info-box-yellow"><?php _e("The Extension has to be enabled to change the settings."); ?></div><?php
        }
        do_action('mainwp-pagefooter-extensions', __FILE__);
    }
    
    //The function "activate_this_plugin" is called when the main is initialized. 
    function activate_this_plugin()
    {
        //Checking if the MainWP plugin is enabled. This filter will return true if the main plugin is activated.
        $this->mainwpMainActivated = apply_filters('mainwp-activated-check', $this->mainwpMainActivated);
        
        // The 'mainwp-extension-enabled-check' hook. If the plugin is not enabled this will return false, 
        // if the plugin is enabled, an array will be returned containing a key. 
        // This key is used for some data requests to our main
        $this->childEnabled = apply_filters('mainwp-extension-enabled-check', __FILE__);       

        $this->childKey = $this->childEnabled['key'];
		
		new MainWPExampleExtension();

    }

    function mainwp_error_notice()
    {
        global $current_screen;
        if ($current_screen->parent_base == 'plugins' && $this->mainwpMainActivated == false)
        {
            echo '<div class="error"><p>MainWP Hello World! Extension ' . __('requires '). '<a href="http://mainwp.com/" target="_blank">MainWP</a>'. __(' Plugin to be activated in order to work. Please install and activate') . '<a href="http://mainwp.com/" target="_blank">MainWP</a> '.__('first.') . '</p></div>';
        }
    }

    public function getChildKey()
    {
        return $this->childKey;
    }

    public function getChildFile()
    {
        return $this->childFile;
    }
}

global $mainWPExampleExtensionActivator;
$mainWPExampleExtensionActivator = new MainWPExampleExtensionActivator();

Thanks Dwayne.

Can you try with this version:

<?php
/*
Plugin Name: Test Plugin
Plugin URI: https://mainwp.com
Description: Test Plugin - we want to launch in-site admin links from this dashboard.
Version: 0.9.3
Author: Dwayne Josephson
Author URI: https://josephsonconsulting.com/
Icon URI:
*/
class MainWPExampleExtension {
	public function __construct() {
		 add_filter( 'mainwp_getsubpages_sites', array( &$this, 'managesites_subpage' ), 10, 1 );
	}
	function managesites_subpage( $subPage ) {
		$subPage[] = array(
			'title'       => 'Advertising Extension',
			'slug'        => 'ExampleExtension',
			'sitetab'     => true,
			'menu_hidden' => true,
			'callback'    => array( 'MainWPExampleExtension', 'renderPage' ),
		);
		return $subPage;
	}
	/*
	* Create your extension page
	*/
	public static function renderPage() {
		global $mainWPExampleExtensionActivator;
		// Fetch all child-sites
		$websites = apply_filters( 'mainwp_getsites', $mainWPExampleExtensionActivator->getChildFile(), $mainWPExampleExtensionActivator->getChildKey(), null );
		// Location to open on child site
		$location = 'admin.php?page=mainwp_child_tab';
		if ( is_array( $websites ) ) {
			?>      
			<div class="postbox">
				<div class="inside">
					<p><?php _e( 'KEM Advertising plug-in. This extension provides for centralized Advertising management' ); ?></p>
					<p><?php echo __( 'For more details, please visit us at ' ) . '<a href="https://josephsonconsulting.com/" target="_blank">' . __( 'Josephson Consulting' ) . '</a>' . ' website.'; ?></p>
				</div>
			</div>
			<div class="postbox">
				<h3 class="mainwp_box_title"><?php _e( 'Get Connected Advertising Sites' ); ?></h3>
				<div class="inside">
					<em><?php _e( 'List all connected advertising sites and link to open the child site' ); ?></em>
					<?php
					// Display number of your child sites
					?>
					<p><?php echo __( 'Number of Managed Sites: ' ) . count( $websites ); ?></p>
					<div id="mainwp_example_links">
						<?php
						// Display a list of your child site with a secure link to child site WP Admin (login not required)
						foreach ( $websites as $site ) {
							// Create link to open $location on child site
							$open_url = 'admin.php?page=SiteOpen&newWindow=yes&websiteid=' . $site['id'] . '&location=' . base64_encode( $location );
							echo $site['name'] . ': ';
							?>
							<a href="<?php echo $open_url; ?>" class="queue" target="_blank"><?php _e( 'Open location', 'mainwp' ); ?></a>.                        
							<br/>              
							<?php
						}
						?>
					</div>  
				</div>
			</div>
			<div class="postbox">
				<h3 class="mainwp_box_title"><?php _e( 'Get Active Ads From Child Sites' ); ?></h3>
				<div class="inside">
					<em><?php _e( 'List all active Ads on your child site' ); ?></em>           
						<?php $site = $websites[0]; ?>
						<p><?php _e( 'Get posts from child site:' ); ?> <?php echo $site['url']; ?> <a href="admin.php?page=Extensions-Mainwp-Example-Extension&siteid=<?php echo $site['id']; ?>" class="button button-primary"><?php _e( 'Click Here!' ); ?></a></p>
						<?php
						if ( isset( $_GET['siteid'] ) && ! empty( $_GET['siteid'] ) ) {
							$websiteId = $_GET['siteid'];
							// fetch information of one child-site
							$website = apply_filters( 'mainwp_getsites', $mainWPExampleExtensionActivator->getChildFile(), $mainWPExampleExtensionActivator->getChildKey(), $websiteId );
							if ( $website && is_array( $website ) ) {
								$website = current( $website );
							}
							if ( ! $website ) {
								echo '<p><strong>ERROR</strong>: ' . __( 'Child Site Not Found' ) . '</p>';
							} else {
								// Example to call function get_all_posts on child-plugin to get posts on child site
								$post_data = array(
									'status'     => 'publish',
									'maxRecords' => 10,
								);
								// hook to call the function get_all_posts
								$information = apply_filters( 'mainwp_fetchurlauthed', $mainWPExampleExtensionActivator->getChildFile(), $mainWPExampleExtensionActivator->getChildKey(), $websiteId, 'get_all_posts', $post_data );
								if ( is_array( $information ) ) {
									if ( isset( $information['error'] ) ) {
										echo '<p><strong>ERROR</strong>: ' . $information['error'] . '</p>';
									} else {
										echo '<h3>' . _( 'List of posts: ' ) . '</h3>';
										echo '<ol>';
										foreach ( $information as $post ) {
											echo '<li>';
											echo $post['title'];
											?>
											<a href="<?php echo $website['url'] . ( substr( $website['url'], -1 ) != '/' ? '/' : '' ) . '?p=' . $post['id']; ?>"
											   target="_blank" 
											   title="View '<?php echo $post['title']; ?>'" 
											   rel="permalink"><?php _e( 'View Post' ); ?></a>
											<?php
											echo '</li>';
										}
										echo '</ol>';
									}
								}
							}
						}
		} else {
				echo 'Child Sites Not Found';
		}
		?>
				</div>
			</div>
		<?php
	}
}
/*
* Activator Class is used for extension activation and deactivation
*/
class MainWPExampleExtensionActivator {
	protected $mainwpMainActivated = false;
	protected $childEnabled        = false;
	protected $childKey            = false;
	protected $childFile;
	protected $plugin_handle = 'mainwp-example-extension';
	public function __construct() {
		 $this->childFile = __FILE__;
		add_filter( 'mainwp_getextensions', array( &$this, 'get_this_extension' ) );
		// This filter will return true if the main plugin is activated
		$this->mainwpMainActivated = apply_filters( 'mainwp_activated_check', false );
		if ( $this->mainwpMainActivated !== false ) {
			$this->activate_this_plugin();
		} else {
			// Because sometimes our main plugin is activated after the extension plugin is activated we also have a second step,
			// listening to the 'mainwp_activated' action. This action is triggered by MainWP after initialisation.
			add_action( 'mainwp_activated', array( &$this, 'activate_this_plugin' ) );
		}
		add_action( 'admin_notices', array( &$this, 'mainwp_error_notice' ) );
	}
	function get_this_extension( $pArray ) {
		$pArray[] = array(
			'plugin'   => __FILE__,
			'api'      => $this->plugin_handle,
			'mainwp'   => false,
			'callback' => array( &$this, 'settings' ),
		);
		return $pArray;
	}
	function settings() {
		// The "mainwp-pageheader-extensions" action is used to render the tabs on the Extensions screen.
		// It's used together with mainwp-pagefooter-extensions and mainwp-getextensions
		do_action( 'mainwp_pageheader_extensions', __FILE__ );
		if ( $this->childEnabled ) {
			MainWPExampleExtension::renderPage();
		} else {
			?>
			<div class="mainwp_info-box-yellow"><?php _e( 'The Extension has to be enabled to change the settings.' ); ?></div>
														  <?php
		}
		do_action( 'mainwp_pagefooter_extensions', __FILE__ );
	}
	// The function "activate_this_plugin" is called when the main is initialized.
	function activate_this_plugin() {
		// Checking if the MainWP plugin is enabled. This filter will return true if the main plugin is activated.
		$this->mainwpMainActivated = apply_filters( 'mainwp_activated_check', $this->mainwpMainActivated );
		// The 'mainwp_extension_enabled_check' hook. If the plugin is not enabled this will return false,
		// if the plugin is enabled, an array will be returned containing a key.
		// This key is used for some data requests to our main
		$this->childEnabled = apply_filters( 'mainwp_extension_enabled_check', __FILE__ );
		$this->childKey = $this->childEnabled['key'];
		new MainWPExampleExtension();
	}
	function mainwp_error_notice() {
		global $current_screen;
		if ( $current_screen->parent_base == 'plugins' && $this->mainwpMainActivated == false ) {
			echo '<div class="error"><p>MainWP Hello World! Extension ' . __( 'requires ' ) . '<a href="http://mainwp.com/" target="_blank">MainWP</a>' . __( ' Plugin to be activated in order to work. Please install and activate' ) . '<a href="http://mainwp.com/" target="_blank">MainWP</a> ' . __( 'first.' ) . '</p></div>';
		}
	}
	public function getChildKey() {
		 return $this->childKey;
	}
	public function getChildFile() {
		return $this->childFile;
	}
}
global $mainWPExampleExtensionActivator;
$mainWPExampleExtensionActivator = new MainWPExampleExtensionActivator();

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.