Build an admin page in the backend of WordPress

With the release of Premise WP 2.0.0 building an admin page in WordPress is easier than ever!

All we have to do is instantiate the PWP_Admin_Page class and boom, we have a working admin page, and I mean working! Fields save and retrieve values, security nonces are taken care of and all the right hooks are set in place.

 

Parameters:

$title – string|array Optional. Can be a string with the title to use for the page or an array with all the parameters you can pass to add_menu_page().

$fields – array Optional. Array of fields to display. Same as array you would pass to pwp_form.

$optiona_names – string|array Optional. Can be a string with an option name to register or array with multiple names.

 

Example

Lets say we want to add an options page for a new theme. On this options page we want the user to be able to change the logo for the theme as well as other global information.

Once we have Premise WP installed on our site. We simply go to our theme’s functions.php file and add the following lines of code:

/**
 * First we create the fields that we want to add to our page
 * 
 * @var array
 */
$theme_options = array(
	'action'      => 'options.php',
	'name_prefix' => 'our_options_name', // notice that by placing the 'name' attribute here we avoid having to do it for each field
	array(
		'type' => 'wp_media',
		'name' => '[theme-logo]',
		'label' => 'Logo',
		'preview' => true,
	),
	array(
		'type' => 'select',
		'name' => '[theme-layout]',
		'label' => 'Layout',
		'options' => array(
			'Boxed' => 'boxed',
			'Full Width' => 'full-width',
		),
	),
	array(
		'type' => 'textarea',
		'name' => '[theme-copyright]',
		'label' => 'Copyright',
	),
	array( 'type' => 'submit' ),
);

/**
 * Title: Theme Options
 * Fields: $theme_options
 * Option Names: same as name attribute for fields
 */
new PWP_Admin_Page( 'Theme Options', $theme_options, $theme_options['name_prefix'] );

 

The code above produces the following results in the admin side of WordPress. All the fields save data, there is nothing else needed to do!

 

View the source code in GitHub!

Send us your comments..

Your email address will not be published. Required fields are marked *