Parliamo del progetto
Wordpress Backoffice

WordPress: Aggiungere una tassonomia manualmente

Esempio di’aggiunta manuale di’una tassonomia (senza passare dalla configurazione).Da mettere nel file functions.php: /** * Manual add new Field to taxonomy or you can use plugin "taxonomy manager" * * BEGIN */ // A callback function to add a cu...

WordPress: Aggiungere una tassonomia manualmente



Esempio di’aggiunta manuale di’una tassonomia (senza passare dalla configurazione).

Da mettere nel file 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);
}
}

Riprodurre questa personalizzazione oggi

Il principio del hook rimane utile, ma gli schermi di amministrazione e le API di WordPress sono evoluti dalla pubblicazione. Testa il codice in un ambiente di prova con la versione esatta del core, del tema e dei plugin del sito.

  • Salvate i file e il database prima di qualsiasi modifica.
  • Controllate le capacità degli utenti, i nonce, la convalida degli input e l'escape degli output.
  • Posizionate la personalizzazione in un'estensione dedicata o in un tema child per preservare gli aggiornamenti.

Riferimento : manuale ufficiale di sviluppo delle estensioni WordPress.

Condividi questo articolo