Gravity Forms Dynamically Populate Post/Page Category - php

I would like to be able to use the same form on several pages, and know which page a submitted form came from. For a long list of reasons, I need to do that based on category.
I found code to add categories to pages, works great.
But I can't figure out how to get Gravity Forms to dynamically populate a field with the category.
I've selected "Allow field to be populated dynamically" on the form, I've set the parameter name to "pagecategory"
Here's what I've got - it does nothing:
//Get Page Category - For Demo Form
add_filter("gform_field_value_pagecategory", "populate_pagecategory");
function populate_pagecategory($value){
global $post;
$value = get_the_category( $post->ID,'metakeyname',true);
return $value;
}

get_the_category() actually returns an array of all category that is assigned to that post. The code below works for me to fetch the name of first category only.
//Get Page Category - For Demo Form
add_filter("gform_field_value_pagecategory", "populate_pagecategory");
function populate_pagecategory($value){
global $post;
$categories = get_the_category( $post->ID,'metakeyname',true);
$value = $categories[0]->cat_name;
return $value;
}

I'm a bit surprised this type of need/question isn't more prevalent. I had to do this same thing, but for a custom taxonomy. In case anyone lands here in need of how to do Mash's answer, but with a custom taxonomy, the following is working for me. My custom taxonomy is "services," which you can see replaces 'metakeyname' in Mash's answer.
//Get Page Custom Taxonomy - For Gravity Forms
add_filter("gform_field_value_pagecategory", "populate_pagecategory");
function populate_pagecategory($value){
global $post;
$terms = get_the_terms( $post->ID,'services',true);
$value = $terms[0]->name;
return $value;
}

Related

Adding WordPress Posts to Elementor Pro Form Select field

I have a client who wants me to create a field in the form where the customer can select additional products to add to their quote. The products are set up as posts. I am using a WordPress installation, with Elementor Pro and Dynamic ooo plugins, and I was able to create a call to collect all the post titles using:
Using the Elementor Pro using Dynamic ooo Token: [query:post] (as seen in the image)
Here are the options in Elementor Pro Forms Module
This is what the form looks like when you try to select an option
The issue I am having is that it is bulking the post titles into one giant select item rather than single selection items separated by the post title. What I want to happen is that I can call all the post titles, and they will populate into separate the items into a multi-select where any of the post titles can be selected and added to their quote request. Does anyone have an idea of how to do this?
Here is the link to see what I am doing:
http://www.closeout.gbwcompanies.com/product-closeouts/stone-veneer-closeouts/blue-ridge-matterhorn/
Click on the blue button to Request a Quote and the form popup should show you what I am talking about. You will see the select list on the first step of the form with all the post titles in one long item.
Here is a link to the Dynamic ooo docs that I used to get me this far:
Dynamic ooo Docs for Tokens
Edited:
Here is a code I am currently trying but is not working:
function get_posts_title_filter($data) {
global $post;
$args = array( 'cat' => 4 );
$myposts = get_posts( $args );
$data = '<select select name="lstdate" id="prods" onchange="document.getElementById(\'prods\').value=this.value;"><option></option>';
foreach( $data as $title ) {
$output.=$title.'
';
}
foreach ( $myposts as $post ) : setup_postdata($post);
$title = get_the_title();
$output .= '<option value="'. $title .'">'. $title .' </option>';
endforeach;
$data .= "</select>";
return var_dump($data);
}
I just figured out the answer to my question. I am so sorry for not seeing this sooner. I found this article Tokens Collection Link
If you are looking to do this, use the following shortcode:
[query:posts|options]
i don't have the plugin. So i can test myself and give you final working solution. But from what i have after read the Dynamic ooo docs, this might be work:
open your theme's functions.php file.
insert this codes at very bottom (above php close tag "?>" if have) and save it:
function get_posts_title_filter($data) {
$output = '';
foreach ($data as $title) {
$output .= $title.'
';
}
return $output;
}
in your token setting, using this token:
[query:post|get_posts_title]
if you get any error, send back to me for debug and correction.

How to return nothing if there are no matches?

add_shortcode( 'subscribe-to-category', function() {
global $wpdb, $post;
$the_cat = get_the_category( $post->ID );
$category_name = $the_cat[0]->cat_name;
$id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM wptq_forms WHERE name = '{$category_name}';"));
return do_shortcode( '[newsletter_form id="' . intval( $id ) . '"]' );
} );
I am matching the name of the first category of the current post with form titles to append the correct form id into a form shortcode that is in my post template.
How do I write the code so, that if there are no matches, no shortcode will be displayed at all?
The issue is that currently if there are no matches, then id 0 will be assigned to the shortcode automatically.
But once I've put [subscribe-to-category] in my template, it seems impossible to totally remove the shortcode if there are no matches...
The issue is multiple categories. I have max 2-3 categories per post.
I want to show a separate subscribe form (button + mail field) for each category of the current post.
(That will notify by e-mail if new posts are posted in that category.)
So I will have to insert the code above 3 times, once checking for the first, then for the second, and then for the third category, and give them different shortcode names.
If there is no second or third category, then those shortcodes should not be displayed.
Is there any smarter way to achieve what I want?

Retrieve meta field values of ACF flexible content items using get_post_meta - WordPress

Thanks for your help in advance. Here's what I'm trying to achieve:
I have a custom post type called 'Campaigns' and I have a custom taxonomy called 'Countries' that is related to the campaign custom post type. When a user adds a new country to a campaign a new campaign post is generated that is the child of the current campaign. I'm duplicating the ACF fields that are assigned to the parent campaign and replicating the values in the child post, however I've run into an issue using the ACF flexible content fields. Here'a snippet of my code that is retrieving the parent post fields and updating the newly created ACF field in the child post with that value.
$action_text = get_post_meta($parent_id, 'action_text', true);
update_field('action_text', $action_text, $post_id);
I've tried doing this with flexible content, but I know I need to loop through and find what content blocks have been created. What is the best way to go about this?
// About Fields
$about_fields = get_post_meta($parent_id, 'content');
var_dump($about_fields);
$meta_key = // How to retrieve the flexible content keys
$meta_value_of_flexible_content = get_post_meta($parent_id, $meta_key);
if($about_fields) {
}
For clarification 'content' is the flexible container name. 'text_and_image' is an example name of one of the flexible content blocks I've created.
Thanks again for any insights.
I've tried doing this with flexible content, but I know I need to loop
through and find what content blocks have been created.
You could just use the get_field() and update_field() functions to duplicate any ACF fields, including Flexible Content fields.
So for example, to clone the whole content field:
$about_fields = get_field( 'content', $parent_id );
if ( $about_fields ) {
update_field( 'content', $about_fields, $post_id );
}
// How to retrieve the flexible content keys
foreach ( $about_fields as $arr ) {
echo 'Layout: ' . $arr['acf_fc_layout']; // e.g. "Layout: text_and_image"
// The rest of items in `$arr` are the SUB-fields of that specific layout as
// identified by the `$arr['acf_fc_layout']`, which is the layout's name. So
// if you have two SUB-fields named `text1` and `image1` respectively, then
// these items are set: `$arr['text1']` and `$arr['image1']`
}
Additional Code
To clone all ACF fields:
$fields = get_fields( $parent_id );
foreach ( $fields as $name => $value ) {
update_field( $name, $value, $post_id );
}
Additional Note
I'd change this to use the get_field() function:
$action_text = get_post_meta($parent_id, 'action_text', true);
So:
$action_text = get_field('action_text', $parent_id);

Display Advanced Custom Field for Woocommerce applied to subcategory on parent category

I have an advanced custom field set up to show on a woocommerce subcategory that allows the user to define a colour via the color picker field.
This field will apply that colour to a number of elements related to that sub category (Styling the sub category thumbnail, the product page itself etc).
Im currently using as per the the ACF documentation this code to pull the field in and display it on the subcategory page:
$term = get_queried_object();
$color = get_field('colour', $term); // Get ACF Field
This works fine until it comes to the parent category for the sub pages. I am unable to call the field in for the sub categories of the parent. I understand I need to use get_terms(). I am unable ot get that working though.
This is some code I found which I have added to the loop on content-product_cat.php. However it just breaks the woocommerce loop. What would I need to do to this code to get the parent category page to show all the child subcategories each with its related color field?
// current term
$current_term = get_queried_object();
// child terms
// this returns an array of terms
$args = array(
'taxonomy' => 'YOUR TAXONOMY HERE',
'parent' => $current_term->term_id,
// you may need other arguments depending on your needs
);
$child_terms = get_terms($args);
// you need to maybe loop through the child terms gotte
// to pick which one you want to use
// I'm assuming that you only want to use the first one
$child_term = false; // set it to false to begin with
// we'll use this later
if ($child_terms) {
$child_term = $child_terms[0];
}
// make a decision
if ($child_term) {
// get field value(s) from child term
$color = get_field('color', $child_term);
} else {
// get field value(s) from current term
$color = get_field('color', $current_term);
}
// do something with the values
echo $color;
I found the solution here:
https://wordpress.stackexchange.com/a/341632
add_action('woocommerce_after_subcategory_title', 'wpse_add_custom_text_under_category_title', 10);
function wpse_add_custom_text_under_category_title($category) {
$term_id = 'product_cat_'.$category->term_id;
the_field('krotki_opis_kategorii', $term_id);
}
From Alex Uleberg:
get_queried_object_id on shop archive page it will return the id of the page and not the category. When you use the hook, the $category object will be passed in through the hook.

Wordpress query_posts category dropdown filter

I'm develeoping a dropdown menu for wordpress that is filtering diferent categories from posts.
I'm currently using query_posts function like this:
query_posts( array('category__and'=>array($_GET['operation'],$_GET['type'])));
The get $_GET['operation'] and $_GET['type'] are passed obviously by get params from the form dropdown menu.
When I pass the 2 values throught the form the query runs correctly, it displays the posts within the correct categories selected, all good.
The trouble comes when at the form I'm not defining any one of the get values, so the get from the url is like empty.
Example:
operation=4
type=2
It Runs correctly.
Trouble:
operation=""
type=2
The query or whatever i can't see breaks and displays that there are no results.
I hope if there is any way to check if any value is empty and exclude it from the array?
Anything like:
query_posts( array('category__and'=>array(
if($_GET['operation']!=""){
$_GET['operation'],
}
$_GET['type']
))
);
Please help!
Something like this should do what you need.
$args = array();
if (!empty($_GET['operation']))
$args[] = $_GET['operation'];
if (!empty($_GET['type']))
$args[] = $_GET['type'];
if (!empty($args))
query_posts( array('category__and'=>$args));
else
query_posts();

Categories