add_filter( 'gform_pre_render_4', 'populate_clients' );
add_filter( 'gform_pre_validation_4', 'populate_clients' );
add_filter( 'gform_pre_submission_filter_4', 'populate_clients' );
add_filter( 'gform_admin_pre_render_4', 'populate_clients' );
function populate_clients( $form ) {
global $current_user;
wp_get_current_user();
$specialistenposts = get_posts(array(
'post_type' => 'specialisten',
'author' => get_current_user_id()
));
$postsfromuser = array();
foreach( $specialistenposts as $specialistenpost ):
$postsfromuser[] = $specialistenpost->ID;
endforeach;
$userscomma = implode(',', $postsfromuser);
$args = array(
'meta_query' => array(
array(
'key' => 'specialist',
'value' => '"' . $userscomma . '"',
'compare' => 'LIKE'
)
)
);
$user_query = new WP_User_Query($args);
if ( empty( $user_query->get_results() ) ):
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'clienten' ) === false ) {
continue;
}
$choices[] = array(
'text' => 'geen clienten gekoppeld',
'value' => 'geen-clienten-gekoppeld'
);
$field->choices = $choices;
}
else:
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'clienten' ) === false ) {
continue;
}
$choices = array();
foreach ( $user_query->get_results() as $user ) {
$choices[] = array(
'text' => $user->user_firstname. ' ' .$user->user_lastname,
'value' => $user->user_email
);
}
$field->placeholder = 'Selecteer een client';
$field->choices = $choices;
}
endif;
return $form;
}
add_filter( 'gform_pre_render_3', 'populate_posts_product' );
add_filter( 'gform_pre_validation_3', 'populate_posts_product' );
add_filter( 'gform_pre_submission_filter_3', 'populate_posts_product' );
add_filter( 'gform_admin_pre_render_3', 'populate_posts_product' );
function populate_posts_product( $form ) {
$specialisten = get_field('specialist', 'user_'. get_current_user_id() );
if($specialisten):
foreach( $specialisten as $specialist ):
$specialistgekoppeld = get_the_title($specialist);
endforeach;
?>
<script type="text/javascript">
jQuery(document).ready(function($){
jQuery("li#field_3_2 .ginput_container select").addClass("hide");
$( "li#field_3_2 .ginput_container" ).after( "<?php echo $specialistgekoppeld; ?>" );
});
</script>
<?php
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'specialisten' ) === false ) {
continue;
}
$choices[] = array(
'text' => 'specialist al gekoppeld',
'value' => 'specialist-al-gekoppeld'
);
$field->choices = $choices;
}
else:
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'specialisten' ) === false ) {
continue;
}
$posts = get_posts( 'numberposts=-1&post_status=publish&post_type=specialisten' );
$choices = array();
foreach ( $posts as $post ) {
$choices[] = array(
'text' => $post->post_title,
'value' => $post->post_title
);
}
$field->placeholder = 'Selecteer een specialist';
$field->choices = $choices;
}
endif;
return $form;
}
#custom #Forms #functions #Gravity #koppeling #taxonomie