|
<?php |
|
|
|
/** |
|
* Plugin Name: [Forminator] autofill hidden fields with custom usermeta from DB |
|
* Plugin URI: https://premium.wpmudev.org/ |
|
* Description: Autmatically fills in hidden fields with usermeta from DB |
|
* Author: adczk |
|
* Author URI: https://premium.wpmudev.org/ |
|
* License: GPLv2 or later |
|
* |
|
* Tested with Forminator 1.23.0 |
|
* |
|
* config explained in code comment |
|
* |
|
* note: use as MU plugin |
|
* |
|
*/ |
|
|
|
add_action( 'forminator_before_form_render', 'wpmudev_generate_usermeta_hidden_data', 10, 5 ); |
|
function wpmudev_generate_usermeta_hidden_data( $id, $form_type, $post_id, $form_fields, $form_settings ){ |
|
|
|
# replace the number below with and ID of your form (form ID is the number you see in form's shortcode) |
|
if( $id == 16091 ){ |
|
add_filter( 'forminator_field_hidden_field_value', 'wpmudev_prefill_hidden_usermeta', 10, 2); |
|
} |
|
} |
|
|
|
function wpmudev_prefill_hidden_usermeta( $value, $saved_value ) { |
|
|
|
|
|
# configure replacement macros |
|
# key (left) is the custom word that you want to replace |
|
# you need to set it exactly the same as a custom value of hidden field |
|
# value (right) is the exact user meta field name, as seen in usermeta table in DB |
|
|
|
$replacements = array( |
|
|
|
'%user_desc%' => 'description', |
|
'%user_activity%' => 'last_activity', |
|
|
|
); |
|
|
|
|
|
######### DON'T EDIT BELOW ######### |
|
|
|
if( !is_user_logged_in() ){ |
|
return $saved_value; |
|
} |
|
$user_id = get_current_user_id(); |
|
|
|
if ( array_key_exists( $value, $replacements ) ) { |
|
|
|
$meta_value = get_user_meta( $user_id, $replacements[$value], true ); |
|
if ($meta_value) { |
|
return $meta_value; |
|
} |
|
|
|
} |
|
|
|
return $saved_value; |
|
|
|
|
|
} |