Snippets

SlimBob WP Settings API Class

Updated by SlimBob WP

File settings-api-class.php Modified

  • Ignore whitespace
  • Hide word diff
 
 	'submit_button_text'    =>    'Save Changes',
 
-	//'option_dump'           =>    'true',
+	'option_dump'           =>    'true',
 
 );
 
Updated by SlimBob WP

File settings-api-class.php Modified

  • Ignore whitespace
  • Hide word diff
 
 		echo '<div id="poststuff">';
 		
-			echo '<div class="postbox">';
+			echo '<div class="stuffbox">';
 
-				echo '<h2 class="hndle">Debug Information: Option Dump</h2>';
+				echo '<h3 class="hndle">Debug Information for Option Name: <em>' . $this->data['option_name'] . '</em></h3>';
 
 				echo '<div class="inside">';
 
+					echo '<strong>Basic Output:</strong>';
+
+					echo '<pre>';
+
+						print_r( get_option( $this->data['option_name'] ) );
+
+					echo '</pre>';
+
+					echo '<strong>Advanced Output:</strong>';
+
 					echo '<pre>';
 
 						var_dump( get_option( $this->data['option_name'] ) );
 
 	'submit_button_text'    =>    'Save Changes',
 
-	'option_dump'           =>    'true',
+	//'option_dump'           =>    'true',
 
 );
 
Updated by SlimBob WP

File settings-api-class.php Modified

  • Ignore whitespace
  • Hide word diff
 
 	function option_dump() {
 
-		echo '<div class="postbox">';
+		echo '<div id="poststuff">';
+		
+			echo '<div class="postbox">';
+
+				echo '<h2 class="hndle">Debug Information: Option Dump</h2>';
 
-			echo '<h2>Debug Information: Option Dump</h2>';
+				echo '<div class="inside">';
 
-			echo '<div class="inside">';
+					echo '<pre>';
 
-				echo '<pre>';
+						var_dump( get_option( $this->data['option_name'] ) );
 
-					var_dump( get_option( $this->data['option_name'] ) );
+					echo '</pre>';
 
-				echo '</pre>';
+				echo '</div>';
 
 			echo '</div>';
 
Updated by SlimBob WP

File settings-api-class.php Modified

  • Ignore whitespace
  • Hide word diff
 
 			switch( $this->data['use_parent_menu'] ) {
 
-				case "Dashboard":
+				case 'Dashboard':
 
-					add_dashboard_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
+					add_dashboard_page(  $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
 
 					break;
 
-				case "Posts":
+				case 'Posts':
 
-					add_posts_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
+					add_posts_page(      $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
 
 					break;
 
-				case "Media":
+				case 'Media':
 
-					add_media_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
+					add_media_page(      $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
 
 					break;
 
-				case "Pages":
+				case 'Pages':
 
-					add_pages_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
+					add_pages_page(      $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
 
 					break;
 
-				case "Comments":
+				case 'Comments':
 
-					add_comments_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
+					add_comments_page(   $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
 
 					break;
 
-				case "Appearance":
+				case 'Appearance':
 
-					add_theme_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
+					add_theme_page(      $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
 
 					break;
 
-				case "Plugins":
+				case 'Plugins':
 
-					add_plugins_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
+					add_plugins_page(    $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
 
 					break;
 
-				case "Users":
+				case 'Users':
 
-					add_users_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
+					add_users_page(      $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
 
 					break;
 
-				case "Tools":
+				case 'Tools':
 
 					add_management_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
 
 					break;
 
-				case "Settings":
+				case 'Settings':
 
-					add_options_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
+					add_options_page(    $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
 
 					break;
 
 
 			echo '<h1>' . $this->data['page_title'] . '</h1>';
 
-			// Uncomment the following function to see output of option array.
-			//$this->pretty_option_dump();
-
 			echo '<form method="post" action="options.php">';
 
 				settings_fields( $this->data['menu_slug'] );
 
 			echo '</form>';
 
+			if( WP_DEBUG || ( isset( $this->data['option_dump'] ) && 'true' == $this->data['option_dump'] ) ) {
+
+				$this->option_dump();
+
+			}
+
 		echo '</div>';
 
 	}
 
-	function pretty_option_dump() {
+	function option_dump() {
+
+		echo '<div class="postbox">';
 
-		echo '<pre>';
+			echo '<h2>Debug Information: Option Dump</h2>';
 
-			var_dump( get_option( $this->data['option_name'] ) );
+			echo '<div class="inside">';
 
-		echo '</pre>';
+				echo '<pre>';
+
+					var_dump( get_option( $this->data['option_name'] ) );
+
+				echo '</pre>';
+
+			echo '</div>';
+
+		echo '</div>';
 		
 	}
 
 
 	'submit_button_text'    =>    'Save Changes',
 
+	'option_dump'           =>    'true',
+
 );
 
 $settings_api_option = new settings_api_option( $data );
Updated by SlimBob WP

File settings-api-class.php Modified

  • Ignore whitespace
  • Hide word diff
 
 		add_action( 'admin_init', array( $this, 'delete_orphan_settings' ) );
 
-		add_action( 'admin_menu', array( $this, 'register_settings_menu_link' ) );
+		if( 'admin' == $this->data['admin_menu_action'] ) {
+
+			add_action( 'admin_menu', array( $this, 'register_settings_menu_link' ) );
+
+		}
+
+		if( 'user' == $this->data['admin_menu_action'] ) {
+
+			add_action( 'user_admin_menu', array( $this, 'register_settings_menu_link' ) );
+
+		}
+
+		if( 'network_admin' == $this->data['admin_menu_action'] ) {
+
+			add_action( 'network_admin_menu', array( $this, 'register_settings_menu_link' ) );
+			
+		}
 
 	}
 
 
 	function register_settings_menu_link() {
 
-		add_options_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
+		if( isset( $this->data['parent_slug'] ) && '' != $this->data['parent_slug'] ) {
+
+			add_submenu_page( $this->data['parent_slug'], $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
+
+		}
+
+		if( isset( $this->data['icon_url'] ) && '' != $this->data['icon_url'] ) {
+
+			add_menu_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ), $this->data['icon_url'], $this->data['position'] );
+
+		}
+
+		if( isset( $this->data['use_parent_menu'] ) && '' != $this->data['use_parent_menu'] ) {
+
+			switch( $this->data['use_parent_menu'] ) {
+
+				case "Dashboard":
+
+					add_dashboard_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
+
+					break;
+
+				case "Posts":
+
+					add_posts_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
+
+					break;
+
+				case "Media":
+
+					add_media_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
+
+					break;
+
+				case "Pages":
+
+					add_pages_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
+
+					break;
+
+				case "Comments":
+
+					add_comments_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
+
+					break;
+
+				case "Appearance":
+
+					add_theme_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
+
+					break;
+
+				case "Plugins":
+
+					add_plugins_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
+
+					break;
+
+				case "Users":
+
+					add_users_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
+
+					break;
+
+				case "Tools":
+
+					add_management_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
+
+					break;
+
+				case "Settings":
+
+					add_options_page( $this->data['page_title'], $this->data['menu_title'], $this->data['capability'], $this->data['menu_slug'], array( $this, 'output_admin_page' ) );
+
+					break;
+
+			}
+
+		}
 
 	}
 
 // Sample data array
 $data = array(
 
+	'admin_menu_action'     =>    'admin',
+
+	'use_parent_menu'       =>    'Settings',
+
+	'parent_slug'           =>    '',
+
+	'icon_url'              =>    '',
+
+	'position'              =>    '',
+
 	'page_title'            =>    'Settings API Test',
 
 	'menu_title'            =>    'Settings API Test',
 
 $settings_api_option = new settings_api_option( $data );
 
-// Need to add ability to add settings page as top level page, or as a submenu page for any core menu pages or custom post type menu pages.
-
 ?>
  1. 1
  2. 2
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.