Save a plugin setting with a sanitize callback
Description: Register an option site_slogan in a plugin, which cleans the value with sanitize_text_field. Save the value <b>Learn</b> fast and print what is stored.
Hint:
register_setting()withsanitize_callbackruns a filter every timeupdate_option()saves the option.
Solution
register_setting( 'my_group', 'site_slogan', array(
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
) );
update_option( 'site_slogan', '<b>Learn</b> fast' );
var_dump( get_option( 'site_slogan' ) );
Output
string(10) "Learn fast"
How it works
register_setting()connects the sanitize function to the option name.- The tags are removed before the value goes to the database, so the stored value is plain text.
- In a real plugin you call
register_setting()on theadmin_inithook.
Read Plugin Settings Page.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |