Skip to content

Instantly share code, notes, and snippets.

@Sidsector9
Last active August 26, 2018 18:53
Show Gist options
  • Save Sidsector9/eb0415280301e3a2c724338794f73342 to your computer and use it in GitHub Desktop.
Save Sidsector9/eb0415280301e3a2c724338794f73342 to your computer and use it in GitHub Desktop.
<?php
/**
* Adding a settings field in the already existing settings
* page: general.
*/
add_action( 'admin_init', function() {
/**
* To create a settings field, it needs to be registered first.
* Here we register a settings field called 'brand-name'.
*
* This 'brand-name' will be also the key in the options table
* and it should also be used as the name of the form field.
*/
register_setting( 'general', 'brand-name' );
/**
* Here we add a settings field after registering it above.
*/
add_settings_field(
'brand-name',
'Brand Name',
'render_radio_for_brand_name',
'general'
);
});
/**
* This will output the radio field.
*/
function render_radio_for_brand_name() {
$brand_name = get_option( 'brand-name' );
printf(
'<p><label for="brand-name-yes"><input type="checkbox" name="brand-name[option-yes]" id="brand-name-yes" value="yes" %s/>Yes</label></p>',
checked( $brand_name['option-yes'], 'yes', false )
);
printf(
'<p><label for="brand-name-no"><input type="checkbox" name="brand-name[option-no]" id="brand-name-no" value="no" %s/>No</label></p>',
checked( $brand_name['option-no'], 'no', false )
);
printf(
'<p><label for="brand-name-maybe"><input type="checkbox" name="brand-name[option-maybe]" id="brand-name-maybe" value="maybe" %s/>Maybe</label></p>',
checked( $brand_name['option-maybe'], 'maybe', false )
);
}
<?php
/**
* Adding a settings field in the already existing settings
* page: general.
*/
add_action( 'admin_init', function() {
/**
* To create a settings field, it needs to be registered first.
* Here we register a settings field called 'brand-name'.
*
* This 'brand-name' will be also the key in the options table
* and it should also be used as the name of the form field.
*/
register_setting( 'general', 'brand-name' );
/**
* Here we add a settings field after registering it above.
*/
add_settings_field(
'brand-name',
'Brand Name',
'render_radio_for_brand_name',
'general'
);
});
/**
* This will output the radio field.
*/
function render_radio_for_brand_name() {
$brand_name = get_option( 'brand-name' );
printf(
'<p><label for="brand-name-yes"><input type="radio" name="brand-name" id="brand-name-yes" value="yes" %s/>Yes</label></p>',
checked( $brand_name, 'yes', false )
);
printf(
'<p><label for="brand-name-no"><input type="radio" name="brand-name" id="brand-name-no" value="no" %s/>No</label></p>',
checked( $brand_name, 'no', false )
);
printf(
'<p><label for="brand-name-maybe"><input type="radio" name="brand-name" id="brand-name-maybe" value="maybe" %s/>Maybe</label></p>',
checked( $brand_name, 'maybe', false )
);
}
<?php
/**
* Adding a settings field in the already existing settings
* page: general.
*/
add_action( 'admin_init', function() {
/**
* To create a settings field, it needs to be registered first.
* Here we register a settings field called 'brand-name'.
*
* This 'brand-name' will be also the key in the options table
* and it should also be used as the name of the form field.
*/
register_setting( 'general', 'brand-name' );
/**
* Here we add a settings field after registering it above.
*/
add_settings_field(
'brand-name',
'Brand Name',
'render_text_input_for_brand_name',
'general'
);
});
/**
* This will output the input field.
*/
function render_text_input_for_brand_name() {
$brand_name = get_option( 'brand-name' );
printf(
'<input type="text" name="brand-name" id="brand-name" value="%s"/>',
$brand_name
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment