Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

pipo5000 on "Simplifying add_submenu_page()?"

$
0
0

Hello everyone,

Recently I have started developing my own plugins. I am trying to make a plugin that I can use easily to add features to my website.

In order to allow myself to easily add dashboard pages/submenu items I made a simplified function of add_submenu_page(). I am just not sure if I am doing this the correct way.

add_action( 'admin_menu', 'tm_plugin_dashboard_menu');
function tm_plugin_dashboard_menu(){

	function tm_plugin_dashboard_submenu( $title, $function ){
		$slug = "terpstra-media-$function";						// Decapitalise page slug by using $function variable.
		$callback = "tm_plugin_dashboard_settings_$function";	// Create callback function name
		// Submenu page defaults
		add_submenu_page( 'terpstra-media', $title, $title, 'manage_options', $slug, $callback );
	}

	/*
	 Create submenu pages trough this function:
	 tm_plugin_dashboard_submenu( $title, $function );
	*/
	tm_plugin_dashboard_submenu( __('Widgets', 'tm-plugin'), 'widgets' );
	tm_plugin_dashboard_submenu( __('Shortcodes', 'tm-plugin'), 'shortcodes' );

	// add plugin main page after submenu pages to prevent wordpress from creating top-level duplicate
	add_menu_page(
			__('Terpstra Media Plugin', 'tm-plugin'),
			__('Terpstra Media Plugin', 'tm-plugin'),
			'manage_options',
			'terpstra-media',
			'tm_plugin_dashboard_settings_main',
			'dashicons-sos',
			'2.1' );
}

I hope you guys can help me accomplish this. As soon as I know how to do this the correct way I can finish my plugin in no-time.


Viewing all articles
Browse latest Browse all 8245

Trending Articles