What if we’d like to add something extra?
1. Add a category specific logo
2. Change the layout of any given category (ex. move the sidebar)
3. Modify CSS per each category screen
In this article I’ll show you a nice trick on how to add extra fields to the category edit screen making sure they affect the Front End. We’ll allow removal of the sidebar here.
We’ll need to do the following:
– Add options to the $prefix_options table connected with the id of each category
– Execute hooks adding input fields to the category edit screen
– Execute hooks saving the options upon category edition
– Make sure Front End gets affected
First lets create a function to facilitate obtaining of category meta:
[php]
define(‘RG_LOCALE’, ‘rg_locale’);
function rgicgier_get_category_meta($term_id = null) {
if ( null === $term_id ) {
if ( is_category() ) {
$term_id = get_cat_id( single_cat_title( “”, false ) );
$meta = get_option( “category_$term_id” );
return $meta;
} else {
throw new Exception( __( ‘Use this hook on a category page or pass term id’ ) );
}
} else {
$meta = get_option( “category_$term_id” );
return $meta;
}
}
[/php]
This code can be either used
a) on a category page (we don’t need to provide terms’ id in that case – this would get obtained automatically, thanks to
[php]
get_cat_id( single_cat_title( “”, false ) );
[/php]
The above hook returns id of the current category and has to be used on the category page.
b) outside of it – requires providing $term_id
Note that if none of the above is satisifed we throw new exception:
[php]
throw new Exception( __( ‘Use this hook on a category page or pass term id’ ) );
[/php]
Now lets add extra fields to the category edit screen, to do so we’ll use the following hook:
[php]
add_action( ‘edit_category_form_fields’, ‘rgicgier_category_custom_fields’, 10, 2 );
[/php]
If you’re not familiar with WP action hooks, please read: http://codex.wordpress.org/Plugin_API
In a nutshell this function injects extra php code to the category edit screen.
[php]
function rgicgier_category_custom_fields($tag) {
$t_id = $tag->term_id; // Get the ID of the term being edited
$category_meta = get_option( “category_$t_id” );
?>
checked‘ here.
Next we need to save our data.
We’ll make usage of the following hook ‘edited_category’. It fires after saving the category.
[php]
add_action( ‘edited_category’, ‘rgicgier_save_category_fields’ , 10, 2 );
function rgicgier_save_category_fields($term_id) {
// make sure that the user has the right privileges
if ( current_user_can( ‘manage_categories’ ) ) {
$t_id = $term_id;
$term_meta = get_option( “category_$t_id” ); // load the old data from the db
// hardcoded, warning !
$checkbox_key = ‘sidebar_display’;
if ( !isset( $_POST[‘category_meta’][$checkbox_key] ) ) {
$term_meta[$checkbox_key] = ‘off’;
} else {
$term_meta[$checkbox_key] = ‘on’;
}
// Note we check to see if the option exists so we get a chance to
// call add_option with the autoload argument. This is an ugly
// aspect of WordPress. This is a performance consideration, to
// prevent WordPress from needlessly querying these options with
// every request.
// See http://codex.wordpress.org/Function_Reference/update_option
$not_exists = false;
$autoload = ‘no’;
$option_name = “category_$t_id”;
$existing_option = get_option( $option_name, $not_exists );
if ( $existing_option === $not_exists ) {
add_option( $option_name, $term_meta, null, $autoload );
} else {
update_option( $option_name, $term_meta );
}
} else {
wp_die( __( ‘Sorry you\’ve got no rights to edit that page’, RG_LOCALE ) );
}
}
[/php]
What’s interesting in here is that part of the code
[php]
$checkbox_key = ‘sidebar_display’;
if ( !isset( $_POST[‘category_meta’][$checkbox_key] ) ) {
$term_meta[$checkbox_key] = ‘off’;
}
else {
$term_meta[$checkbox_key] = ‘on’;
}
[/php]
A checked checkbox sents $_POST data to the edit-category.php page, if not checked then there’d no $_POST data sent, that’s why we’re doing a check here and seeting terms meta to ‘on’ or ‘off’.
Now lets make sure that the data affects Front End.
Let’s assume that our category.php page has the following format:
[php]
[/php]
Now we’re going to:
1. Load the db data associated with a given category
2. Check if sidebar should be displayed or not
[php]
[/php]
Full code can be forked at: github.com