So I have a plugin with two user-customisable settings. (It’s not complete but I’m getting the framework in place.) When the two settings are on the “general” settings page they work just fine…
add_action('admin_init', 'mj_admin_init');
function mj_admin_init(){
register_setting('general', 'mj_options', 'mj_validate_input');
add_settings_section('mjid', 'MJ Settings', 'mj_callback', 'general');
add_settings_field('mj_notify_email', 'Email', 'mj_email_input', 'general', 'mjid');
add_settings_field('mj_cm_enabled', 'Enable Messages', 'mj_cm_enabled_input', 'general', 'mjid');
}
add_action('admin_menu', 'mj_admin_menu');
function mj_admin_menu(){
do_settings_sections('mjid');
}
function mj_admin(){
// to-do later
}
function mj_callback(){
echo '<p>MJ Callback function produced this</p>';
}
function mj_email_input(){
$options = get_option('mj_options');
$value = $options['mj_email'];
echo '<input id="mj_email" name="mj_options[mj_email]" type="text" value="'.esc_attr($value).'" /> <em>Email address</em>';
}
function mj_cm_enabled_input(){
$options = get_option('mj_options');
$value = $options['cm-enabled'];
echo '<input id="cm-enabled" name="mj_options[cm-enabled]" type="checkbox" value=1 '.checked(1, esc_attr($value), false).' /> <em>Checkbox for enabled messages</em>';
}
function mj_validate_input($input){
$valid = array();
$valid['mj_email'] = sanitize_email( $input['mj_email']);
if($valid['mj_email'] != $input['mj_email']) {
add_settings_error('mj_email_setting', 'mj_texterror', 'Invalid email', 'error');
}
$valid['cm-enabled'] = intval($input['cm-enabled']);
return $valid;
}
But I want to put them on a separate submenu page, called “mj-admin-settings”. But, try as I might, they won’t show up. All I get is the output of the “mj_callback” function. There are no errors in the error logs, even with debug set to true.
To do the above I changed the mj_admin_init
and mj_admin_menu
functions to the following:
add_action('admin_init', 'mj_admin_init');
function mj_admin_init(){
register_setting('mj-admin-settings', 'mj_options', 'mj_validate_input');
add_settings_section('mjid', 'MJ Settings', 'mj_callback', 'mj-admin-settings');
add_settings_field('mj_notify_email', 'Email', 'mj_email_input', 'mj-admin-settings', 'mjid');
add_settings_field('mj_cm_enabled', 'Enable Messages', 'mj_cm_enabled_input', 'mj-admin-settings', 'mjid');
}
add_action('admin_menu', 'mj_admin_menu');
function mj_admin_menu(){
add_menu_page('MJ Admin', 'MJ Admin', 'manage_options', 'mj-admin-options', 'mj_admin');
add_submenu_page('mj-admin-options', 'MJ Admin - Settings', 'Settings', 'manage_options', 'mj-admin-settings', 'mj_callback');
do_settings_sections('mjid');
}
Does anyone have any ideas as to how I get the settings to display in the submenu please? I’ve been all over the codex and am stumped!
If you check the arguments for add_menu_page, you’ll see that the fifth argument is your callback function. And, right now, there’s nothing in the mj_admin function.
What you need to do is output your form in that function.
Don’t know if is this the problem but when i add a sub menu page i do it in this way:
Type the slug with his php extension 😉