I have a custom post type named Student on WordPress that Adds new students. You can fill out the Students name, Student description and Student Admission Number.
I have also created a gravity form where a potential sponsor can submit their details when they want to sponsor a student. However, the sponsors need to fill in the Students Name and Student Admission Number on the form. Is it possible to have the fields Student Name and Student Admission Number populate dynamically on the gravity form?
The form is being displayed on the same page as the student post. The student name is the post title. The student registration number is a custom field created using advanced custom field plugin.The field label for student admission number is Student Admission Number and field name is student_admission_number on the custom field. On gravity form, we have Student Name and Student Admission Number as form fields.
How can I have the fields Student Name and Student Admission Number populate dynamically on the gravity form?
I tried adding the following code to my functions.php file but it did not work.
// Add a custom shortcode to display the form
function custom_gravity_form_shortcode( $atts ) {
$current_post = get_post();
// Get the student name and admission number from the current post
$student_name = $current_post->post_title;
$student_admission_number = get_field('student_admission_number', $current_post->ID);
// Get the form ID from the shortcode attributes
$form_id = $atts['id'];
// Prepare the form arguments
$form_args = array(
'field_values' => array(
'student_name' => $student_name,
'student_admission_number' => $student_admission_number,
),
);
// Render the form
return gravity_form( $form_id, false, false, false, $form_args, true );
}
add_shortcode( 'gravityform', 'custom_gravity_form_shortcode' );
Try the following (The shortcode you would use on the page is [stud_gravityform]:
function custom_gravity_form_shortcode($atts = array()){
$details = shortcode_atts( array(
'form_id'=>'176'//change 176 to your form id
), $atts );
$current_post = get_post();
// Get the student name and admission number from the current post
$student_name = $current_post->post_title;
$student_admission_number = get_field('student_admission_number', $current_post->ID);
// Get the form ID from the shortcode attributes
$form_id = $details['form_id'];
// Prepare the form arguments
$form_args = array(
'field_values' => array(
'student_name' => $student_name,
'student_admission_number' => $student_admission_number
)
);
// Render the form
return gravity_form( $form_id, false, false, false, $form_args, true );
}
add_shortcode( 'stud_gravityform', 'custom_gravity_form_shortcode' );//the gravityform shortcode is already taken so I changed it to "stud_gravityform"
It might be easier to rely on the {custom_field} merge tag set in the default value setting of each field that should be dynamically populated. With this, it would fetch whatever custom field you specify from the post on which the form is embedded.
Less code. And easier to configure.
Related
I'm creating a custom post type for our projects page. I've also made a custom posts type for our employees.
With ACF I've made a Relationship field which where you can add the team members to a project and it's displayed on the website.
Based in the selected team members posts in the relationship field I would like the generate a tag for each title (Employee name) that is loaded in the relationship field.
This is were I'm stuck now.
The name in the Post Object is called teamleden. I've tried adding code to my customs posts type file but it doesn't work.
<?php
// save post action
add_action('acf/save_post', 'set_employee_tags_on_save_update', 20);
/**
* #param $post_id int|string
*/
function set_employee_tags_on_save_update($post_id) {
// get our current post object
$post = get_post($post_id);
// if post is object
if(is_object($post)) {
// check we are on the projects custom type and post statuses are either publish or draft
// change 'projects' post type to your post type name which has the relationship field
if($post->post_type === 'projects' && ($post->post_status === 'publish' || $post->post_status === 'draft')) {
// get relationship field employees
// this example uses Post Object as the Return Format and is a multiple value
// change get field param 'employees' to your relationship field name
$employees = get_field('employees');
// team member tags to set empty array
$team_member_tags_to_set = [];
// if employees has value or values
if($employees) {
// get all of our current team member tags
// change 'team_members' taxonomy value to your members taxonomy name
$team_member_tags = get_terms([
'taxonomy' => 'team_members',
'orderby' => 'name',
'order' => 'ASC'
]);
// empty array for existing team member tags
$existing_team_member_tags = [];
// if we have existing team member tags
if(!empty($team_member_tags)) {
// foreach team member tags as team member tag
foreach($team_member_tags as $team_member_tag) {
// add existing team member to our existing team member tags array by tag ID => tag name
// this is so we can use this later to check if a team member tag already exists so we dont create duplicates
$existing_team_member_tags[$team_member_tag->ID] = $team_member_tag->name;
}
}
// foreach employees as employee
foreach($employees as $employee) {
// get the title for current employee
$title = get_the_title($employee->ID);
// search existing team members array and return the tag id via key
$existing_team_member_tag_id = array_search($title, $existing_team_member_tags);
// if we have an existing team member tag id
if($existing_team_member_tag_id) {
// add the existing team member tag id as integer to array
$team_member_tags_to_set[] = (int)$existing_team_member_tag_id;
} else {
// else create a new tag for team member by adding title (name) as string to array
$team_member_tags_to_set[] = (string)$title;
}
}
}
// remove the action
remove_action('acf/save_post', 'acf_save_post');
// set post tags for this post id, removing any unused team member tags if relationship field team members are changed
wp_set_object_terms($post_id, $team_member_tags_to_set, $taxonomy = 'team_members', false);
// re add the action
add_action('acf/save_post', 'acf_save_post');
}
}
// finally return
return;
}
This is not tested but should get you on the right track.
You will need to add this in your function.php.
Reference to things you will need to change in my example code...
projects post_type is the post type name which this code fires when post is saved or updated.
employees is the name of the acf relationship field in the projects post type.
team_members is the custom tag taxonomy name which you should have working on your projects post type.
Basically what this code does, is when a projects post is saved, published or updated using the acf/save_post action. It gets the acf relationship field member data for this post. If there are members in the field, it then gets all existing team member tags and creates a simple array of existing member tags by ID => Title(name).
It then loops through all members in the relationship field, and checks if tag already exists for member, if it does it adds the existing member tag (int) ID to the $team_member_tags_to_set array. If no existing member tag is found, we just add the member title (name) as a (string) to the $team_member_tags_to_set array.
After all this is done, we simply use wp_set_post_tags() passing our $team_member_tags_to_set array to update team_members taxonomy terms for the current projects post.
I've also set append to false in wp_set_post_tags() which removes all previous tags and creates a new set of tags. This will help if members get updated in the acf relationship field.
https://developer.wordpress.org/reference/functions/wp_set_post_tags/
See code below, and read my comments so you know whats happening.
<?php
// save post action
add_action('acf/save_post', 'set_employee_tags_on_save_update', 20);
/**
* #param $post_id int|string
*/
function set_employee_tags_on_save_update($post_id) {
// get our current post object
$post = get_post($post_id);
// if post is object
if(is_object($post)) {
// check we are on the projects custom type and post statuses are either publish or draft
// change 'projects' post type to your post type name which has the relationship field
if($post->post_type === 'projects' && ($post->post_status === 'publish' || $post->post_status === 'draft')) {
// get relationship field employees
// this example uses Post Object as the Return Format and is a multiple value
// change get field param 'employees' to your relationship field name
$employees = get_field('employees');
// team member tags to set empty array
$team_member_tags_to_set = [];
// if employees has value or values
if($employees) {
// get all of our current team member tags
// change 'team_members' taxonomy value to your members taxonomy name
$team_member_tags = get_terms([
'taxonomy' => 'team_members',
'orderby' => 'name',
'order' => 'ASC'
]);
// empty array for existing team member tags
$existing_team_member_tags = [];
// if we have existing team member tags
if(!empty($team_member_tags)) {
// foreach team member tags as team member tag
foreach($team_member_tags as $team_member_tag) {
// add existing team member to our existing team member tags array by tag ID => tag name
// this is so we can use this later to check if a team member tag already exists so we dont create duplicates
$existing_team_member_tags[$team_member_tag->ID] = $team_member_tag->name;
}
}
// foreach employees as employee
foreach($employees as $employee) {
// get the title for current employee
$title = get_the_title($employee->ID);
// search existing team members array and return the tag id via key
$existing_team_member_tag_id = array_search($title, $existing_team_member_tags);
// if we have an existing team member tag id
if($existing_team_member_tag_id) {
// add the existing team member tag id as integer to array
$team_member_tags_to_set[] = (int)$existing_team_member_tag_id;
} else {
// else create a new tag for team member by adding title (name) as string to array
$team_member_tags_to_set[] = (string)$title;
}
}
}
// remove the action
remove_action('acf/save_post', 'acf_save_post');
// set post tags for this post id, removing any unused team member tags if relationship field team members are changed
wp_set_post_tags($post_id, $team_member_tags_to_set, false);
// re add the action
add_action('acf/save_post', 'acf_save_post');
}
}
// finally return
return;
}
CONTEXT:
I’ve used the WPForms plugin in Wordpress to create a custom donation form for my client. The client wants to accept donations via PayPal.
PROBLEM:
WPForms has a PayPal add-on feature but it costs $200.
QUESTION
To avoid paying $200 for the expensive plugin, how can I manually direct/post the WPForm data entered by the user (name, email, address, donation amount, and employment info) to PayPal?
In the example code you’ll see below, we’re first checking the form ID to make sure it matches the form that’s being targeted. Then we’re checking a specific field (by the field ID) to see if it’s empty.
Just remember to change the form ID from 5 to match your form ID and change the '4' to match your field ID.
function wpf_dev_process( $fields, $entry, $form_data ) {
// Optional, you can limit to specific forms. Below, we restrict output to
// form #5.
if ( absint( $form_data['id'] ) !== 5 ) {
return $fields;
}
// check the field ID 4 to see if it's empty and if it is, run the error
if(empty($fields[4]['value']))
{
// Add to global errors. This will stop form entry from being saved to the database.
// Uncomment the line below if you need to display the error above the form.
// wpforms()->process->errors[ $form_data['id'] ]['header'] = esc_html__( 'Some error occurred.', 'plugin-domain' );
// Check the field ID 4 and show error message at the top of form and under the specific field
wpforms()->process->errors[ $form_data['id'] ] [ '4' ] = esc_html__( 'Some error occurred.', 'plugin-domain' );
// Add additional logic (what to do if error is not displayed)
}
}
add_action( 'wpforms_process', 'wpf_dev_process', 10, 3 );
I am trying to get the array for all the shipping forms with a new custom element that I already added. This code is not working:
$newObj = new WC_Checkout();
$shipping_fields = $newObj->get_checkout_fields($fieldset = 'shipping');
Woocommerce WC_Checkout methods:
Visit https://docs.woocommerce.com/wc-apidocs/source-class-WC_Checkout.html#197-281 for more info!
https://superuser.com?
I get NULL so the object is not existing
In detail:
I'm developing a website, where the total price is calculated according to a delivery area in some city. I decided to create a custom field within the shipping-form:
// Adding districts for the city of Lima on shipping form
add_filter('woocommerce_checkout_fields', 'custom_district_checkout_field');
function custom_district_checkout_field($fields) {
//the list for this example was shortened
$option_cities = array(
'' =>__('Select your district'),
'chorrillos' =>'Chorrillos',
'miraflores' =>'Miraflores'
};
$fields['shipping']['shipping_district']['type'] = 'select';
$fields['shipping']['shipping_district']['options'] = $option_cities;
$fields['shipping']['shipping_district']['class'] = array('update_totals_on_change');
$fields['shipping']['shipping_district']['input_class'] = array('wc-enhanced-select');
$fields['billing']['billing_district']['type'] = 'select';
$fields['billing']['billing_district']['options'] = $option_cities;
$fields['billing']['billing_district']['input_class'] = array('wc-enhanced-select');
wc_enqueue_js("jQuery( ':input.wc-enhanced-select' ).filter( ':not(.enhanced)' ).each( function() {var select2_args = { minimumResultsForSearch:5};jQuery( this ).select2( select2_args ).addClass( 'enhanced' );})");
return $fields;
I can confirm that the custome field is working. For other hand I'm trying to change the way the WooCommerce Advance Shipping plugin works as Jeroen Sormani (who is the developer) explain in his blogs:
How the plugin works!
and WAS Shipping fields conditions
The idea is to add to the condition list the shipping fields, the plugin shows by default this fields: WC Advanced Shipping Fields by Default
The goal is to been able to select the newly created field in the conditions (for example: "districts") so the price would appear in the cart when the user select the correct option, the plugin already has a list of the different districts with their respective prices. However, there is an error in the plugin because this line is not working (check the Github for the WAS Shipping fields conditions inside the first function:
$shipping_fields = WC()->checkout()->checkout_fields['shipping'];
I have been trying to solve this for weeks, hence the original ask in this post.
/**
* WAS all checkout fields
*/
add_filter('was_conditions', 'was_conditions_add_shipping_fields', 10, 1);
function was_conditions_add_shipping_fields($conditions) {
$newObj = new WC_Checkout();
$shipping_fields = $newObj->get_checkout_fields($fieldset = 'shipping');
debugToConsole($shipping_fields);
foreach ($shipping_fields as $key => $values) :
if (isset($values['label'])) :
$conditions['Shipping Fields'][$key] = $values['label'];
endif;
endforeach;
return $conditions;
}
The above results in a NULL with the debugToConsole function.
I'm using WP-Members. And I'm using wp_update_user function to update value.
When I try to update the "text" value, it is updated. Like this.
$test = array ( 'ID' => $id,'first_name' => $first_name);
But, when it comes to update the value of "radio", it didn't work.
$test = array ( 'ID' => $id,'plan' => $plan);
$plan is a string value
setting of the "plan"
How can I update the radio button value?
wp_update_user() only updates WordPress native fields. If you have a radio field in your form, then that would certainly be a custom field not native to WP. Any custom field is a user meta field and must be updated with update_user_meta():
update_user_meta( $id, 'plan', $plan );
Note that this would be true of any field that is not a native WP field (i.e. any custom fields you added in WP-Members, or using any other method).
I have created a drop down field in the CMS like so:
class ProductPage extends Page {
//.....
private static $has_one = [
'TeaserImage'=>Image::Class,
'LinkedProduct'=>'Product'
];
public function getCMSFields(){
$fields = parent::getCMSFields();
$productLinkField = DropdownField::create('LinkedProduct', 'Link a Product', Product::get()->map('ID', 'ProductName'));
$productLinkField->setEmptyString('(Select one)');
$fields->addFieldToTab('Root.Main', $productLinkField, 'Content');
return $fields;
}
}
The problem is that when I select a value and save/publish the page the drop down goes back to "Select one" instead of showing the saved selection.
I have not checked the database to see if the value is being stored but I assume it is.
EDIT: Not a duplicate.
The suggested duplicate dealt with removing a field from the CMS.
This question deals with setting the drop down value to the saved selection.
The answers are however similar. The user must always append ID to a has_one field for the CMS to interact with it.
By default SilverStripe appends an 'ID' parameter to the end of has_one relation fields when saving them in the database.
As such when you override the field for relations you will need to append 'ID' to the field identifier.
DropdownField::create('LinkedProductID', 'Link a Product', Product::get()->map('ID', 'ProductName'));