1

I have added a setting field in the general settings page. The field and the validation work, but the validation error message does not show.

<?php
add_action( 'admin_init', 'initialize_extra_settings' );
function initialize_extra_settings() {
  register_setting( 
    'general', 
    'setting_app_store_app_url', 
    array (
      'type' => 'string',
      'sanitize_callback' => 'settings_url_field_validation'
    ) 
  );
  add_settings_field(
    'app-store-app-url-field', // Field slug
    'App Store app URL',
    'app_store_app_url_field_cb',
    'general', // In this settings page (slug)
    'default', // In this section (slug)
    array(
      'label_for' => 'app-store-app-url-field',
      'class' => 'custom-settings-row'
    )
  );
}

function app_store_app_url_field_cb( $args) {
  $setting = get_option('setting_app_store_app_url'); 
  ?>
    <input 
      id="<?php echo $args['label_for'];?>" 
      name="setting_app_store_app_url" 
      type="url" 
      value="<?php echo isset( $setting ) ? esc_attr( $setting ) : ''; ?>"  
    />
  <?php
}

function settings_url_field_validation( $value ) {
  $urlRegExp = "/https:\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])/";
  if ( !preg_match($urlRegExp, $value) ) {
    add_settings_error( 'setting_app_store_app_url', 'invalid_app_store_app_url', 'Must be a valid url', 'error' );
    return null;
  }
  return $value;
}

add_action( 'admin_notices', 'show_custom_settings_admin_notices' );
function show_custom_settings_admin_notices() {
  var_dump(get_settings_errors('setting_app_store_app_url'));
  settings_errors('setting_app_store_app_url');
}

The var_dump(get_settings_errors('setting_app_store_app_url')); returns an empty array.

4
  • Where did you see the array being empty? If you saw it on the "General" options page after submitting the form with an empty/invalid "App Store app URL" value, then it's strange if you get an empty array. And actually, you don't need to manually call settings_errors() because it is automatically called on that admin page (wp-admin → Settings → General), hence any validation errors would appear at the top of the page without one having to manually show them.
    – Sally CJ
    Mar 17 at 3:15
  • I see the array being empty in the General options after submitting the form with the invalid field. Actually, I have noticed that no error messages are displayed from any field. For example, if I leave empty the Administration Email Address field and submit, it returns the previous value but no error messages. Also, I have removed the call to settings_errors() but nothing changes.
    – jamomani
    Mar 17 at 4:18
  • 1
    Have you tried deactivating all plugins and/or switching to a default theme? Try doing so if you haven't already and see if the same issue persists. Also, try checking the raw/server-generated HTML source (Ctrl+u on Chrome+Windows desktop) - do the error messages really not available anywhere in the source?
    – Sally CJ
    Mar 17 at 4:47
  • 1
    I had checked the html source but the error message was not in there. I am developing locally with Local, so I also tested in the online version, where it worked. I deactivated many plugins and then it started working also locally. After reactivating all the plugins... it now still works, so I do not know. I might have also stopped the site and started it again, which maybe helped, I am not sure.
    – jamomani
    Mar 17 at 20:32

1 Answer 1

0

I was developing locally with Local, so I tested in the online version, where it worked. I then deactivated most of the plug-ins locally, and it started to work. I might have also stopped the site and run it again, I am not sure, but it might have helped? Anyway, after reactivating all the same plug-ins, it kept working, so I guess that some of the actions above have helped. Here it the complete working solution.

<?php
//... The content of functions.php until here

add_action( 'admin_init', 'initialize_extra_settings' );
function initialize_extra_settings( ) {
  register_setting( 'general', 'setting_app_store_app_url', array(
     'type' => 'string',
    'sanitize_callback' => function( $value ) {
      return settings_url_field_validation( $value, 'setting_app_store_app_url', 'App store app url' );
    } 
  ) );
  register_setting( 'general', 'setting_android_app_url', array(
     'type' => 'string',
    'sanitize_callback' => function( $value ) {
      return settings_url_field_validation( $value, 'setting_android_app_url', 'Android app url' );
    } 
  ) );
  // Register new fields in the sections
  add_settings_field( 'app-store-app-url-field', // Field slug
    'App Store app URL', 'app_store_app_url_field_cb', 'general', // In this settings page (slug)
    'default', // In this section (slug)
    array(
     'label_for' => 'app-store-app-url-field',
    'class' => 'custom-settings-row' 
  ) );
  add_settings_field( 'android-app-url-field', // Field slug
    'Android app URL', 'android_app_url_field_cb', 'general', // In this settings page (slug)
    'default', // In this section (slug)
    array(
     'label_for' => 'android-app-url-field',
    'class' => 'custom-settings-row' 
  ) );
}

function app_store_app_url_field_cb( $args ) {
  $setting = get_option( 'setting_app_store_app_url' );
?>
   <input 
        id="<?php echo $args['label_for']; ?>" 
        name="setting_app_store_app_url" 
        type="url" 
        value="<?php echo isset( $setting ) ? esc_attr( $setting ) : ''; ?>"  
    />
    <?php
}

function android_app_url_field_cb( $args ) {
  $setting = get_option( 'setting_android_app_url' ); // The name used in the register_setting call
?>
   <input 
        id="<?php echo $args['label_for']; ?>" 
        name="setting_android_app_url" 
        type="url"
        value="<?php echo isset( $setting ) ? esc_attr( $setting ) : ''; ?>"  
    />
    <?php
}

function settings_url_field_validation( $value, $field, $name ) {
  $urlRegExp = "/https:\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])/";
  if ( !preg_match( $urlRegExp, $value ) ) {
    $value = get_option( $field ); // ignore the user's changes and use the old database value
    add_settings_error( $field, $field, $name . ' must be a valid url', 'error' );
    return $value;
  }
  return $value;
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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