Gravity form auto populating field not working - php

I am using gravity forms for wordpress and and i have following function to auto populate the form field:
add_filter('gform_field_value_vendor_category', 'populate_post_vendor_category');
function populate_post_vendor_category($value){
global $post;
$vendor_category = the_terms( $post->ID, 'listing_category');
return $vendor_category;
}
Adding the parameter name vendor_category to the form does not seem to work.
I have tried the following code on a template file and it displays the current listing category.
<?php global $post;
$vendor_cat = the_terms( $post->ID, 'listing_category');
echo $vendor_cat; ?>
Not sure why the field is not auto populating?

the terms is for echoing . Use get_the_terms() instead for returning values.
get_the_terms( $id, $taxonomy );
as a rule of thumb, when you do not see the prefix get_ in wordpress, the function will echo on the screen. like the_title() ( echoing ) and get_the_title (returning )..

Related

How to use PHP variable with a Wordpress Shortcode of AAWP Plugin

I use Wordpress with the GeneratePress Theme. A feature of this theme called "Elements" let you write php code which will be executed on a specific hook. These Hooks can be found here: https://docs.generatepress.com/article/hooks-visual-guide/
I use the AAWP plugin to show an amazon bestseller list. I want to get the Wordpress category and place it into the bestseller parameter of the aawp shortcode to get Products for the respective categories.
I already read posts here about using php variables in wordpress shortcodes but it seems that something other than the syntax is the problem.
I have this code
<?php
$categories = get_the_category();
if ( ! empty($categories ) ) {
$category = $categories[0];
$name = $category->name;
$bestseller = 'bestseller="'.$name.'"';
$shortCode = '[aawp grid="3" '.$bestseller.' items="3" filter_items="20" orderby="percentage_saved" order="desc"]';
echo do_shortcode($shortCode);
}
?>
The amazon list doesnt appear on my page. It seems that the "bestseller" parameter is neccesarry in order to get shown.
The variable doesnt really get recognized.
When I remove the variable and replace it with a hardcoded bestseller parameter its working fine.
When I use this code:
<?php
$categories = get_the_category();
if ( ! empty($categories ) ) {
$category = $categories[0];
$name = $category->name;
$bestseller = 'bestseller="'.$name.'"';
$shortCode = '[aawp template="angebot-vertical" grid="3" bestseller="'.$name.'" items="3" filter_items="20" orderby="percentage_saved" order="desc"]';
echo do_shortcode($shortCode);
}
?>
It seems that the bestseller value doesnt get read but the "empty" bestseller parameter is enough to show the amazon list, so it just shows random products.
How can I make this work? I just want to place a variable as a value into the "bestseller" parameter. Should be the easiest thing but it doesnt work in the scope of this GeneratePress Elements Hook whith the AAWP shortcode
You're possibly not getting what you believe you need back from get_the_category(). Assuming you're able to get the category id you can use get_cat_name() function to return the category name.
<?php
$cat_name = get_cat_name($category_id);
if ( ! empty($cat_name) ) {
$shortCode = '[aawp template="angebot-vertical" grid="3" bestseller="'.$cat_name.'" items="3" filter_items="20" orderby="percentage_saved" order="desc"]';
echo do_shortcode($shortCode);
}
?>

ACF field on Product Attribute show on single product page

I'm trying to add a logo to a Woocommerce product attribute (Brand Logo) and then display that brand logo on the Single Product page for each product tagged with that attribute.
I've tried the way I thought it would work however looks far too simple and it returns null.
<?php
function brandLogo(){
global $product;
?><p><?php the_field('brand_logo'); ?></p><?php
}
add_shortcode('brandLogo', 'brandLogo');
?>
I've got the ACF field to return URL of the image and have also tried using get_field as well as the_field.
My php knowledge is quite limited so I can't quite figure out how to get this one sorted.
Cheers.
I have changed the code and learned that it is important to use 'pa_' for product attribute names. So 'pa_yourattribute'. Here is the working code:
<?php
function brandLogo(){
global $product;
$terms = get_the_terms($post->ID , 'pa_brand');
if($terms){
foreach ($terms as $term){
?><img src="<?php the_field('brand_logo', 'pa_brand' . '_' . $term->term_id) ?>"/><?php
}
}
}
add_shortcode('brandLogo', 'brandLogo');
?>

get_post_meta - WordPress

This code is designed to add a button to specific posts using the get_post_meta function. How do I alter the get_post_meta function to display this button on a specific post? I have already tried changing its $post->ID parameter to '1464', which is the post ID I want to use.
function custom_listify_single_job_listing_actions_after() {
global $post;
$url = get_post_meta( $post->ID, 'your_custom_meta_key', true );
echo 'My Button';
}
add_filter( 'listify_single_job_listing_actions_after', 'custom_listify_single_job_listing_actions_after' );
If you only want to run this code on a specific post, you need to add an if statement to check for that post ID.
Your code would need to look similar to this:
if($post->ID == 1464){
$url = get_post_meta( $post->ID, 'your_custom_meta_key', true );
echo 'My Button';
}
This simply wraps the get_post_meta() function and echo statement so that both of these only run on the post you want them to. Any other post will ignore the code.

Wordpress: custom meta tag listing

Using Wordpress, I am trying to write a php script that, given a certain post ID, returns all the related custom tags (also named custom terms, i. e. these words that I can use to meta describe a post and are category-independent. For example, in this page the custom terms are "passabile" and "thriller").
Is there a way to do that, possibly some WP-method that takes care of custom tag revisions?
Thanks a lot
Use the get_post_meta() function. Say your tag is named 'custom_terms', you can do this:
$terms = get_post_meta( $id, 'custom_terms' );
If you are already on the post that you want to get that information for you can do:
$terms = get_post_meta( get_the_ID(), 'custom_terms' );
See here: http://codex.wordpress.org/Function_Reference/get_post_meta
$terms will be an array containing all of the values.
EDIT: If you are talking strictly about the tags of a post, you can try this:
$tags = get_the_tags();
This returns an array of tag objects that you can access by looping through them.
Use
<?php
$id = //post id
$taxonomy = //your custom texonomy name
get_the_term_list( $id, $taxonomy );
?>
for more info about get_the_term_list

Get custom fields values in filter on wp_insert_post_data

Hi all, thanks for reading.
Environment :
Wordpress + Advanced Custom Fields plugin
Problem :
I have searched for hours now and I can't seem to find the correct syntax to do the following:
When posting a new post, get custom field value in order to automatically replace the title of the post by that value. Example: I create a post and set '10am' in my 'time' custom field. The title of the post is automatically replaced by '10am'.
Example:
So I'm adding a filter with the following :
add_filter('wp_insert_post_data', 'change_title') ;
function change_title($data)
{
$time = XXX ; // How should I get this custom field value ?
$new_title = 'Topic created at'.$time ;
$data['post_title'] = $time ;
return $data;
}
It must be very simple but I have tried every function available on both WP and the plugin's documentations. I would be very thankful if anyone passing by gave me the solution.
Thanks in advance !
Tweak to Riadh's accepted answer (would add as a comment but haven't got enough rep yet):
As documented in the WordPress Codex wp_update_post includes the save_post hook so calling wp_update_post() inside the save_post hook creates an infinite loop. To avoid this, unhook then rehook your function like so:
add_action('save_post', 'change_title');
function change_title($post_id) {
$time = get_field('time',$post_id);
$post_title = 'Topic created at '. $time;
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'change_title');
// update the post, which calls save_post again
wp_update_post(array('ID' => $post_id, 'post_title' => $post_title));
// re-hook this function
add_action('save_post', 'change_title');
}
You can actually access the global $_POST variable for your field value , but i guess you can do it in a cleaner way by using the save_post action to update your post's title, eg:
add_action('save_post', 'change_title');
function change_title($post_id) {
$time = get_field('time',$post_id);
$post_title = 'Topic created at '. $time;
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'change_title');
// update the post, which calls save_post again
wp_update_post(array('ID' => $post_id, 'post_title' => $post_title));
// re-hook this function
add_action('save_post', 'change_title');
}
assuming that your ACF fieldname is "time".
Edit: Updated the answer as per Mark Chitty's answer.
You may try this
add_filter( 'wp_insert_post_data', 'change_title', '99', 2 );
function change_title($data , $postarr){
$custom_field = 'custom_filed_name';
$post_id = $postarr['ID'];
$time = get_post_meta( $post_id, $custom_field, true );
// Now you have the value, do whatever you want
}
Advanced Custom Fields creates a 'field key' for each custom field that is created. I was able to refer to this key value when trying to access the custom fields. The field key value can be found by viewing page source when viewing the post type within the Wordpress admin section.
Look for data-field-key. You will see a value similar to data-field-key="field_5847b00820f13" in the page source. Use this value when accessing the value in the $postarr argument in the wp_insert_post_data filter. The custom fields will be in a nested array named fields within the $postarr argument.
Alternatively, the field key value can be located by navigating to the Advanced Custom Fields / Export option from within the admin section. Once you are on the export page for Advanced Custom Fields, select the export to PHP option and you will see the value in the resulting PHP code.
In the example below, I am concatenating two Advanced Custom Fields and updating the post_title in the $data array returned from the function.
The result is that the post_title value will be saved to the database via the built in Wordpress save post logic.
add_filter('wp_insert_post_data', 'slb_set_title', '99', 2);
function slb_set_title ($data, $postarr){
if($data['post_type']==='slb_subscriber'){
$data['post_title'] = $postarr['fields']['field_5847b00820f13'] .' '.
$postarr['fields']['field_5847b03f20f14'];
}
return $data;
}

Categories