Skip to content

Instantly share code, notes, and snippets.

@k4zuki02h4t4
Last active November 20, 2017 03:46
Show Gist options
  • Save k4zuki02h4t4/c5096240c8e94005fb142e45cdf04933 to your computer and use it in GitHub Desktop.
Save k4zuki02h4t4/c5096240c8e94005fb142e45cdf04933 to your computer and use it in GitHub Desktop.
[WordPress] How to add custom field to taxonomy.
<?php
/**
* Add custom fields to term.
*/
function kaleidpixel_add_term_form_fields() {
if ( 1 < did_action( 'category_add_form_fields' ) ) {
return;
}
?>
<div class="form-field term-group">
<label for="custom_term_meta">
<?php esc_html_e( 'Example meta field', 'kaleidpixel' ); ?>
</label>
<?php wp_nonce_field( 'kaleidpixel_term_meta', 'kaleidpixel_term_meta_nonce' ); ?>
<input type="text" name="custom_term_meta" id="custom_term_meta" value="">
<p class="description">
<?php esc_html_e( 'Enter a text for this field', 'kaleidpixel' ); ?>
</p>
</div>
<?php
}
add_action( 'category_add_form_fields', 'kaleidpixel_add_term_form_fields', 10, 2 );
/**
* Add table column for term.
*
* @param array $columns Table columns.
* @return array $columns New table columns.
*/
function kaleidpixel_add_term_table_columns( $columns ) {
$columns['custom_term_meta'] = __( 'Example meta field', 'kaleidpixel' );
return $columns;
}
add_filter( 'manage_edit-category_columns', 'kaleidpixel_add_term_table_columns' );
/**
* Add table content for term.
*
* @param string $content column content.
* @param string $column_name column name.
* @param int $term_id Term id.
*
* @return string
*/
function kaleidpixel_add_term_table_content( $content, $column_name, $term_id ) {
if ( 'custom_term_meta' === $column_name ) {
$content = get_term_meta( absint( $term_id ), 'custom_term_meta', true );
}
return $content;
}
add_filter( 'manage_category_custom_column', 'kaleidpixel_add_term_table_content', 10, 3 );
/**
* Add custom fields to term.
*
* @param object $term Term data.
*/
function kaleidpixel_add_term_edit_form_fields( $term ) {
if ( 1 < did_action( 'category_edit_form_fields' ) ) {
return;
}
$term_meta = get_term_meta( absint( $term->term_id ), 'custom_term_meta', true );
?>
<tr class="form-field">
<th scope="row" valign="top">
<label for="custom_term_meta"><?php esc_html_e( 'Example meta field', 'kaleidpixel' ); ?></label>
</th>
<td>
<?php wp_nonce_field( 'kaleidpixel_term_meta', 'kaleidpixel_term_meta_nonce' ); ?>
<input type="text" name="custom_term_meta" id="custom_term_meta" value="<?php echo esc_attr( $term_meta ); ?>">
<p class="description">
<?php esc_html_e( 'Enter a text for this field', 'kaleidpixel' ); ?>
</p>
</td>
</tr>
<?php
}
add_action( 'category_edit_form_fields', 'kaleidpixel_add_term_edit_form_fields', 10, 2 );
/**
* Created term meta.
*
* @param int $term_id Term ID.
*/
function kaleidpixel_created_term_meta( $term_id ) {
if ( ! check_admin_referer( 'kaleidpixel_term_meta', 'kaleidpixel_term_meta_nonce' ) || ! current_user_can( 'manage_categories' ) || 1 < did_action( 'created_category' ) || 1 < did_action( 'edited_category' ) ) {
return;
}
$meta = filter_input( INPUT_POST, 'custom_term_meta', FILTER_SANITIZE_SPECIAL_CHARS );
if ( is_null( $meta ) && ( true === defined( 'DOING_AJAX' ) && true === DOING_AJAX ) ) {
$meta = get_term_meta( absint( $term_id ), 'custom_term_meta', true );
}
update_term_meta( absint( $term_id ), 'custom_term_meta', $meta );
}
add_action( 'created_category', 'kaleidpixel_created_term_meta' );
add_action( 'edited_category', 'kaleidpixel_created_term_meta' );
/**
* Rest api init.
*/
function kaleidpixel_rest_api_init() {
register_rest_field( 'category',
'meta',
array(
'get_callback' => '_kaleidpixel_rest_api_cb',
'update_callback' => null,
'schema' => null,
)
);
}
add_action( 'rest_api_init', 'kaleidpixel_rest_api_init' );
/**
* Add new fields for rest api.
*
* @param array $object Object of the term.
*
* @return array|null
*/
function _kaleidpixel_rest_api_cb( $object ) {
$result = null;
if ( false !== isset( $object['taxonomy'] ) && 'category' === $object['taxonomy'] ) {
$result = array( 'custom_term_meta' => get_term_meta( absint( $object['id'] ), 'custom_term_meta', true ) );
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment