samedi 28 février 2015

Wordpress - Functions Custom Admin Page

Simplified question.


Below is a simple functions page which displays two custom wordpress dashboard menus. One main menu and a submenu.


As you can see both pages (main and sub) have an input, one for name and one for email.


Question as follows 1. Is there a way to simplify so i dont have to register 2 settings, 1 for each page? 2. What is the best way to show this frontend on the theme.



<?php

/* Add Page */

add_action('admin_menu', 'settings' );
add_action('admin_init', 'register_settings_page');
add_action('admin_init', 'register_settings_sub_page');

/* Add to WP admin menu */

function settings() {
add_menu_page( 'Settings Page', 'Settings Page', 'manage_options', 'settings- page', 'settings_page');
add_submenu_page( 'settings-page', 'Settings Page Sub', 'Settings Page Sub', 'manage_options', 'settings-page-sub', 'settings_page_sub');

/* Register Settings */

function register_settings_page() {
register_setting('settings-page-reg', 'settings_name');
}

function register_settings_sub_page() {
register_setting('settings-sub-page-reg', 'settings_email');
}

/*Add Content for Pages */

function settings_page() {
?>
<form method="post" action="options.php">
<?php settings_fields('settings-page-reg'); ?>
<label>Settings Page Input</label>
<input name="settings_name" type="test" value="<?php echo get_option('settings_name'); ?>" />
<input type="submit" value="<?php _e('Save') ?>" />
</form>
<?php
}

function settings_page_sub() {
?>
<form method="post" action="options.php">
<?php settings_fields('settings-sub-page-reg'); ?>
<label>Settings Sub Page Input</label>
<input name="settings_email" type="test" value="<?php echo get_option('settings_email'); ?>" />
<input type="submit" value="<?php _e('Save') ?>" />
</form>
<?php
}
}
?>

<html>
<body>
<h1>I want name here</h1>
<p>I want email here</p>
</body>
<html>

Aucun commentaire:

Enregistrer un commentaire