Contact Form 7 - Populate Select List With Taxonomy

Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress.

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/HTML/CSS questions might be better asked at Stack Overflow or another appropriate site of the Stack Exchange network. Third party plugins and themes are off topic.

Closed 10 years ago.

Improve this question

With contact form 7, is it possible to populate a DDL (Selection List) with Custom Taxonomy Values? I want the user to be able to click the category they want to refer to from my Custom Post Type.

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/HTML/CSS questions might be better asked at Stack Overflow or another appropriate site of the Stack Exchange network. Third party plugins and themes are off topic.

Closed 10 years ago.

Improve this question

With contact form 7, is it possible to populate a DDL (Selection List) with Custom Taxonomy Values? I want the user to be able to click the category they want to refer to from my Custom Post Type.

Share Improve this question asked Sep 27, 2013 at 16:19 Howdy_McGeeHowdy_McGee 20.9k24 gold badges91 silver badges177 bronze badges 3
  • Thanks for sharing this solution. Would it be possible to exclude some categories from appearing in the ddl with your code? – user1690536 Commented Mar 4, 2014 at 12:24
  • Definitely. You can use get_terms which has tons of options or even WP_Query – Howdy_McGee Commented Mar 4, 2014 at 14:41
  • well I hope the stack overflow folks don't chase us out saying this is specific to wordpress. – Nikhil VJ Commented Feb 11, 2018 at 14:29
Add a comment  | 

1 Answer 1

Reset to default 11

The following is a more up to date way to dynamically populate the built-in select with options and could easily be extended to support more.

/**
 * Dynamic Select List for Contact Form 7
 * @usage [select name taxonomy:{$taxonomy} ...]
 * 
 * @param Array $tag
 * 
 * @return Array $tag
 */
function dynamic_select_list( $tag ) {

    // Only run on select lists
    if( 'select' !== $tag['type'] && ('select*' !== $tag['type']) ) {
        return $tag;
    } else if ( empty( $tag['options'] ) ) {
        return $tag;
    }

    $term_args = array();

    // Loop thorugh options to look for our custom options
    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;

            }
        }

    }

    // Ensure we have a term arguments to work with
    if( empty( $term_args ) ) {
        return $tag;
    }

    // Merge dynamic arguments with static arguments
    $term_args = array_merge( $term_args, array(
        'hide_empty' => false,
    ) );

    $terms = get_terms( $term_args );

    // Add terms to values
    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 );

The below is an older/original way to do this and should be considered outdated.

/** Dynamic List for Contact Form 7 **/
/** Usage: [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];

    //check if post_type is set
    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'][] = 'Other';  
    $tag['values'][] = 'Other';  
    $tag['labels'][] = 'Other - Please Specify Below';

    return $tag; 
}
add_filter( 'wpcf7_form_tag', 'dynamic_select_list', 10, 2);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745327311a4622718.html

相关推荐

  • Contact Form 7 - Populate Select List With Taxonomy

    Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress.

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信