Exemple d’ajout manuel d’une taxonomie (sans passer par la configuration).
A mettre dans le fichier functions.php :
/**
* Manual add new Field to taxonomy or you can use plugin "taxonomy manager"
*
* BEGIN
*/
// A callback function to add a custom field to our "expertise" taxonomy
function expertise_taxonomy_custom_fields($tag) {
// Check for existing taxonomy meta for the term you're editing
$t_id = $tag->term_id; // Get the ID of the term you're editing
$term_meta = get_option("taxonomy_term_$t_id"); // Do the check
?>
<tr class="form-field">
<th scope="row" valign="top">
<label for="presenter_id"><?php _e('WordPress User ID'); ?></label>
</th>
<td>
<input type="text" name="term_meta[presenter_id]" id="term_meta[presenter_id]" size="25" style="width:60%;" value="<?php echo $term_meta['presenter_id'] ? $term_meta['presenter_id'] : ''; ?>"><br />
<span class="description"><?php _e('The Presenter\'s WordPress User ID'); ?></span>
</td>
</tr>
<?php
}
// A callback function to save our extra taxonomy field(s)
function save_taxonomy_custom_fields($term_id) {
if (isset($_POST['term_meta'])) {
$t_id = $term_id;
$term_meta = get_option("taxonomy_term_$t_id");
$cat_keys = array_keys($_POST['term_meta']);
foreach ($cat_keys as $key) {
if (isset($_POST['term_meta'][$key])) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
//save the option array
update_option("taxonomy_term_$t_id", $term_meta);
}
}
Reproduire cette personnalisation aujourd’hui
Le principe du hook reste utile, mais les écrans d’administration et les API WordPress ont évolué depuis la publication. Testez le code dans un environnement de recette avec la version exacte du cœur, du thème et des extensions du site.
- Sauvegardez les fichiers et la base avant toute modification.
- Contrôlez les capacités utilisateur, les nonces, la validation des entrées et l’échappement des sorties.
- Placez la personnalisation dans une extension dédiée ou un thème enfant afin de préserver les mises à jour.
Référence : manuel officiel de développement des extensions WordPress.