This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
plugins:tabs_menus [2013/04/19 15:04] 127.0.0.1 external edit |
plugins:tabs_menus [2014/02/07 10:02] datiswous [Side Menu Creation Function] |
||
---|---|---|---|
Line 3: | Line 3: | ||
You can add menu links to any of GetSimple's tabs with the following code | You can add menu links to any of GetSimple's tabs with the following code | ||
- | <code>add_action('xxxxxx-sidebar','createSideMenu',array('your-plugin-filename','Menu Text'));</code> | + | <code php>add_action( 'xxxxxx-sidebar', 'createSideMenu', array( 'your-plugin-filename', 'Menu Text' ) );</code> |
Replace ''xxxxxx-sidebar'' with the sidebar you want(e.g.: ''backups-sidebar'', ''files-sidebar'', ''pages-sidebar'', ''plugins-sidebar'', ''settings-sidebar'', ''support-sidebar'' or ''theme-sidebar'') | Replace ''xxxxxx-sidebar'' with the sidebar you want(e.g.: ''backups-sidebar'', ''files-sidebar'', ''pages-sidebar'', ''plugins-sidebar'', ''settings-sidebar'', ''support-sidebar'' or ''theme-sidebar'') | ||
- | (3.1+) If you want to add multiple links for your plugin, you can use the optional parameter ''action'': | + | (3.1+) If you want to add multiple links for your plugin, you can add an optional 3rd parameter in the params array: |
- | <code>add_action('xxxxxx-sidebar','createSideMenu',array('your-plugin-filename','Menu Text', 'my-action'));</code> | + | <code php>add_action( 'xxxxxx-sidebar', 'createSideMenu', array( 'your-plugin-filename', 'Menu Text', 'my-action' ) );</code> |
- | This way a new parameter //action=my-action// is added to the link and you can determine which link was clicked. | + | This way a new parameter **//my-action//** is added to the link so you can determine which link was clicked. This can be done by checking in your plugin's main function if ''$_GET['**//my-action//**']'' was set: |
+ | <code php> | ||
+ | if( isset( $_GET[ 'my-action1' ] ) ){ | ||
+ | ... | ||
+ | } | ||
+ | </code> | ||
- | (3.1+) If you want a functionality like the "Edit Page" link which only shows if the function is active, add a 4th parameter ''false'': | + | (3.1+) If you want a functionality like the "Edit Page" link which is only visible if the function is active, add a 4th parameter in the params array as ''false'': |
- | <code> | + | <code php> |
- | add_action('xxxxxx-sidebar','createSideMenu',array('your-plugin-filename','Show list of It', 'list')); | + | add_action( 'xxxxxx-sidebar', 'createSideMenu', array( 'your-plugin-filename', 'Show It', 'list' ) ); |
- | add_action('xxxxxx-sidebar','createSideMenu',array('your-plugin-filename','Edit It', 'edit', false)); | + | add_action( 'xxxxxx-sidebar', 'createSideMenu', array( 'your-plugin-filename', 'Edit It', 'edit', false ) ); |
</code> | </code> | ||
When you click the newly created menu link, the function you registered in ''register_plugin()'' will be called, and unless you specified the page your new menu link is on in the 7th argument of ''register_plugin()'', the side menu will disappear. | When you click the newly created menu link, the function you registered in ''register_plugin()'' will be called, and unless you specified the page your new menu link is on in the 7th argument of ''register_plugin()'', the side menu will disappear. | ||
- | Thus if you add side bar links to two different tabs, you need to change this parameter dynamically, e.g. (setup on the plugins tab, other functionality on theme tab): | + | If you need to add side bar menu entries to multiple different tabs, you'll want to dynamically set the page type to ''register_plugin()'', so that the side bar menus won't disappear as soon as you click any menu entry created by your plugin (unless this is desired). One way to get around that is to create a simple function called in place of the page type argument that will check what kind of action was called and return a corresponding page type: |
+ | <code php> | ||
+ | function page_type() | ||
+ | { | ||
+ | if( isset( $_GET[ 'my-action1' ] ) ){ | ||
+ | return 'page_type1'; | ||
+ | } elseif( isset( $_GET[ 'my-action2' ] ) ){ | ||
+ | return 'page_type2'; | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
- | <code> | + | Let's combine all that knowledge in this example - we want to have a plugin that adds two side bar menu entries: one with **//edit//** action in ''pages'' tab and one with **//show//** action in ''themes'' tab. |
- | $thisfile = basename(__FILE__, ".php"); | + | First, let's create the plugin's ID: |
- | + | <code php> | |
- | # register plugin | + | $thisfile = basename( __FILE__, ".php" ); |
+ | </code> | ||
+ | Then the dynamic page type function that will return proper page type for chosen actions: | ||
+ | <code php> | ||
+ | function page_type() | ||
+ | { | ||
+ | if( isset( $_GET[ 'edit' ] ) ){ | ||
+ | return 'pages'; | ||
+ | } elseif( isset( $_GET[ 'show' ] ) ){ | ||
+ | return 'themes'; | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | Now we can register the plugin with page_type() function call in place of page type parameter: | ||
+ | <code php> | ||
register_plugin( | register_plugin( | ||
- | $thisfile, //Plugin id | + | $thisfile, # Plugin id |
- | 'Hello World', //Plugin name | + | 'MyPlugin', # Plugin name |
- | '1.0', //Plugin version | + | '1.0', # Plugin version |
- | 'Chris Cagle', //Plugin author | + | 'John Hancock', # Plugin author |
- | 'http://www.cagintranet.com/', //author website | + | 'http://website.com', # author website |
- | 'Finds email addresses in content and components and "hides" them', //Plugin description | + | 'A plugin that does things', # Plugin description |
- | @$_GET['id'] == $thisfile && @$_GET['action'] == 'setup' ? 'plugins' : 'theme', | + | page_type(), # (dynamic) page type - on which admin tab to display |
- | //page type - on which admin tab to display | + | 'main_MyPlugin' # main plugin function |
- | 'hello_world_show' //main function (administration) | + | |
); | ); | ||
- | |||
- | add_action('plugins-sidebar','createSideMenu',array($thisfile,'Setup Hello World','setup')); | ||
- | add_action('theme-sidebar','createSideMenu',array($thisfile,'Show Hello World','show')); | ||
</code> | </code> | ||
- | + | And the menu entries with proper actions... | |
- | If you have multiple sidebar items like above, your main function (here //hello_world_show//) would look like this: | + | <code php> |
- | <code> | + | add_action( 'pages-sidebar', 'createSideMenu', array( $thisfile, 'Edit it', 'edit' ) ); |
- | function hello_world_show() { | + | add_action( 'theme-sidebar', 'createSideMenu', array( $thisfile, 'Show it', 'show' ) ); |
- | if (@$_GET['action'] == 'setup') { | + | </code> |
- | ... | + | Finally the plugin's main function with action detection: |
- | } else if (@$_GET['action'] == 'show') { | + | <code php> |
- | ... | + | function main_MyPlugin() { |
- | } | + | if ( isset ( $_GET['edit'] ) { |
+ | ... | ||
+ | } elseif ( isset ( $_GET['show'] ) { | ||
+ | ... | ||
+ | } | ||
} | } | ||
</code> | </code> | ||
- | |||
===== Tab Creation Function ===== | ===== Tab Creation Function ===== | ||
Line 60: | Line 88: | ||
<code> | <code> | ||
- | add_action('nav-tab','createNavTab',array('thetabname','your-plugin-filename','Tab Text', 'my-action')); | + | add_action( 'nav-tab', 'createNavTab', array( 'thetabname', 'your-plugin-filename', 'Tab Text', 'my-action' ) ); |
</code> | </code> | ||
Line 67: | Line 95: | ||
<code> | <code> | ||
- | add_action('thetabname-sidebar', ...); // e.g. 'newsmanager-sidebar' | + | add_action( 'thetabname-sidebar', ... ); // e.g. 'newsmanager-sidebar' |
</code> | </code> | ||
Only use this functionality, if you need a lot of side bar links for you plugin and want to group them. | Only use this functionality, if you need a lot of side bar links for you plugin and want to group them. |