WordPress Options Page Generator

This is a simple web application used to make an options page for your WordPress theme or plugin. It provides the PHP code that you can copy paste into your theme functions or plugin base files. I wrote this for myself to cut down on the number of times I have to repeat work so I decided to share it.

Try It Out

Screenshot of the WordPress option  page generator

The first section asks for a “Title” and “Text”. The title is required because it’s used to generate the prefix for the functions as well as name the class and provide the menu item and title for the page itself. So, yea, required. The text is just in case you need to say something before you get down to the fields.

Screenshot of the WordPress option  page generator

The next section determines the attributes of our menu item. Whether it’s top-level or a child of another menu item, the icon used if you’re creating a top-level link and where at on the admin menu it appears (again, if it’s top-level).

Screenshot of the WordPress option  page generator

The last section is where you generate the fields themselves. Choose the type of field, provide a label and, for some of the input types, additional data respective of that input type. You can add more with the “+” button and remove any extra ones with the “x”.

Here’s a sample of the output:

class SampleOptionPageTitle {
	private $sample_option_page_title_options;

	public function __construct() {
		add_action( 'admin_menu', array( $this, 'sample_option_page_title_add_plugin_page' ) );
		add_action( 'admin_init', array( $this, 'sample_option_page_title_page_init' ) );
	}

	public function sample_option_page_title_add_plugin_page() {
		add_menu_page(
			'Sample Option Page Title', // page_title
			'Sample Option Page Title', // menu_title
			'manage_options', // capability
			'sample-option-page-title', // menu_slug
			array( $this, 'sample_option_page_title_create_admin_page' ), // function
			'dashicons-share', // icon_url
			26 // position
		);
	}

	public function sample_option_page_title_create_admin_page() {
		$this->sample_option_page_title_options = get_option( 'sample_option_page_title_option_name' ); ?>

		<div class="wrap">
			<h2>Sample Option Page Title</h2>
			<p>Some text below the title. Instructions maybe.</p>
			<?php settings_errors(); ?>

			<form method="post" action="options.php">
				<?php
					settings_fields( 'sample_option_page_title_option_group' );
					do_settings_sections( 'sample-option-page-title-admin' );
					submit_button();
				?>
			</form>
		</div>
	<?php }

	public function sample_option_page_title_page_init() {
		register_setting(
			'sample_option_page_title_option_group', // option_group
			'sample_option_page_title_option_name', // option_name
			array( $this, 'sample_option_page_title_sanitize' ) // sanitize_callback
		);

		add_settings_section(
			'sample_option_page_title_setting_section', // id
			'Settings', // title
			array( $this, 'sample_option_page_title_section_info' ), // callback
			'sample-option-page-title-admin' // page
		);

		add_settings_field(
			'text_field_0', // id
			'Text Field', // title
			array( $this, 'text_field_0_callback' ), // callback
			'sample-option-page-title-admin', // page
			'sample_option_page_title_setting_section' // section
		);

		add_settings_field(
			'select_field_1', // id
			'Select Field', // title
			array( $this, 'select_field_1_callback' ), // callback
			'sample-option-page-title-admin', // page
			'sample_option_page_title_setting_section' // section
		);
	}

	public function sample_option_page_title_sanitize($input) {
		$sanitary_values = array();
		if ( isset( $input['text_field_0'] ) ) {
			$sanitary_values['text_field_0'] = sanitize_text_field( $input['text_field_0'] );
		}

		if ( isset( $input['select_field_1'] ) ) {
			$sanitary_values['select_field_1'] = $input['select_field_1'];
		}

		return $sanitary_values;
	}

	public function sample_option_page_title_section_info() {
		
	}

	public function text_field_0_callback() {
		printf(
			'<input class="regular-text" type="text" name="sample_option_page_title_option_name[text_field_0]" id="text_field_0" value="%s">',
			isset( $this->sample_option_page_title_options['text_field_0'] ) ? esc_attr( $this->sample_option_page_title_options['text_field_0']) : ''
		);
	}

	public function select_field_1_callback() {
		?> <select name="sample_option_page_title_option_name[select_field_1]" id="select_field_1">
			<?php $selected = (isset( $this->sample_option_page_title_options['select_field_1'] ) && $this->sample_option_page_title_options['select_field_1'] === 'Option One') ? 'selected' : '' ; ?>
			<option <?php echo $selected; ?>>Option One</option>
			<?php $selected = (isset( $this->sample_option_page_title_options['select_field_1'] ) && $this->sample_option_page_title_options['select_field_1'] === 'option-2') ? 'selected' : '' ; ?>
			<option value="option-2" <?php echo $selected; ?>>Option Two</value>
		</select> <?php
	}

}
if ( is_admin() )
	$sample_option_page_title = new SampleOptionPageTitle();

/* 
 * Retrieve this value with:
 * $sample_option_page_title_options = get_option( 'sample_option_page_title_option_name' ); // Array of All Options
 * $text_field_0 = $sample_option_page_title_options['text_field_0']; // Text Field
 * $select_field_1 = $sample_option_page_title_options['select_field_1']; // Select Field
 */

Share

Comments


Notice: ob_end_flush(): failed to send buffer of zlib output compression (0) in /home/jhixon/public_html/wp-includes/functions.php on line 3722