0

How to do sanitization at register_setting() ?

I am learning Plugin Development. I am using below code.

public function settings_page_registration() {
    register_setting(
        'sports_info_api_key',
        'wp_api_key',
        array(
            'type' => 'string',
            'sanitize_callback' => 'sanitize_text_field',
        )
    ); 
}

How can I move forward ? What should I write inside sanitize_text_field function if I would like to receive only text value ?

1 Answer 1

2

You can use sanitize_text_field exactly as you are. From the function's documentation:

  • Checks for invalid UTF-8,
  • Converts single < characters to entities
  • Strips all tags
  • Removes line breaks, tabs, and extra whitespace
  • Strips percent-encoded characters

sanitize_text_field() is already defined as a function in WordPress, so you don't need to change anything. The sanitize_callback parameter takes a function name; only if you want to do custom sanitization would you need to define a new function.

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.