I want to populate an input field with data from the current user.
At the moment I populate the fields value parameter but I want to keep the input empty.
Therefore it would be great if I could populate the label or the placeholder parameter of the input field.
I couldn't find anything in the docs.
Here's my current code:
add_filter('gform_field_value_username', create_function("", '$value = populate_usermeta(\'nickname\'); return $value;' ));
I believe this should work out-of-the-box with Gravity Forms:
With some help from the Gravity Forms support and some own testing, I came up with a solution:
function populate_usermeta($meta_key){
global $current_user;
return $current_user->__get($meta_key);
}
add_filter( 'gform_pre_render', 'populate_text' );
//Note: when changing choice values, we also need to use the gform_pre_validation so that the new values are available when validating the field.
add_filter( 'gform_pre_validation', 'populate_text' );
//Note: this will allow for the labels to be used during the submission process in case values are enabled
add_filter( 'gform_pre_submission_filter', 'populate_text' );
function populate_text( $form ) {
$items = array();
$fields = $form['fields'];
foreach( $form['fields'] as &$field ) {
if ( $field->type != 'text' || strpos( $field->cssClass, 'populate-placeholder' ) === false ) {
continue;
}
$field->placeholder = populate_usermeta($field->inputName);
}
return $form;
}
Related
How to adjust the submitted Data before it gets submitted??
(I am trying to adjust the dropdown value's to use the value instead of the text)
Here is my code:
function wpf_dev_process( $fields, $entry, $form_data ) {
if ( absint( $form_data['id'] ) !== 66203 ) {
return $fields;
}
foreach ( $fields as $key => $value ){
if ($fields[$key]["type"] == "select"){
$fields[$key]["value"] = $fields[$key]["value_raw"];
}
}
echo "<pre>". print_r($fields, true)."</pre><hr>";
return $fields;
}
add_action( 'wpforms_process', 'wpf_dev_process', 10, 3 );
My echo statement shows on my page the "corrected" data as can be seen here in this image :
echo "<pre>". print_r($fields, true)."</pre><hr>";
But this is a User Registration Form and the data submitted to the Database is still the original value and not the value_raw that I change it to?
Anyone have any idea?
I solved this using the wpforms_user_registered action.
After the system created the user, I used update_user_meta to insert the correct values from the drop-downs.
GOAL
I currently have an acf field group with some fields for generic data like phonenumber, email, etc. (all simple text fields).
My goal is to have one function that will loop over all the fields and create a shortcode based on their respective name/label.
EDIT: PROGRESS
I think I am getting closer, simplified the whole thing and I believe I am on the right path..
I attached the field group to a post and if I log them like so:
$testing = get_field_objects(22);
do_action( 'php_console_log', $testing );
I get all the names, values etc. With the help of another snippet I found the shortcodes seem to be at least dynamic since they all stop showing just that there is no value somehow so it all stays blank.
This is the function now:
$hlfields = get_field_objects( 22 );
foreach ( $hlfields as ['name' => $name, 'value' => $value] ) {
${"{$name}_fn"} = function() {
$field = get_field($name, 22);
return $field;
};
add_shortcode($name, ${"{$name}_fn"});
}
Original attempt, not working and over complicated:
I currently have an acf field group with some fields for generic data like phonenumber, email, etc. (all simple text fields).
To be able to insert them all easily using a shortcode, I create one for each field like so:
function adresse_shortcode( $adresse ) {
$adresse = get_field( "adresse", 'option' );
return $adresse;
}
add_shortcode('adresse', 'adresse_shortcode');
While this works fine, I feel like I am repeating myself unnecessarily and also I need to add a function whenever I add a new field.
My goal is to have one function that will loop over all the fields and create a shortcode based on their respective name/label.
I found this snippet to get all fields of a field group by ID:
function get_specifications_fields() {
global $post;
$specifications_group_id = 13; // Post ID of the specifications field group.
$specifications_fields = array();
$fields = acf_get_fields( $specifications_group_id );
foreach ( $fields as $field ) {
$field_value = get_field( $field['name'] );
if ( $field_value && !empty( $field_value ) ) {
$specifications_fields[$field['name']] = $field;
$specifications_fields[$field['name']]['value'] = $field_value;
}
}
return $specifications_fields;
}
But I can't figure out how to create the shortcodes now, I have been trying everything I could think of along these lines:
function adresse_shortcode( $value ) {
$specifications_fields = get_specifications_fields();
foreach ( $specifications_fields as $name => $field ) {
$value = $field['value'];
$label = $field['label'];
$name = $field['name'];
return $name;
}
add_shortcode($value, 'adresse_shortcode');
}
I am not well versed in PHP so I am sure it's all sorts of wrong but I have been trying to figure it out for hours and was hoping someone might be able point me in the right direction.
Here's an example using anonymous functions that should do the trick
$hlfields = get_field_objects( 22 );
foreach ( $hlfields as ['name' => $name, 'value' => $value] ) {
$cb = function() use ($name) {
$field = get_field($name, 22);
return $field;
};
add_shortcode( $name, $cb );
}
Here's the sources i referred to while answering: Dynamic function name in php, Dynamic shortcodes and functions in WordPress, PHP: Variable functions
I'm trying to dynamically update the CF7 to field by replacing the recipient string with a custom post field value, though I can't figure out how to assign the value it's definitely replacing the string as I get an error and if I change email it sends. So it tells me the problem is when I'm trying to get the value.
<?php
function wpcf7_dynamic_email_field($args) {
$dynamic_email = get_post_meta(get_the_ID(), 'dynamic_email', true);
if(!empty($args['recipient'])) {
$args['recipient'] = str_replace('emailtobereplaced#email.com', $dynamic_email["dynamic_email"], $args['recipient']);
return $args;
}
return false;
}
add_filter('wpcf7_mail_components', 'wpcf7_dynamic_email_field');
?>
Can anyone point me in the right direction here? My custom field is called dynamic_email and I need the value
get_the_id() won't return the proper ID because Contact form 7 uses Ajax to perform the send.
You can get the post ID from the submission unit tag. The unit tag is a hidden form field that gets posted with the form and it looks cryptic but it stores information about the post. An example unit tag looks like wpcf7-f235-p27-o1. Using a regex we can get the post id out of the unit tag.
function wpcf7_dynamic_email_field( $args ) {
$dynamic_email = '';
$submission = WPCF7_Submission::get_instance();
$unit_tag = $submission->get_meta( 'unit_tag' );
// get the post ID from the unit tag
if ( $unit_tag && preg_match( '/^wpcf7-f(\d+)-p(\d+)-o(\d+)$/', $unit_tag, $matches ) ) {
$post_id = absint( $matches[2] );
$dynamic_email = get_post_meta( $post_id, 'dynamic_email', true );
}
if ( $dynamic_email ) {
$args['recipient'] = str_replace('emailtobereplaced#email.com', $dynamic_email["dynamic_email"], $args['recipient']);
}
return $args;
}
add_filter( 'wpcf7_mail_components', 'wpcf7_dynamic_email_field' );
Since this is a filter and not an action you always want to return the first parameter that was passed to your callback in this case $args.
I am using gravity forms plugin, and I'm trying to display the categories as a drop down list in the form I have already created.
If required, please here's a link to my website
I've been on this for too long, and no way out. Kindly help me out.
add_filter( 'gform_pre_render_1', 'populate_categories' );
add_filter( 'gform_pre_validation_1', 'populate_categories' );
add_filter( 'gform_pre_submission_filter_1', 'populate_categories' );
add_filter( 'gform_admin_pre_render_1', 'populate_categories' );
function populate_categories( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->id != 1 ) {
continue;
}
// you can add additional parameters here to alter the posts that are retrieved
// more info: [http://codex.wordpress.org/Template_Tags/get_posts](http://codex.wordpress.org/Template_Tags/get_posts)
$categories = get_categories ;
$choices = array();
foreach ( $categories as $categories ) {
$choices[] = array( 'text' => $categories->name, 'value' => $categories->name );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Category';
$field->choices = $choices;
}
return $form;
}
You can dynamically generate drop downs for gravity forms using the syntax provided below in this link. You have to take control over the functions.php file of the theme to retrieve your output as per your requirement.
Here is the clear documentation provided and you can create in a simple manner using this methods. There are two methods available refer to it.
https://www.gravityhelp.com/documentation/article/dynamically-populating-drop-down-fields/
If you face any problem with creation let me know we shall solve it.
I'm trying to remove spaces automatically from data entered into a custom field generated by the ACF plugin when a custom post is updated or saved in wordpress.
I believe I need to use the acf/save_post hook but I'm struggling to get the preg_replace to work. I wonder if I'm not using the right identifier as the custom field name has field name postcodes but when inspected it has name fields[field_55c7969262970]. Can't seem to make it work with that either.
function remove_spaces( $post_id ) {
if( empty($_POST['postcodes']) ) {
return;
} else{
$postcodes = $_POST['postcodes'];
$postcodes = preg_replace('/\s+/', '', $postcodes);
return $postcodes; }
}
add_action('acf/save_post', 'remove_spaces', 1);
I think you are better off using the acf/update_value filter. From thr docs: "This hook allows you to modify the value of a field before it is saved to the database."
function remove_spaces($value, $post_id, $field) {
if(empty($value)) {
return;
}
$value = preg_replace('/\s+/', '', $value);
return $value;
}
add_filter('acf/update_value/name=postcodes', 'remove_spaces', 10, 3);