0

I'm creating a custom settings page for a custom theme however the callbacks for sanatizing the data in the fields are not being called (I put in xdebug breakpoints in them to check).

/**
*------------------------- Admin Page Titles -------------------------
*/

function header_options_text() {
    echo '<p>These options affect the Header content.</p>';
}

/**
*------------------------- Header Options -------------------------
*/

function phone_number_input() {
    $option = get_option('header_phone');
    echo '<input id="header_phone" name="header_phone" size="40" type="text" value="'.$option.'"> </input>';
}
function header_logo_input() {
    $option = get_option('header_logo');
    echo '<input id="header_logo" name="header_logo" size="40" type="text" value="'.$option.'"> </input>';
}

/**
*------------------------- Header Validation Options -------------------------
*/

function header_phone_validate($input) {
    $new_input=trim($input);
    if(get_option( 'header_phone' ) === $new_input || !preg_match('/^[\+\d\(\)\- ]*$/', $new_input))
        $new_input=get_option( 'header_phone' );
    return $new_input;
}
function header_logo_validate($input) {
    $new_input=esc_url_raw(trim($input));
    if(get_option( 'header_logo' ) === $new_input && !filter_var($new_input, FILTER_VALIDATE_URL))
        $new_input=get_option( 'header_logo' );
    return $input;
}

/**
 *------------------------- Actually create the pages and add to menu -------------------------
 */

add_action( 'admin_menu', 'ec1_admin', 10, 0 );
function ec1_admin() {
    add_menu_page( 'EC1 Theme Options', 'EC1 Theme Options', 'manage_options', 'ec1-theme-options', 'ec1_theme_options_page' );
}

function ec1_theme_options_page() {
    ?>
    <div>
        <h2>EC1 Theme Options</h2>
        <form action="options.php" method="post">
            <?php settings_fields('header-options'); ?>
            <?php settings_fields('header-logo-options'); ?>
            <?php do_settings_sections( 'ec1-theme-options' ); ?>
            <input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" />
        </form>
    </div>
    <?php
}


add_action('admin_init', 'plugin_admin_init');
function plugin_admin_init(){
    register_setting( 'header-options', 'header_phone', 'header_phone_validate' );
    register_setting( 'header-logo-options', 'header_logo', 'header_logo_validate' );
    add_settings_section('header-options-pane', 'Header Options', 'header_options_text', 'ec1-theme-options');
    add_settings_field('header_phone', 'Phone Number', 'phone_number_input', 'ec1-theme-options', 'header-options-pane');
    add_settings_field('header_logo', 'Header Logo', 'header_logo_input', 'ec1-theme-options', 'header-options-pane');
}
?>

1 Answer 1

0

It's because you have typos in your code :

in the function called header_logo_validate() it should be :

return $new_input;

EDIT:

add_action( 'admin_menu', 'ec1_admin', 10, 0 ); 

args are useless just write add_action( 'admin_menu', 'ec1_admin');

EDIT 2 :

This is not a correct way to add several fields. You should put this into arrays or in one callback.

Maybe you need to globalize $settings (write global $settings;) to make esc_url() work in this context, see this answer

5
  • While this is true, a breakpoint on $new_input=esc_url_raw(trim($input)); is not triggering with xdebug, showing the function isn't even running!
    – ewanm89
    Commented Jul 9, 2013 at 15:38
  • The other validation does not have this typo and has the same issue.
    – ewanm89
    Commented Jul 9, 2013 at 15:38
  • why do you use esc_url_raw() and not esc_url()? esc_url_raw does not replace entities for display
    – JMau
    Commented Jul 9, 2013 at 15:52
  • I wasn't sure if I wanted to replace entities, but still, it's not even entering the function, breakpoints halt execution at the beginning of the line of code, not the end.
    – ewanm89
    Commented Jul 9, 2013 at 16:14
  • I've edited my answer hoep this helps. I think you should rebuild your code
    – JMau
    Commented Jul 9, 2013 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.