0

I'm creating a plugin and I am a quite newbie. So I have difficulty to properly validate and sanitize input fields.

This is my code:

register_setting( 'wp_banner_settings_fields', 'wp_banner_settings_fields', 'wp_banner_sanitize' );
add_settings_section( 'wp_banner_id', __( 'WP Banner Management', 'wp_banner' ), array( $this, 'wp_banner_setting_section'), 'wp_banner_settings_sections' );

add_settings_field( 'wp_banner_id_turn_on', __( 'Enable/Disable banner', 'wp_banner' ), array( $this, 'wp_banner_field_turn_on' ), 'wp_banner_settings_sections', 'wp_banner_id' );
add_settings_field( 'wp_banner_id_text', __( 'Banner Des', 'wp_banner' ), array( $this, 'wp_banner_field_text'), 'wp_banner_settings_sections', 'wp_banner_id' );
add_settings_field( 'wp_banner_id_exclude', __( 'Exclude Pages ( comma separated )', 'wp_banner' ), array( $this, 'wp_banner_field_exclude'), 'wp_banner_settings_sections', 'wp_banner_id' );

public function wp_banner_field_turn_on()
{
    $options = get_option( 'wp_banner_settings_fields' );
    $is_options_empty = ( ! empty( $options[ 'turn_on' ] ) ? $options[ 'turn_on' ] : '' );

    echo '<label class="wp-banner-switch" for="wp_banner_id_turn_on">';
    echo '<input type="checkbox" id="wp_banner_id_turn_on" class="wp-banner-switch-input wp-banner-field-size" name="wp_banner_settings_fields[turn_on]" value="1"' . checked( 1, $is_options_empty, false ) . '/>';
    echo '<span class="wp-banner-slider wp-banner-round"></span>';
    echo '</label>';
}

public function wp_banner_field_text()
{
    $options = get_option( 'wp_banner_settings_fields' );
    $is_options_empty = ( ! empty( $options[ 'text' ] ) ? $options[ 'text' ] : '' );

    echo '<textarea id="wp_banner_id_text" name="wp_banner_settings_fields[text]" placeholder="' . __( 'My cool description for the banner', 'wp_banner' ) . '" rows="10" cols="100">' . esc_attr( sanitize_text_field( $is_options_empty ) ) . '</textarea>';
}

public function wp_banner_field_exclude()
{
    $options = get_option( 'wp_banner_settings_fields' );
    $is_options_empty = ( ! empty( $options[ 'exclude' ] ) ? $options[ 'exclude' ] : '' );

    echo '<input type="text" id="wp_banner_id_exclude" name="wp_banner_settings_fields[exclude]" class="wp-banner-field-size" value="' . esc_attr( sanitize_text_field( $is_options_empty ) ) . '" placeholder="page-five, page-six">';
}

So if someone can give me an example of how can I sanitize the fields above, I will be much then grateful.

2
  • Read the docs? – vancoder Sep 16 '20 at 19:57
  • @vancoder Yes. Can you provide me one example with my fields? – upss1988 Sep 16 '20 at 22:05

Your Answer

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

Browse other questions tagged or ask your own question.