Easily create plugin/theme settings page, custom fields metaboxes and term meta, and user meta settings.
This plugin is my attempt to simplify the process of adding a setting page for the WordPress themes I was working on. It was originally inspired by the Hybrid theme. I then re-wrote it to utilize the amazing WordPress' Settings API after reading Otto's article.
Currently you can use this plugin for these types of settings:
For plugin/theme setting pages, you have the option to choose between 'plain' and 'metaboxes' display types. For post metaboxes/custom fields and plugin/theme setting pages with display time set to metaboxes, you can freely set the metaboxes positions/contexts and priorities.
Supported input types:
texttextareaemailcolordate (not supported in theme customizer and menu item metadata)month (not supported in theme customizer and menu item metadata)time (no polyfill yet, not supported in theme customizer and menu item metadata)datetime (no polyfill yet, not supported in theme customizer and menu item metadata)datetime-local (no polyfill yet, not supported in theme customizer and menu item metadata)week (no polyfill yet, not supported in theme customizer and menu item metadata)radiocheckbox (not supported in theme customizer)selectmultiselect (not supported in theme customizer)numberpasswordtelfile (not supported in theme customizer and menu item metadata)multiinput (not supported in theme customizer, menu and menu item metadata)editor (not supported in theme customizer, menu and menu item metadata)special (not supported in theme customizer)If you think there are other input types that need to be added, don't hesitate to ask!
Just install it like any other WordPress plugins :)
Copy kc-settings.php and kc-settings-inc to your wp-content/mu-plugins directory (don't just copy the kc-settings directory).
If you want to bundle KC Settings with your theme/plugin, follow these steps:
kc-settings.php and kc-settings-inc to your theme/plugin directory (don't just copy the kc-settings directory).function.php file:
function mytheme_load_kc_settings() {
// Load the settings array
require_once get_template_directory() . '/my-settings.php';
if ( class_exists('kcSettings') )
return;
// This is to avoid the conflict when KC Settings is bundled in your theme/plugin and installed as plugin
if ( is_admin() ) {
global $pagenow;
if (
$pagenow === 'plugins.php'
&& (
// Single plugin activation
( isset($_REQUEST['action']) && $_REQUEST['action'] === 'activate' && isset($_REQUEST['plugin']) && $_REQUEST['plugin'] === 'kc-settings/kc-settings.php' )
|| (
// Bulk activation
!empty($_POST)
&& ( ( isset($_POST['action']) && $_POST['action'] === 'activate-selected' ) || ( isset($_POST['action2']) && $_POST['action2'] === 'activate-selected' ) )
&& ( isset($_POST['checked']) && is_array($_POST['checked']) && in_array('kc-settings/kc-settings.php', $_POST['checked']) )
)
)
)
return;
}
// Use get_stylesheet_directory() if you're doing this in a child theme
require_once get_template_directory() . '/kc-settings.php';
kcSettings::setup();
}
add_action( 'after_setup_theme', 'mytheme_load_kc_settings' );
Note: The action hook should always be after_setup_theme even if you're bundling KC Settings in a plugin.
If you only need the theme customizer module, copy the theme.php file from the kc-settings/kc-settings-inc/ to your theme's directory then add this block of code to your theme's function.php file:
function mytheme_load_kc_customizer() {
// Load the settings array
require_once get_template_directory() . '/mytheme-settings.php';
if ( class_exists('kcSettings') )
return;
// Use get_stylesheet_directory() if you're doing this in a child theme
require_once get_template_directory() . '/theme.php';
}
add_action( 'after_setup_theme', 'mytheme_load_kc_customizer', 99 );
Note: You need to make sure that your options array is valid, because the module won't do any validation. Also, I strongly suggest to rename the theme.php file to something else to avoid confusion :)
You can either create the settings using the Builder by going to Settings » KC Settings, or create them manually. Note that theme customizer is not available in the Builder.
There are a few sample options files included in the sample directory. Copy (examine and modify) the sample options files included in the sample directory to your plugin/theme directory and then include them from your theme/plugin, eg. require_once get_template_directory() . '/theme_options.php';
$prefix = 'myPrefix';
$var = kc_get_option( $prefix );
// Or, you can also use WordPress' builtin function:
$var = get_option( "{$prefix}_settings" );
$prefix = 'myPrefix';
$section_id = 'section_one';
$var = kc_get_option( $prefix, $section_id );
$prefix = 'myPrefix';
$section_id = 'section_one';
$field_id = 'field_one';
$var = kc_get_option( $prefix, $section_id, $field_id );
If you have set a default value for your field, you can get it like this:
$prefix = 'myPrefix';
$section_id = 'section_one';
$field_id = 'field_one';
$var = kc_get_default( 'plugin', $prefix, $section_id, $field_id );
Just use standard WordPress function, but using the prefix, section ID, and field ID as the first argument, for example:
$prefix = 'myPrefix';
$section_id = 'section_one';
$field_id = 'field_one';
$var = get_theme_mod( "{$prefix}_{$section_id}_{$field_id}" ); // Notice the underscores
If you have set a default value for the field and want to get it when the setting hasn't been changed:
$var = get_theme_mod( "{$prefix}_{$section_id}_{$field_id}", kc_get_default('theme', $prefix, $section_id, $field_id) );
Just use standard WordPress function, but prefix the meta key with an underscore, for example:
$post_id = 1;
$field_id = 'field_one';
$var = get_post_meta( $post_id, "_{$field_id}", true ); // Notice the underscore
You can use standard WordPress function, for example:
$term_id = 1;
$field_id = 'field_one';
$var = get_metadata( 'term', $term_id, $field_id, true );
You can use standard WordPress function, for example:
$user_id = 1;
$field_id = 'field_one';
$var = get_user_meta( $user_id, $field_id, true );
stringstringstringarraystringstringarraystringstringarrayarraymixedAll options are filtered before added to the database. You can add your own filter(s) by using one ore more of these filters:
kcv_setting_prefixadd_filter( 'kcv_setting_myPrefix', 'my_filter_function' );
function my_filter_function( $data ) {
// .... do something with the POST data
return $data;
}
kcv_setting_prefix_sectionID
add_filter( 'kcv_setting_myPrefix_mySectionID', 'my_filter_function' );
function my_filter_function( $section_data ) {
// .... do something with the POST data
return $section_data;
}
kcv_setting_prefix_fieldType
add_filter( 'kcv_setting_myPrefix_textarea', 'my_filter_function' );
function my_filter_function( $field_data ) {
// .... do something with the POST data
return $field_data;
}
kcv_setting_prefix_sectionID_fieldID
add_filter( 'kcv_setting_myPrefix_mySectionID_myFieldID', 'my_filter_function' );
function my_filter_function( $field_data ) {
// .... do something with the POST data
return $field_data;
}
You can also filter your metadata values using the filters below. Note that there are three arguments passed to these filters, and they're valid for the three metadata types (post, term and user):
$nu_val: The new metadata value from the user$section: The section array$field: The field arrayExample validation/sanitation function for metadata:
function my_filter_function( $nu_val, $section, $field ) {
//... do someting with the data
return $nu_val;
}
kcv_postmeta_postTypeadd_filter( 'kcv_postmeta_post', 'my_filter_function', 10, 3 );Note: For menu item metadata, use nav_menu_item as the post type.
kcv_postmeta_postType_fieldTypeadd_filter( 'kcv_postmeta_post_textarea', 'my_filter_function', 10, 3 );kcv_postmeta_postType_sectionIDadd_filter( 'kcv_postmeta_post_mySectionID', 'my_filter_function', 10, 3 );kcv_postmeta_postType_sectionID_fieldIDadd_filter( 'kcv_postmeta_post_mySectionID_fieldID', 'my_filter_function', 10, 3 );The filters used for validating term meta values are very similiar with custom fields' filters. The only difference is that you'd use taxonomy name instead of post type name. Also the filters are prefixed with kcv_termmeta_ instead of kcv_postmeta_. Here are the filters used:
And here are the filters:
kcv_termmeta_taxonomyadd_filter( 'kcv_termmeta_category', 'my_filter_function', 10, 3 );Note: For nav menu, use nav_menu as the taxonomy name.
kcv_termmeta_taxonomy_fieldTypeadd_filter( 'kcv_termmeta_category_textarea', 'my_filter_function', 10, 3 );kcv_termmeta_taxonomy_sectionIDadd_filter( 'kcv_termmeta_category_mySectionID', 'my_filter_function', 10, 3 );kcv_termmeta_taxonomy_sectionID_fieldIDadd_filter( 'kcv_termmeta_category_mySectionID_fieldID', 'my_filter_function', 10, 3 );Validation/sanitation filters for user meta are very similiar with post and term meta. However, with user meta, you'll get fewer filters:
kcv_usermetaadd_filter( 'kcv_usermeta', 'my_filter_function', 10, 3 );kcv_usermeta_fieldTypeadd_filter( 'kcv_usermeta_textarea', 'my_filter_function', 10, 3 );kcv_usermeta_sectionIDadd_filter( 'kcv_usermeta_mySectionID', 'my_filter_function', 10, 3 );kcv_usermeta_sectionID_fieldIDadd_filter( 'kcv_usermeta_mySectionID_fieldID', 'my_filter_function', 10, 3 );Since version 2.6, KC Settings comes with options helpers to make your life easier ;)
There are a few variables you can use to pass as field options:
kcSettings_options::$nav_menuskcSettings_options::$image_sizeskcSettings_options::$image_sizes_defaultkcSettings_options::$image_sizes_customkcSettings_options::$post_types (public post types)kcSettings_options::$post_types_allkcSettings_options::$taxonomies (public taxonomies)kcSettings_options::$taxonomies_allkcSettings_options::$post_statuseskcSettings_options::$roleskcSettings_options::$yesnoAs of version 2.7.3, you can also use a callback as the options.