Skip to content

Instantly share code, notes, and snippets.

@mayeenulislam
Last active December 13, 2015 21:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mayeenulislam/de3af92c59497867950b to your computer and use it in GitHub Desktop.
Save mayeenulislam/de3af92c59497867950b to your computer and use it in GitHub Desktop.
<?php
/**
* Category Meta Fields (add form).
* @param string $taxonomy Taxonomy slug.
*/
function post_category_add_extra_fields( $taxonomy ) { ?>
<div class="form-field extra-input-wrap">
<label for="text"><?php _e( 'Extra Input Field', 'textdomain' ); ?></label>
<input type="text" name="extra_input" id="extra-input" size="40">
<p><?php _e( 'Extra input field description', 'textdomain' ); ?></p>
</div>
<?php
}
add_action( 'category_add_form_fields', 'post_category_add_extra_fields' );
<?php
/**
* Category Meta Fields (edit form).
* @param object $taxonomy Taxonomy object.
*/
function post_category_edit_extra_fields( $taxonomy ) {
$saved_meta = get_option( "tax_meta{$taxonomy->term_id}" );
//var_dump($saved_meta); //for debugging
?>
<tr class="form-field">
<th valign="top" scope="row">
<label for="extra-input"><?php _e( 'Extra Input Field', 'textdomain' ); ?></label>
</th>
<td>
<input type="text" name="extra_input" id="extra-input" value="<?php if( !empty($saved_meta['extra_input']) ) echo $saved_meta['extra_input']; ?>" size="40">
<p class="description"><?php _e( 'Extra input field description', 'textdomain' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'category_edit_form_fields', 'post_category_edit_extra_fields' );
<?php
/**
* Category Meta Fields (edit form).
* @param object $taxonomy Taxonomy object.
*/
function post_category_edit_extra_fields( $taxonomy ) { ?>
<tr class="form-field">
<th valign="top" scope="row">
<label for="extra-input"><?php _e( 'Extra Input Field', 'textdomain' ); ?></label>
</th>
<td>
<input type="text" name="extra_input" id="extra-input" value="" size="40">
<p class="description"><?php _e( 'Extra input field description', 'textdomain' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'category_edit_form_fields', 'post_category_edit_extra_fields' );
<?php
/**
* Saving Category Meta Fields.
* @param integer $term_id Term ID.
* @param integer $tt_id Term Taxonomy ID.
* @param string $taxonomy Taxonomy slug.
*/
function post_category_save_extra_fields( $term_id, $tt_id, $taxonomy ) {
//Handle the Category only
if( $taxonomy === 'category' ) {
$category_meta_array = array();
if( isset($_POST['extra_input']) )
$category_meta_array['extra_input'] = sanitize_text_field( $_POST['extra_input'] );
update_option( "tax_meta{$term_id}", $category_meta_array );
}
}
add_action( 'edit_term', 'post_category_save_extra_fields', 10, 3 );
add_action( 'create_term', 'post_category_save_extra_fields', 10, 3 );
<?php
/**
* Category Meta Fields (add form).
* @param string $taxonomy Taxonomy slug.
*/
function post_category_add_extra_fields( $taxonomy ) { ?>
<div class="form-field extra-input-wrap">
<label for="text"><?php _e( 'Extra Input Field', 'textdomain' ); ?></label>
<input type="text" name="extra_input" id="extra-input" size="40">
<p><?php _e( 'Extra input field description', 'textdomain' ); ?></p>
</div>
<?php
}
add_action( 'category_add_form_fields', 'post_category_add_extra_fields' );
/**
* Category Meta Fields (edit form).
* @param object $taxonomy Taxonomy object.
*/
function post_category_edit_extra_fields( $taxonomy ) {
$saved_meta = get_option( "tax_meta{$taxonomy->term_id}" );
//var_dump($saved_meta); //for debugging
?>
<tr class="form-field">
<th valign="top" scope="row">
<label for="extra-input"><?php _e( 'Extra Input Field', 'textdomain' ); ?></label>
</th>
<td>
<input type="text" name="extra_input" id="extra-input" value="<?php if( !empty($saved_meta['extra_input']) ) echo $saved_meta['extra_input']; ?>" size="40">
<p class="description"><?php _e( 'Extra input field description', 'textdomain' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'category_edit_form_fields', 'post_category_edit_extra_fields' );
/**
* Saving Category Meta Fields.
* @param integer $term_id Term ID.
* @param integer $tt_id Term Taxonomy ID.
* @param string $taxonomy Taxonomy slug.
*/
function post_category_save_extra_fields( $term_id, $tt_id, $taxonomy ) {
//Handle the Category only
if( $taxonomy === 'category' ) {
$category_meta_array = array();
if( isset($_POST['extra_input']) )
$category_meta_array['extra_input'] = sanitize_text_field( $_POST['extra_input'] );
update_option( "tax_meta{$term_id}", $category_meta_array );
}
}
add_action( 'edit_term', 'post_category_save_extra_fields', 10, 3 );
add_action( 'create_term', 'post_category_save_extra_fields', 10, 3 );
<?php
function function_name_here() {
//...
}
add_action( 'category_add_form_fields', 'function_name_here' ); //hooked for default term category (Category)
add_action( 'post_tag_add_form_fields', 'function_name_here' ); //hooked for default term 'pst_tag' (Tags)
add_action( 'taxonomy_one_add_form_fields', 'function_name_here' ); //hooked for custom taxonomy 'taxonomy_one'
add_action( 'taxonomy_two_add_form_fields', 'function_name_here' ); //hooked for custom taxonomy 'taxonomy_two'
<?php
//Let's have an array
$_array = array( 'extra_input' => 'data I stored', 'file_url' => 'http://localhost/project/wp-content/uploads/2015/09/image.jpg' );
var_dump( $_array ); //how our array looks like
//Let's serialize it
$_serialized = serialize( $_array );
var_dump( $_serialized ); //what our serialization produced
<?php
/**
* Get Taxonomy Meta Field.
* @param mixed $term_id Term ID or Term Object.
* @param string $meta_key Meta Key to retrieve its data.
* @return mixed Meta value as string or as array.
*/
function nano_get_tax_meta( $term_id, $meta_key = false ){
$_term_id = ( is_object( $term_id ) ) ? $term_id->term_id : $term_id;
$saved_meta = get_option( "tax_meta{$_term_id}" );
if( $meta_key ) {
if( isset( $saved_meta[$meta_key] ) ){
return $saved_meta[$meta_key];
} else {
return '';
}
} else {
return $saved_meta;
}
}
/**
* Get taxonomy meta data.
* @param mixed $term_id Term ID or Term Object.
* @param string $meta_key Meta Key to retrieve its data.
* @param string $size Image size: medium, thumbnail, large, full or custom size.
* @return string Image URL only.
*/
function nano_get_tax_meta_img_src( $term_id, $meta_key, $size = 'thumbnail' ) {
$img_meta = nano_get_tax_meta( $term_id, $meta_key );
if( !empty($img_meta) )
$img_url = wp_get_attachment_image_src( absint( $img_meta ), $size );
if( $img_url )
return $img_url[0];
else
return '';
}
/**
* Delete Taxonomy Meta.
* @param integer|object $term_id Term ID or Term Object.
* @param string $meta_key Default is false, or the meta key to delete only.
*/
function nano_delete_tax_meta( $term_id, $meta_key = false ) {
$_term_id = ( is_object( $term_id ) ) ? $term_id->term_id : $term_id;
$term_meta = nano_get_tax_meta( $_term_id, $meta_key );
if( $meta_key ) {
if( isset( $term_meta[$meta_key] ) )
unset( $term_meta[$meta_key] );
update_option( "tax_meta{$_term_id}", $term_meta );
} else {
delete_option( "tax_meta{$_term_id}" );
}
}
/**
* Add/Update Taxonomy Meta.
* @param integer $term_id|object Term ID or Term Object.
* @param string $meta_key Taxonomy Meta Key to update its value.
* @param string $meta_value New value to update with.
*/
function nano_update_tax_meta( $term_id, $meta_key, $meta_value ) {
$_term_id = ( is_object( $term_id ) ) ? $term_id->term_id : $term_id;
$term_meta = nano_get_tax_meta( $_term_id, $meta_key );
if( isset( $term_meta[$meta_key] ) ) {
$term_meta[$meta_key] = $meta_value;
update_option( "tax_meta{$_term_id}", $term_meta );
}
}
<?php
/**
* Get image ID from full URL.
* @link https://pippinsplugins.com/retrieve-attachment-id-from-image-url/
* @link http://codex.wordpress.org/Function_Reference/url_to_postid
*
* @param string $image_url Full URL of an image.
* @return integer Attachment ID of the image.
*/
function nanodesigns_get_image_id( $image_url ) {
global $wpdb;
if( $image_url ) {
$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
return $attachment[0];
} else {
return '';
}
}
/**
* Saving Category Meta Fields.
* @param integer $term_id Term ID.
* @param integer $tt_id Term Taxonomy ID.
* @param string $taxonomy Taxonomy slug.
*/
function post_category_save_image_field( $term_id, $tt_id, $taxonomy ) {
//Handle the Category only
if( $taxonomy === 'category' ) {
$category_meta_array = array();
if( isset($_POST['image_url']) ) {
$img_id = nanodesigns_get_image_id( sanitize_text_field( $_POST['image_url'] ) );
$category_meta_array['image_id'] = absint( $img_id );
}
update_option( "tax_meta{$term_id}", $category_meta_array );
}
}
add_action( 'edit_term', 'post_category_save_image_field', 10, 3 );
add_action( 'create_term', 'post_category_save_image_field', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment