Template for Adding a New Admin Settings Page for your Plugin

If you’re building a WordPress plugin, chances are you’ll need a settings page to go along with it (Unless you’re Hello Dolly).

If you’re like me and have had to go through this many times, you would agree that building an admin settings page for your plugin become tedious and takes up time that could better be spent on the actual plugin.

So I’ve finally done myself a favor and built a template for this. Pretty much all that needs to be done is change all plugin_name instances to your plugin name (obv) and include your view file in plugin_name_render_main_page() around line 92.

*note, to add this as a PHPstorm File Template, replace the dollar signs with ${DS}.

/*
 * Template to add admin settings page
 *
 * just change "plugin_name" and include your view file
 *
*/

class Plugin_Name_Admin {
	
	function __construct() {
		
		// Register & Enqueue css, JS, whatever
//		add_action( 'admin_init', array( $this, 'plugin_name_register_assets' ) );
//		add_action( 'admin_init', array( $this, 'plugin_name_enqueue_assets' ) );
		
		// Set up the admin page in menu
		add_action( 'admin_menu', array( $this, 'plugin_name_register_admin_page' ), 1 );
		
		// Save changes
		add_action( 'admin_init', array( $this, 'plugin_name_name_save_settings' )  );
	}
	
//	function plugin_name_register_assets() {
//		wp_register_style( 'plugin_name-css', 'path-to-your/style.css' );
//	}
	

	/*
	 * Register your Menu or Submenu page
	 */
	function plugin_name_register_admin_page() {
		
		/*
		 *  Menu Page
		 *
		$plugin_name_page_title = 'Page Title';
		$plugin_name_menu_title = 'Menu Title';
		$plugin_name_capability = 'manage_options';
		$plugin_name_menu_slug  = 'parent-slug';
		$plugin_name_function   = array( $this, 'render_plugin_name_main_page' );
		$plugin_name_icon_url   = 'icon-url.com';
		$plugin_name_position   = 10;
		
		
		$plugin_name_hook = add_menu_page(
			$plugin_name_page_title,
			$plugin_name_menu_title,
			$plugin_name_capability,
			$plugin_name_menu_slug,
			$plugin_name_function,
			$plugin_name_icon_url,
			$plugin_name_position,
		);
		*/
		
		
		/*
		 *  Submenu Page
		 * 
		$parent_slug            = 'settings.php';
		$plugin_name_page_title = 'Page Title';
		$plugin_name_menu_title = 'Menu Title';
		$plugin_name_capability = 'manage_options';
		$plugin_name_menu_slug  = 'parent-slug';
		$plugin_name_function   = array( $this, 'render_plugin_name_main_page' );
		
		$plugin_name_hook = add_submenu_page(
			$parent_slug,
			$plugin_name_page_title,
			$plugin_name_menu_title,
			$plugin_name_capability,
			$plugin_name_menu_slug,
			$plugin_name_function
		);
		*/

		add_action( 'admin_enqueue_scripts-' . $plugin_name_hook, array( $this, 'plugin_name_enqueue_assets' ) );
	}

//	function plugin_name_enqueue_assets() {
//		wp_enqueue_style( 'plugin_name-css' );
//	}
	
	/*
	 * 
	 * INCLUDE THE VIEW FILE
	 * 
	 * 
	 */
	function plugin_name_render_main_page() {
		require 'path-to-your/view-file.php';
	}
	
	/**
	 * Save our settings
	 */
	function plugin_name_save_settings() {
		if ( isset( $_POST['plugin_name_save_nonce'] ) && wp_verify_nonce( $_POST['plugin_name_save_nonce'], 'plugin_name_save' ) ) {
			
			add_action( 'admin_notices', array( $this, 'plugin_name_updated_success_message' ) );
		}
	}
	function plugin_name_updated_success_message() {
		echo '

Settings Updated!

'; } } new Plugin_Name_Admin();