Settings page Generator

Home » WordPress Generators » Settings page Generator
The title to be displayed on the menu item.
The slug to be used in URL for the menu item.
The icon to be displayed on the menu item.
The position of the menu item.
The parent menu item.
The required capability to access this settings page.
The title for this settings page.
The description for this settings page.
The name of the class to be used for the settings.
The type of field to create.
The name of the field.
The description of the field.
The ID of the field. Must be unique.
The options for the field. One per line
                    class WP_Turbo_My_Plugin_Settings {
	public function __construct() {
		// Register the settings page.
		add_action( 'admin_menu', array( $this, 'register_settings' ) );

		// Register the sections.
		add_action( 'admin_init', array( $this, 'register_sections' ) );

		// Register the fields.
		add_action( 'admin_init', array( $this, 'register_fields' ) );
	}

	// Register settings.
	public function register_settings(){
		add_menu_page(
			'Settings', // The title of your settings page.
			'My Plugin\'s Settings Page', // The name of the menu item.
			'manage_options', // The capability required for this menu to be displayed to the user.
			'my-plugin-settings', // The slug name to refer to this menu by (should be unique for this menu).
			array( $this, 'render_settings_page' ), // The callback function used to render the settings page.
			'dashicons-menu', // The icon to be used for this menu.
			2 // The position in the menu order this one should appear.
		);
	}

	// Register sections.
	public function register_sections(){
		add_settings_section( 'my-plugin-settings-section', '', array(), 'MyMenuName' );
	}

	// Register fields.
	public function register_fields(){
		$fields = array(
			'' => array(
				'section' => 'my-plugin-settings-section',
				'label' => '',
				'type' => 'text',
			),
		);
		foreach( $fields as $id => $field ){
			$field['id'] = $id;
			add_settings_field( $id, $field['label'], array( $this, 'render_field' ), 'my-plugin-settings-section', $field['section'], $field );
			register_setting( 'my-plugin-settings-section', $id );
		}
	}

	// Render individual fields.
	public function render_field( $field ){
		$value = get_option( $field['id'] );

		switch ( $field['type'] ) {
			case 'textarea':{
				echo "<textarea name='{$field['id']}' id='{$field['id']}'>$value</textarea>";
				break;
			}
			case 'checkbox':{
				echo "<input type='checkbox' name='{$field['id']}' id='{$field['id']}' " . ($value === '1' ? 'checked' : '') . " />";
				break;
			}
			case 'wysiwyg':{
				wp_editor($value, $field['id']);
				break;
			}
			case 'select':{
				if( is_array( $field['options'] ) && !empty( $field['options'] ) ) {
					echo "<select name='{$field['id']}' id='{$field['id']}'>";
					foreach( $field['options'] as $key => $option ){
						echo "<option value='$key' ". ($value === $key ? 'selected' : '') .">$option</option>";
					}
					echo "</select>";
				}
				break;
			}
			default:{
				echo "<input name='{$field['id']}' id='{$field['id']}' type='{$field['type']}'>$value</textarea>";
				break;
			}
		}

		if( isset($field['description']) ){
			echo "<p class='description'>{$field['description']}</p>";
		}
	}

	// Render the settings page.
	public function render_settings_page(){
		echo "<div class='wrap'>";
			echo "<h1>Settings</h1>";
			settings_errors();
			echo "<form method='POST' action='options.php'>";
				settings_fields( 'my-plugin-settings' );
				do_settings_sections( 'my-plugin-settings' );
				submit_button();
			echo "</form>";
		echo "</div>";
	}


}