This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
plugins:tabs_menus [2014/01/04 11:14] Everyone [Tab Creation Function] |
plugins:tabs_menus [2014/01/08 22:44] Everyone [Side Menu Creation Function] |
||
---|---|---|---|
Line 11: | Line 11: | ||
<code>add_action( 'xxxxxx-sidebar', 'createSideMenu', array( 'your-plugin-filename', 'Menu Text', 'my-action' ) );</code> | <code>add_action( 'xxxxxx-sidebar', 'createSideMenu', array( 'your-plugin-filename', 'Menu Text', 'my-action' ) );</code> | ||
- | This way a new parameter **//my-action//** is added to the link so 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> | ||
+ | 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 in the params array as ''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> | ||
Line 22: | Line 27: | ||
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> | <code> | ||
- | $thisfile = basename(__FILE__, ".php"); | + | function page_type() |
+ | { | ||
+ | if( isset( $_GET[ 'my-action1' ] ) ){ | ||
+ | return 'page_type1'; | ||
+ | } elseif( isset( $_GET[ 'my-action2' ] ) ){ | ||
+ | return 'page_type2'; | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
- | # register plugin | + | 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. |
+ | First, let's create the plugin's ID: | ||
+ | <code> | ||
+ | $thisfile = basename( __FILE__, ".php" ); | ||
+ | </code> | ||
+ | Then the dynamic page type function that will return proper page type for chosen actions: | ||
+ | <code> | ||
+ | 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> | ||
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> |
+ | add_action( 'pages-sidebar', 'createSideMenu', array( $thisfile, 'Edit it', 'edit' ) ); | ||
+ | add_action( 'theme-sidebar', 'createSideMenu', array( $thisfile, 'Show it', 'show' ) ); | ||
+ | </code> | ||
+ | Finally the plugin's main function with action detection: | ||
<code> | <code> | ||
- | function hello_world_show() { | + | function main_MyPlugin() { |
- | if ( isset ( $_GET['setup'] ) { | + | if ( isset ( $_GET['edit'] ) { |
- | ... | + | ... |
- | } elseif ( isset ( $_GET['show'] ) { | + | } elseif ( isset ( $_GET['show'] ) { |
- | ... | + | ... |
- | } | + | } |
} | } | ||
</code> | </code> | ||
- | |||
===== Tab Creation Function ===== | ===== Tab Creation Function ===== | ||