Created
July 14, 2022 22:36
-
-
Save MogulChris/d19021d8482a66e375c5dd1d8ae1a7b8 to your computer and use it in GitHub Desktop.
Add term meta to WordPress terms when created
This file contains 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 | |
//replace my_taxonomy with your own taxonomy slug | |
//The hook create_{$taxonomy} is fired when terms are created in $taxonomy | |
function my_taxonomy_meta_add($term_id, $tax_term_id){ | |
//here you can add whatever meta you need | |
add_term_meta($term_id,'_date_created',time()); | |
} | |
add_action('create_my_taxonomy','my_taxonomy_meta_add',10,3); | |
//optionally display the custom meta on the term edit form (mine is not editable) | |
function my_taxonomy_edit_form($term, $tax){ | |
$created = get_term_meta($term->term_id,'_date_created',true); | |
if(!empty($created)){ | |
$dt = new DateTime(); | |
$dt->setTimeStamp($created); | |
$dt->setTimezone(new DateTimeZone('Pacific/Auckland')); //use your own time zone | |
echo '<tr class="form-field date-created-wrap"> | |
<th scope="row"><label>Date created</label></th> | |
<td>' . $dt->format('d F Y H:ia') . '</td> | |
</tr>'; | |
} | |
} | |
add_action('my_taxonomy_edit_form_fields', 'my_taxonomy_edit_form',10,2); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment