WordPress – is_my_plugin_page   

Aug 4th, 2011 // In: Php & MySQL, Wordpress // By: Comments 0

This is for new plugin developers. How to code a simple script that checks if the current admin page is one of your plugins pages. I needed something like this when I begun created my WTG Plugin Template, a plugin I plan to use to create many other plugins. It needed a flexible administration menu, one that is built from an array of values. So I ended up with page slugs and the ability to loop through my plugins page slugs.

	$looped = 0;
	foreach($wtgpt_mpt_arr as $key=>$pagearray)
	{

		if (isset($_GET['page']) && $_GET['page'] == $pagearray['slug'])
		{
			add_action( 'wp_print_scripts', 'wtg_load_with_api' );
		}

		++$looped;
	}

As you  can see, we use $_GET['page'] and I always do the isset check with my $_GET and $_POST even if it is for debugging later. Then we compare the page value with our slug. If the current page is a match to one of our plugins pages then do something. Your array may be different from my own, but the idea is to loop through your slugs.

My Action

I wrote my script to call add_action and print scripts, currently jQuery. You can do anything you want knowing that it will only be done when the user is on one of your plugin pages. This could be part of your security, not required as security and anti-conflict. By loading as many files and functions within this loop as you can, your avoiding loading them any other time. Obviously this is fine for functions you only need when viewing one of your plugin pages.

My Array

You may be wondering about the array, especially if you never considered and still can’t figure out why you would want to create an array for building your administration menu. Let me be clear on the fact that I do not do any sort of looping through this array to build the administration menu. However, I think it is possible without major work, simply by adding the page type to the array and using the correct add page function based on it. Here is my array below, ignore the tab areas, they are for tabs within each page and the script above does not consider their slugs…

// main page
$wtgpt_mpt_arr[0]['active'] = true;
$wtgpt_mpt_arr[0]['slug'] = $wtgtp_homeslug;
$wtgpt_mpt_arr[0]['menu'] = "Main Page";
$wtgpt_mpt_arr[0]['role'] = 10;
$wtgpt_mpt_arr[0]['title'] = 'The Main Plugin Page';
// sub page 1 tab 1
$wtgpt_mpt_arr[0]['tabs'][0]['active'] = true;
$wtgpt_mpt_arr[0]['tabs'][0]['slug'] = 'tab1_home';
$wtgpt_mpt_arr[0]['tabs'][0]['label'] = 'Latest Updates';
// sub page 1 tab 2
$wtgpt_mpt_arr[0]['tabs'][1]['active'] = true;
$wtgpt_mpt_arr[0]['tabs'][1]['slug'] = 'tab2_home';
$wtgpt_mpt_arr[0]['tabs'][1]['label'] = 'Support';
// sub page 1 tab 3
$wtgpt_mpt_arr[0]['tabs'][2]['active'] = true;
$wtgpt_mpt_arr[0]['tabs'][2]['slug'] = 'tab3_home';
$wtgpt_mpt_arr[0]['tabs'][2]['label'] = 'Statistics';

// sub page 1
$wtgpt_mpt_arr[1]['active'] = true;
$wtgpt_mpt_arr[1]['slug'] = "wtgpt_subone";
$wtgpt_mpt_arr[1]['menu'] = "Sub One";
$wtgpt_mpt_arr[1]['role'] = 10;
$wtgpt_mpt_arr[1]['title'] = 'The First Sub Page';
$wtgpt_mpt_arr[1]['icon'] = 'options-general';
// sub page 1 tab 1
$wtgpt_mpt_arr[1]['tabs'][0]['active'] = true;
$wtgpt_mpt_arr[1]['tabs'][0]['slug'] = 'tab1_page1';
$wtgpt_mpt_arr[1]['tabs'][0]['label'] = 'Tab One';
// sub page 1 tab 2
$wtgpt_mpt_arr[1]['tabs'][1]['active'] = true;
$wtgpt_mpt_arr[1]['tabs'][1]['slug'] = 'tab2_page1';
$wtgpt_mpt_arr[1]['tabs'][1]['label'] = 'Tab Two';
// sub page 1 tab 3
$wtgpt_mpt_arr[1]['tabs'][2]['active'] = true;
$wtgpt_mpt_arr[1]['tabs'][2]['slug'] = 'tab3_page1';
$wtgpt_mpt_arr[1]['tabs'][2]['label'] = 'Tab Three';

// sub page 10 (reserve for settings which will probably become more complex)
$wtgpt_mpt_arr[10]['active'] = true;
$wtgpt_mpt_arr[10]['slug'] = "wtgpt_subten";
$wtgpt_mpt_arr[10]['menu'] = "Settings";
$wtgpt_mpt_arr[10]['role'] = 10;
$wtgpt_mpt_arr[10]['title'] = 'Settings';
$wtgpt_mpt_arr[10]['icon'] = 'options-general';
// sub page 1 tab 1
$wtgpt_mpt_arr[10]['tabs'][0]['active'] = true;
$wtgpt_mpt_arr[10]['tabs'][0]['slug'] = 'tab1_page10';
$wtgpt_mpt_arr[10]['tabs'][0]['label'] = 'Main';
// sub page 1 tab 2
$wtgpt_mpt_arr[10]['tabs'][1]['active'] = true;
$wtgpt_mpt_arr[10]['tabs'][1]['slug'] = 'tab2_page10';
$wtgpt_mpt_arr[10]['tabs'][1]['label'] = 'Subscriber Access';
// sub page 1 tab 3
$wtgpt_mpt_arr[10]['tabs'][2]['active'] = true;
$wtgpt_mpt_arr[10]['tabs'][2]['slug'] = 'tab3_page10';
$wtgpt_mpt_arr[10]['tabs'][2]['label'] = 'Interface';

My Admin Menu

Now, you might wonder how I use this array when building my plugins administration menu. I did mention above that I do not do any sort of loop, the values are hard coded. I would like to offer my plugin itself for download but by the time anyone reads this the approach to building the menu will be more advanced. I’ll paste what I have right now and maybe you can work on your own from here…

function wtgpt_toppage(){require_once(WTG_PT_PATH.'pages/pagemain/wtgpt_home.php');}
function wtgpt_subpage1(){require_once(WTG_PT_PATH.'pages/pageone/wtgpt_main_page1.php');}
function wtgpt_subpage10(){require_once(WTG_PT_PATH.'pages/pageten/wtgpt_main_page10.php');}

function wtgpt_admin_menu(){
global $wtgpt_mpt_arr;
global $wtgtp_homeslug;
add_menu_page($wtgpt_mpt_arr[0]['menu'],$wtgpt_mpt_arr[0]['menu'],$wtgpt_mpt_arr[0]['role'],$wtgtp_homeslug,'wtgpt_toppage');
add_submenu_page(__FILE__ ,$wtgpt_mpt_arr[1]['menu'],$wtgpt_mpt_arr[1]['menu'],$wtgpt_mpt_arr[1]['role'],$wtgpt_mpt_arr[1]['slug'],'wtgpt_subpage1');
add_submenu_page(__FILE__ ,$wtgpt_mpt_arr[10]['menu'],$wtgpt_mpt_arr[10]['menu'],$wtgpt_mpt_arr[10]['role'],$wtgpt_mpt_arr[10]['slug'],'wtgpt_subpage10');
}

You will notice how all slugs and names are generic. The plugin is a template I plan to use to create many other plugins. All I need to do is edit the array to configure the menu per plugin.

Tags: - -

Leave a Reply

You must be logged in to post a comment.