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.
In this mode, KC Settings will act as a dependency for you plugin/theme. You should have it installed and activated all the time.
To have KC Settings active all the time, you need to install it inside the mu-plugins
directory. To do that, follow these steps:
wp-content
directory.mu-plugins
directory if it doesn't exist inside wp-content
yet.kc-settings.php
file and kc-settings-inc
directory into the mu-plugins
directory.If you think you'd rather have the KC Setting's functionality builtin with your plugin/theme (so user won't have to install it), follow this steps:
require_once(dirname(__FILE__) . '/kc-settings/kc-settings.php');
The only drawback of using this method is that you will have to overwrite the plugin's files each time it has an update. Your options arrays should be safe as long as you keep them outside the plugin's directory and use different names than the originals.
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( TEMPLATEPATH . '/__theme_options.php');
$var = kc_get_option( 'prefix' );
Or, you can also use WordPress' builtin function:
$var = get_option( 'prefix_settings' );
$var = kc_get_option( 'prefix', 'section_id' );
$var = kc_get_option( 'prefix', 'section_id', 'field_id' );
Just use standard WordPress function, but prefix the meta key with an underscore, for example:
$var = get_post_meta( 'post_id', '_field_id', true );
or:
$var = get_metadata( 'post', 'post_id', '_field_id', true );
You can use standard WordPress function, for example:
$var = get_metadata( 'term', 'term_id', 'field_id', true );
You can use standard WordPress function, for example:
$var = get_user_meta( 'user_id', 'field_id', true );
or:
$var = get_metadata( 'user', 'user_id', 'field_id', true );
All 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_prefix
add_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_postType
add_filter( 'kcv_postmeta_post', 'my_filter_function', 10, 3 );
kcv_postmeta_postType_fieldType
add_filter( 'kcv_postmeta_post_textarea', 'my_filter_function', 10, 3 );
kcv_postmeta_postType_sectionID
add_filter( 'kcv_postmeta_post_mySectionID', 'my_filter_function', 10, 3 );
kcv_postmeta_postType_sectionID_fieldID
add_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_taxonomy
add_filter( 'kcv_termmeta_category', 'my_filter_function', 10, 3 );
kcv_termmeta_taxonomy_fieldType
add_filter( 'kcv_termmeta_category_textarea', 'my_filter_function', 10, 3 );
kcv_termmeta_taxonomy_sectionID
add_filter( 'kcv_termmeta_category_mySectionID', 'my_filter_function', 10, 3 );
kcv_termmeta_taxonomy_sectionID_fieldID
add_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_usermeta
add_filter( 'kcv_usermeta', 'my_filter_function', 10, 3 );
kcv_usermeta_fieldType
add_filter( 'kcv_usermeta_textarea', 'my_filter_function', 10, 3 );
kcv_usermeta_sectionID
add_filter( 'kcv_usermeta_mySectionID', 'my_filter_function', 10, 3 );
kcv_usermeta_sectionID_fieldID
add_filter( 'kcv_usermeta_mySectionID_fieldID', 'my_filter_function', 10, 3 );
If you think you found a bug, need some features added, have questions or just want to say hi, please feel free to contact me.