Created
July 12, 2020 16:06
-
-
Save mariovalney/5b401488789d1838fcabdaf39122dc08 to your computer and use it in GitHub Desktop.
Add data to select of Contact Form 7 like Listo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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