Modify the checked property of the checkbox when using register_setting

When developing WordPress plug-ins, sometimes a checkbox based confirmation option is provided in the background. How to take values and update the checked attribute when using the register_setting stored value provided officially?

text

example

This is a simple example of plug-in background setting panel:

 <? php function RegMenu(){ add_menu_page( "Page Title", # Page Title "Menu Title", # Menu Title "Administrator", # Who can see "My test setting", # Set page alias (url access) "Output_settingpage", # Output function "dashicons-heart" #icon url ); Add_submenu_page (# Add submenu "My test setting", # parent menu alias "Page Title", # Page Title "Sub menu title", # menu title "administrator", "My test setting1", # Page alias "Output_settingpage1" # Output function ); add_action('admin_init', 'reg_custom_settings'); } fcunction reg_custom_settings(){ register_setting('test_group','zm_test_isopen'); } function output_settingpage(){ ?> <form method="post" action="options.php"> <? php settings_fields('test_group'); ?> <input type="checkbox" name="zm_test_isopen" value="1" <? php checked( '1', get_option( 'zm_test_isopen' ), true ); ?>/>Test</label> <? php submit_button(); ?> </form> <? php } function output_settingpage1(){ } ?>

Among them, checked( '1', get_option( 'zm_anti_copy_open' ),true ) The setting elements provided for WordPress checked Property.

WordPress checked details

First, let's take a look at the definition of the checked () function:

 //Outputs the html checked attribute. checked( mixed $checked, # One of the values to compare mixed $current = true, #  The other value to compare if not just true bool $echo = true # Whether to echo or just return the string )

The following two methods are equivalent:

 <input type='checkbox' name='zm_test_isopen' value='1' <? php if ( 1 == $options['zm_test_isopen'] ) echo 'checked="checked"'; ?> />
 <input type="checkbox" name="zm_test_isopen" value="1" <? php checked("1",$options['zm_test_isopen'], 1 ); ?> />

When submitting an Html form, if checkbox is not checked, there will be no sub item in the submission. If checkbox is checked, the submitted form will have a sub item corresponding to name, whose value is value Value.

The checked () function will compare the $checked value with the $current value. If they are the same, it is considered that this item is checked, and the output checked

Zimiao haunting blog (azimiao. com) All rights reserved. Please note the link when reprinting: https://www.azimiao.com/6261.html
Welcome to the Zimiao haunting blog exchange group: three hundred and thirteen million seven hundred and thirty-two thousand

Comment

*

*