Created
November 25, 2012 22:22
-
-
Save gicolek/4145661 to your computer and use it in GitHub Desktop.
Extra Category Meta
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// put that into functions.php or include via functions.php | |
define( 'RG_LOCALE', 'rg_locale' ); | |
add_action( 'edit_category_form_fields', 'rgicgier_category_custom_fields', 10, 2 ); | |
add_action( 'edited_category', 'rgicgier_save_category_fields', 10, 2 ); | |
/** | |
* Facilitate obtaining of category meta | |
* | |
* @param int $term_id | |
* @return array | |
* @throws Exception | |
*/ | |
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; | |
} | |
} | |
/** | |
* Print custom form fields | |
* | |
* @param object $tag | |
*/ | |
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" ); | |
?> | |
<tr valign="top"> | |
<th scope="row"><?php _e( 'Sidebar Display', RG_LOCALE ); ?></th> | |
<td> <fieldset><legend class="screen-reader-text"><span>Sidebar display</span></legend><label for="sidebar_display"> | |
<input type="checkbox" id="sidebar_display" name="category_meta[sidebar_display]" <?php checked( isset( $category_meta['sidebar_display'] ) && $category_meta['sidebar_display'] !== 'off' ) ?>> | |
<?php _e( 'Checked displays sidebar on the category page', RG_LOCALE ); ?></label> | |
</fieldset> | |
</td> | |
</tr> | |
<?php | |
} | |
/** | |
* Save custom category fields | |
* | |
* @param id $term_id | |
*/ | |
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 ) ); | |
} | |
} | |
// lack of closing tag, see http://php.net/manual/en/language.basic-syntax.instruction-separation.php |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment