Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add data to select of Contact Form 7 like Listo
<?php
/**
* Plugin Name: CF7 Custom Select
* Description: Add data to select like Listo
* Version: 1.0.0
* Author: Mário Valney
* Author URI: https://mariovalney.com
* Text Domain: cf7-custom-select
*/
/**
* You should add a tag like Listo:
* [select your-select-key data:your_filter_key]
*
* Support args:
* [select your-select-key data:your_filter_key.argument]
*/
add_filter( 'wpcf7_form_tag_data_option', 'cf7_custom_select_wpcf7_form_tag_data_option', 99, 3 );
function cf7_custom_select_wpcf7_form_tag_data_option( $data, $options, $args ) {
if ( empty( $options ) || empty( $options[0] ) ) {
return $data;
}
$option = explode( '.', $options[0], 2 );
$arg = ! empty( $option[1] ) ? $option[1] : '';
return apply_filters( 'cf7_custom_select_' . $option[0], $data, $arg );
}
/**
* You can add filters like this:
* cf7_custom_select_{your_filter_key}
*/
add_filter( 'cf7_custom_select_users', 'cf7_custom_select_users', 10, 3 );
function cf7_custom_select_users( $data, $arg = '' ) {
$args = array(
'role' => $arg,
'fields' => array(
'user_email',
),
);
$users = get_users( $args );
$users = array_column( $users, 'user_email' );
return array_values( array_filter( $users ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment