Pass dynamic post id to cf7 form - wordpress

I am using https://wordpress.org/plugins/tangible-loops-and-logic/ to show a list of posts. Each post also contains a Contact Form 7 form. That form has a dropdown which should be populated by an acf repeater field that is attached to each post.
My issue is that in order to retrieve the correct values I need to get the correct post id for each post. I know I can create a dynamic field in cf7 to show the post ID but I'm not really sure how I can pass that around so that I can retrieve the correct repeater.
My loop (simplified):
<Loop type=seminar count=3>
<Field title />
<div class="seminarContent">
...
</div>
[contact-form-7 id="619" title="Seminar"]
</Loop>
The select field of my form:
[select termine data:gigs]
my current code to retrieve the post ID which would work if it was on the single post page, but unfortunately not within a loop showing all posts on the same page as discussed here https://stackoverflow.com/a/71840727/6118046:
add_filter( 'wpcf7_form_tag_data_option', 'dd_filter_form_tag_data', 10, 3 );
function dd_filter_form_tag_data( $n, $options, $args ) {
// Get the current form.
$cf7 = wpcf7_get_current_contact_form();
// Get the form unit tag.
$unit_tag = $cf7->unit_tag();
// Turn the string into an array.
$tag_array = explode( '-', $unit_tag );
// The 3rd item in the array will be the page id.
$post_id = substr( $tag_array[2], 1 );
if ( in_array( 'gigs', $options, true ) ) {
$gigs = array();
if ( have_rows( 'termine', $post_id ) ) :
while ( have_rows( 'termine', $post_id ) ) :
the_row();
$gigs[] = get_sub_field( 'termin' );
endwhile;
endif;
$n = array_merge( (array) $n, $gigs );
}
return $n;
}

Seems to work if I use the previous version of the plugin, called "custom content shortcode". Then add a custom field (text) to my post in which I paste the cf7 shortcode (same for all posts) and then call the field in the frontend. Dropdown gets properly filled that way.

Related

ACF Value not updating as per frontend submit data

I am using ACF and Gravity Form with Post Creation Addon.
In GF i used radio fields, select fields to take input from frontend and save data into ACF select field or radio fields,
All data is saving correctly except the radio, select dropdowns.
When checking Post Meta all data is exactly showing which is entered in frontend but ACF field isn't updating.
I used Below Code as well but the correct ACF option isn;t showing.
function occassion_save( $post_id, $feed, $entry, $form ) {
// Checkboxes field id.
$field_id = 1;
// Get field object.
$field = GFAPI::get_field( $form, $field_id );
if ( $field->type == 'radio' ) {
// Get a comma separated list of checkboxes checked
$checked = $field->get_value_export( $entry );
// Convert to array.
$values = explode( ', ', $checked );
}
// Replace my_custom_field_key with your custom field meta key.
update_post_meta( $post_id, 'occassion', $values );
}
[![Gravity Form and Post Creation Addon, Campaign SUbmission][1]][1]
[![ACF Field in Backend woo product][2]][2]
[![Meta Data showing Correct Data][3]][3]
[1]: https://i.stack.imgur.com/NM3wz.png
[2]: https://i.stack.imgur.com/zYhBj.png
[3]: https://i.stack.imgur.com/xQMY9.png
How can i update ACF field exactly as my meta data value...

Can you edit WordPress custom post types directly from the archive list page?

Is there a way to edit WordPress custom post types directly from the archive list page. For example, you would use the "list" view to edit the posts and pages in that custom post type, including all o the custom fields.
Option 1: Using a plugin
If you're using ACF, the easiest way to do this would be with ACF Quick Edit Fields plugin.
Option 2: Using code
If you don't want to use a plugin, you need to:
Add columns to the list page
Get the fields and display it in these columns
You can add the following code to your theme's functions.php:
Add columns for the data you want displayed in the list
// Replace 'cpt_name' with your custom post type name
function add_custom_columns ( $columns ) {
$columns['custom_field_1'] = __( 'Custom Field 1 Column Name' );
$columns['custom_field_2'] = __( 'Custom Field 2 Column Name' );
return $columns;
}
add_filter ( 'manage_cpt_name_posts_columns', 'add_custom_columns' );
Get the fields and display it in the columns
function display_cf_columns ( $column, $post_id ) {
switch ( $column ) {
case 'custom_field_1':
echo get_post_meta ( $post_id, 'post_meta_1', true );
break;
case 'custom_field_2':
echo get_post_meta ( $post_id, 'post_meta_1', true );
break;
}
}
add_action ( 'manage_cpt_name_posts_custom_column', 'display_cf_columns', 10, 2 );

WORDPRESS User specific access via ID to specific contact forms CFDB7

I'm trying to implement in the code a way to show specific contact forms data to a particular user via ID but I'm having difficulty finding which part of the code to add it to.
$user_ID = get_current_user_id();
if ( $user_ID == ('2') ) {
I'm currently inside the admin-mainpage.php within the plugin files.
It's not as simple as I thought as it doesn't store the form names via db.
Any help would be appreciated.
Forms-list
It looks like Contact Forms 7 is your plugin for the forms portion while CFDB7 is an accompanying plugin that writes the form submissions to the WP database.
Get the Form ID
There are a couple ways to get the form ID. It looks like the easiest path is to look at the shortcode that the CF7 creates. You can see it in their documentation here. The code is something like [contact-form-7 id="4" title="Foo"] with id being the form ID (4 in this case).
Show the Right Form to the Right Person
Assuming you know the user ids and the related form ids you want to show them, you could write a very simple shortcode plugin to display the right forms for the right people. It'd be something like this untested code below.
//[user-form-display]
function user_form_display( $atts ){
$user_id = get_current_user_id();
if ($user_id == 2){
echo do_shortcode('[contact-form-7 id="4" title="Foo"]');
} else if ($user_id == 4){
echo do_shortcode('[contact-form-7 id="88" title="Bar"]');
}
}
add_shortcode( 'user-form-display', 'user_form_display' );
You could then put the shortcode in the regular post field and not edit either the CF7 plugin nor mess with the theme files.
You could also make the shortcode fancier and tie user ids to form ids directly in the shortcode arguments. That would take a bit more effort but is probably worth it.
Getting the Form Data
You could modify the $args to include form ids based on an association with a user id or multiple user ids. The form ids should be a field in that table. That's the example indicated below.
Alternately you could modify how the information is returned based on the same relationships by setting up the if/then statements in the $data_value lines. This is easier probably but messier in the long run.
function specfic_table_data()
{
global $wpdb;
$user_id = get_current_user_id();
if($user_id == 1){
$form_ids = array(4,6);//only returns forms with id 4 and 6 when it's user w id 1
}
$cfdb = apply_filters( 'cfdb7_database', $wpdb );
$data = array();
$table_name = $cfdb->prefix.'db7_forms';
$args = array(
'post_type'=> 'wpcf7_contact_form',
'order' => 'ASC',
'posts_per_page' => 10,
'post__in' => $form_ids,
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
$form_post_id = get_the_id();
$totalItems = $cfdb->get_var("SELECT COUNT(*) FROM $table_name WHERE form_post_id = $form_post_id");
$title = get_the_title();
$link = "<a class='row-title' href=admin.php?page=cfdb7-list.php&fid=$form_post_id>%s</a>";
$data_value['name'] = sprintf( $link, $title );
$data_value['count'] = sprintf( $link, $totalItems );
$data[] = $data_value;
endwhile;
var_dump($data);
}

Unable to save Query Vars with a post

I'm trying to save Query Vars to a post for later retrieval.
I'm using permalinks in this format: domain.com/%category%/%postname%/
Example:
I create a following page
domain.com/page-003/
I add Query Var called email to the page
add_query_arg('email', 'test#abc.com', 'domain.com/page-003/')
Now when I call
get_permalink($post_id);
I get
domain.com/page-003/
Instead of
domain.com/page-003/?email=test#abc.com
What am I missing? Aren't Query Vars saved with a post?
You want to save some meta data which you want to restore later on and add as query arg into the URL.
You need to first save it as post_meta like e.g. when you save post with that data. You use:
<?php update_post_meta(get_the_ID(), 'email_address', 'abc#mail.com'); ?>
More details: https://codex.wordpress.org/Function_Reference/update_post_meta
Then during the retrieval, you may hook into a HOOK early on like template_redirect or earlier of the post you can get post_meta to get the email and then add to query arg:
<?php $email = get_post_meta( get_the_ID(), 'email_address' ); ?>
Then
esc_url( add_query_arg( 'email', $email, get_permalink( get_the_ID() ) ) );
Something like that, code is untested, I just wrote here, you may please read detailed doc in codex for each function used above.
Update: How to update/fill Ninja form field from Meta Value:
add_filter( 'ninja_forms_render_default_value', 'wm_the_value' , 10 , 3);
function wm_the_value( $default_value, $field_type, $field_settings ) {
if( 'textbox' == $field_type && in_array('ref' , $field_settings)){
$default_value = get_post_meta(get_the_ID(),'_listing_mls', true);
}
return $default_value;
}
'ref' is field name in Ninja form.
'_listing_mls' is meta_key name from WP database.
Hope it works for you.

Woocommerce remove admin order item item meta

Im adding a custom item meta to every item with woocommerce_add_order_item_meta action.
I dont need to show this custom meta in the Order Detail, because it's an arry stringy that im using to print a pdf.
How can i remove this meta custom item? Is there some action to do it?
Thanks
I understand its a bit old question but I am answering for some other users who will have same issue in future.
If you want your order item meta to not display in admin order details page than you should append underscore (_) at the start of your meta name.
Example:
_custom_order_meta
The underscore trick no longer works. In Woo 3.x there is a hidden meta array:
add_filter('woocommerce_hidden_order_itemmeta',
array($this, 'hidden_order_itemmeta'), 50);
function hidden_order_itemmeta($args) {
$args[] = 'my_hidden_meta';
return $args;
}
It sounds like you need to keep it in order to print the PDF. If you override the order-details.php template you can possibly change:
$item_meta = new WC_Order_Item_Meta( $item['item_meta'], $_product );
to
$array = $item['item_meta'];
if( isset( $array['your_pdf_array_key'] ) ){ unset( $array['your_pdf_array_key'] ); }
$item_meta = new WC_Order_Item_Meta( $array, $_product );
EDIT
The wc_add_order_item_meta() function has 4 parameters as seen in the code:
function wc_add_order_item_meta( $item_id, $meta_key, $meta_value, $unique = false ) {
return add_metadata( 'order_item', $item_id, $meta_key, $meta_value, $unique );
}
If you choose a $meta_key with a preceding underscore, the meta will be automatically hidden from view on the checkout/order-received page, the My Order's list of the My account area, as well as in the admin's order overview page.
Therefore, I would suggest making your woocommerce_add_order_item_meta callback function look something like the following:
add_action( 'woocommerce_add_order_item_meta', '25979024_add_order_item_meta', 10, 3 );
function 25979024_add_order_item_meta( $order_item_id, $cart_item, $cart_item_key ) {
wc_add_order_item_meta( $order_item_id, '_pdf_something', 'hide this stuff' );
}

Resources