Contact Form 7 - Popolare una lista di selezione con una tassonomia personalizzata

27 set 2013, 19:19:42
Visualizzazioni: 13.9K
Voti: 7

Con Contact Form 7, è possibile popolare una lista di selezione (DDL) con i valori di una Tassonomia Personalizzata? Vorrei che l'utente possa selezionare la categoria a cui fare riferimento dal mio Custom Post Type.

3
Commenti

Grazie per aver condiviso questa soluzione. Sarebbe possibile escludere alcune categorie dal comparire nel menu a discesa con il tuo codice?

user1690536 user1690536
4 mar 2014 14:24:18

Certamente. Puoi usare get_terms che ha tantissime opzioni o anche WP_Query

Howdy_McGee Howdy_McGee
4 mar 2014 16:41:39

beh, spero che la gente di stack overflow non ci cacci via dicendo che questo è specifico per wordpress.

Nikhil VJ Nikhil VJ
11 feb 2018 16:29:15
Tutte le risposte alla domanda 1
1
11

Il seguente è un modo più aggiornato per popolare dinamicamente il menu a tendina predefinito con opzioni e potrebbe essere facilmente esteso per supportarne altre.

/**
 * Lista a Selezione Dinamica per Contact Form 7
 * @utilizzo [select name taxonomy:{$taxonomy} ...]
 * 
 * @param Array $tag
 * 
 * @return Array $tag
 */
function dynamic_select_list( $tag ) {

    // Esegui solo su menu a tendina
    if( 'select' !== $tag['type'] && ('select*' !== $tag['type']) ) {
        return $tag;
    } else if ( empty( $tag['options'] ) ) {
        return $tag;
    }

    $term_args = array();

    // Scorri le opzioni per cercare le nostre opzioni personalizzate
    foreach( $tag['options'] as $option ) {

        $matches = explode( ':', $option );

        if( ! empty( $matches ) ) {

            switch( $matches[0] ) {

                case 'taxonomy':
                    $term_args['taxonomy'] = $matches[1];
                    break;

                case 'parent':
                    $term_args['parent'] = intval( $matches[1] );
                    break;

            }
        }

    }

    // Assicurati di avere argomenti per i termini con cui lavorare
    if( empty( $term_args ) ) {
        return $tag;
    }

    // Combina argomenti dinamici con argomenti statici
    $term_args = array_merge( $term_args, array(
        'hide_empty' => false,
    ) );

    $terms = get_terms( $term_args );

    // Aggiungi termini ai valori
    if( ! empty( $terms ) && ! is_wp_error( $term_args ) ) {

        foreach( $terms as $term ) {

            $tag['values'][] = $term->name;

        }

    }

    return $tag;

}
add_filter( 'wpcf7_form_tag', 'dynamic_select_list', 10 );

Quello che segue è un metodo più vecchio/originale per fare ciò e dovrebbe essere considerato obsoleto.

/** Lista Dinamica per Contact Form 7 **/
/** Utilizzo: [select name term:taxonomy_name] **/
function dynamic_select_list($tag, $unused){ 
    $options = (array)$tag['options'];

    foreach ($options as $option) 
        if (preg_match('%^term:([-0-9a-zA-Z_]+)$%', $option, $matches)) 
            $term = $matches[1];

    //controlla se post_type è impostato
    if(!isset($term))
        return $tag;

    $taxonomy = get_terms($term, array('hide_empty' => 0));

    if (!$taxonomy)  
        return $tag;

    foreach ($taxonomy as $cat) {  
        $tag['raw_values'][] = $cat->name;  
        $tag['values'][] = $cat->name;  
        $tag['labels'][] = $cat->name;
    }

    $tag['raw_values'][] = 'Altro';  
    $tag['values'][] = 'Altro';  
    $tag['labels'][] = 'Altro - Specificare di Seguito';

    return $tag; 
}
add_filter( 'wpcf7_form_tag', 'dynamic_select_list', 10, 2);
1 ott 2013 18:16:32
Commenti

Questa soluzione va bene, ma da WP 4.5 in poi le tassonomie dovrebbero essere passate tramite l'argomento 'taxonomy' nell'array $args. $taxonomy = get_terms(array('taxonomy' => $term, 'hide_empty' => false));

5ulo 5ulo
23 set 2016 19:26:41