• 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 4 replies - 1 through 4 (of 4 total)
  • Thread Starter pipo5000

    (@pipo5000)

    Still no luck..

    What I’ve done so far:

    add_action('admin_menu', 'tm_plugin_menu_create');
    function tm_plugin_menu_create() {
    
    	/* define parent menu */
    	$tm_plugin_menu_parent = __('Terpstra Media', 'tm-plugin');
    
    	/* rename or create new submenu items */
    	$tm_plugin_menu_child = array(
    		__('General Settings', 'tm-plugin'),
    		__('Widgets', 'tm-plugin'),
    		__('Shortcodes', 'tm-plugin')
    	);
    
    	/* general plugin menu settings */
    	$tm_plugin_menu_general = array(
    		'icon_url'	=>	'dashicons-sos',
    		'position'	=>	'2.1',
    		'capability'	=>	'manage_options',
    		'slug_prefix'	=>	'tm-plugin-',
    		'func_prefix'	=>	'tm_plugin_',
    		'slug_suffix'	=>	'.php'
    	);
    
    	if ( function_exists('add_menu_page') ) {
    		add_menu_page(
    			$tm_plugin_menu_parent,
    			$tm_plugin_menu_parent,
    			$tm_plugin_menu_general['capability'],
    			$tm_plugin_menu_general['slug_prefix'] . $tm_plugin_menu_parent . $tm_plugin_menu_general['slug_suffix'],
    			$tm_plugin_menu_general['func_prefix'] . $tm_plugin_menu_parent,
    			$tm_plugin_menu_general['icon_url'],
    			$tm_plugin_menu_general['position']
    		);
    	}
    	if ( function_exists('add_submenu_page') ) {
    		foreach ($tm_plugin_menu_child as $page) {
    			add_submenu_page(
    				$tm_plugin_menu_general['slug_prefix'] . $tm_plugin_menu_parent,
    				$page,
    				$page,
    				$tm_plugin_menu_general['capability'],
    				$tm_plugin_menu_general['slug_prefix'] . $page . $tm_plugin_menu_general['slug_suffix'],
    				$tm_plugin_menu_general['func_prefix'] . $page
    			);
    		}
    	}
    }

    What problems are you having? Are you getting PHP errors? Is there something in WordPress’ debug.log? Are the menu items just not displaying?

    To turn on WordPress debugging, set the following two constants in wp-config.php:

    define('WP_DEBUG', true);
    define('WP_DEBUG_LOG', true);

    And then open an admin page and check if there is something in wp-content/debug.log.

    That’s a good place to start.

    Thread Starter pipo5000

    (@pipo5000)

    I am looking for a way to clean up my code. I’ve read about extensible plugins and I’d like mine to be. I want to be able to create new features for my plugin without having to edit 20 files every time.

    What I’ve tried is to leave all the options I might want to change out of the function that actually creates it. What is was wondering is if there are any other (easier but mainly better) ways of doing this.

    I see what you mean. Honestly, what you have is pretty good. I don’t think you need to check if the WordPress functions exist, because they should always be there… if they’re not, there is a much bigger problem.

    Something I’ve learned over the years is to not overcomplicate things under the disguise of extensibility. For example, don’t create options or variables just for the sake of that you’ll “maybe” need them later or if they, realistically, will infrequently change.

    I personally would opt for readability here and do something like this:

    function tm_plugin_menu_create() {
    	add_menu_page(
    		__('Terpstra Media', 'tm-plugin'),
    		__('Terpstra Media', 'tm-plugin'),
    		'manage_options',
    		'tm-plugin',
    		'tm_plugin_menu',
    		'dashicons-sos',
    		'2.1'
    	);
    	add_submenu_page(
    		'tm-plugin',
    		__('General Settings', 'tm-plugin'),
    		__('General Settings', 'tm-plugin'),
    		'manage_options',
    		'tm-plugin-general',
    		'tm_plugin_menu_general'
    	);
    	add_submenu_page(
    		'tm-plugin',
    		__('Widgets', 'tm-plugin'),
    		__('Widgets', 'tm-plugin'),
    		'manage_options',
    		'tm-plugin-widgets',
    		'tm_plugin_menu_widgets'
    	);
    	add_submenu_page(
    		'tm-plugin',
    		__('Shortcodes', 'tm-plugin'),
    		__('Shortcodes', 'tm-plugin'),
    		'manage_options',
    		'tm-plugin-shortcodes',
    		'tm_plugin_menu_shortcodes'
    	);
    }

    It’s much less complex and in this case, just as easily manageable.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Simplifying add_submenu_page()?’ is closed to new replies.