I want to check if select field changed it's value after post editing, and if it is, then to send an email to admin.
I saved previous value to a variable $pre_status_eksperimenta using acf/pre_save_post, like this:
function action_pre_post_update( $post_id ) {
$pre_status_eksperimenta = get_post_meta($post_id, 'status', true);
};
add_action( 'acf/pre_save_post', 'action_pre_post_update', 10, 1 );
When I var_dump($pre_status_eksperimenta) I get correct value, wich means it works.
Then I want to pass that to a acf/save_post hook and check if there was a change, but now when I var_dump($pre_status_eksperimenta) I get NULL
function status_change_notification($ID) {
var_dump($pre_status_eksperimenta);
die();
}
add_action( 'acf/save_post', 'status_change_notification', 10, 1);
I think its about variable scope. You should global it when you use it in another function.
function action_pre_post_update( $post_id ) {
global $pre_status_eksperimenta;
$pre_status_eksperimenta = get_post_meta($post_id, 'status', true);
};
and then
function status_change_notification($ID) {
global $pre_status_eksperimenta;
var_dump($pre_status_eksperimenta);
die();
}
Related
I need to add my ACF field to a custom function. I have tried this but there is syntax error.
add_action( 'flatsome_custom_single_product_1', function () {
echo <?php the_field('designer'); ?>;
} );
When you are calling the_field('designer') outside the actual post loop, you need to pass the post ID as well.
Here is the Ref.
For your case, you can achieve this by using the code like below
add_action( 'flatsome_custom_single_product_1', function () {
global $post;
echo get_field('designer', $post->ID);
} );
I would like to send a confirmation email to a dynamic recipient. This is in the functions.php file. The below code works, but when I try to replace the static email with a variable, it has an error. How do I properly query the post_meta women_email and dynamically insert it in place of the static gmail address?
Example page: https://www.ohioacc.org/women/sandra-nichols/
function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
$women_email = get_post_meta( get_the_ID(), 'women_email', true);
//$dynamic_email = $women_email; THIS DOES NOT WORK
$dynamic_email = "myemail#gmail.com"; //THIS WORKS
$properties = $contact_form->get_properties();
$properties['mail']['recipient'] = $dynamic_email;
$contact_form->set_properties($properties);
return $contact_form;
}
add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );
Within the contact form, it doesn't let you get the $post->ID of the parent post. This data is stored in the form's meta. It's a hidden field passed in the form.
If you inspect your form from the front end, you'll see this For example:
<input type="hidden" name="_wpcf7_container_post" value="2730">
To get this, you access the $submission->get_meta('$value') method.
This will work for you, given that the women_email is properly formatted as an email address. Also note that before_send_mail is an "ACTION" and not a "FILTER"
Below is not tested, but should work. Put this in functions.php
function wpcf7_before_send_mail_function($contact_form, &$abort, $submission){
// This gets your post_id
$post_id = $submission->get_meta('container_post_id');
$properties = $contact_form->get_properties();
$properties['mail']['recipient'] = get_post_meta( $post_id, 'women_email', true);
$contact_form->set_properties($properties);
}
add_action( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );
Contact Form 7 allows you to create an auto-responder, by selecting Mail(2), which is located under the Mail tab. You'll need to edit the relevant form:
Contact Form 7 Mail Tab,
Contact Form 7 Mail(2).
You can then use your form's variable to populate the "To" field:
Contact Form 7 auto-responder field
Are you certain you're passing the post ID in correctly?
get_the_ID() needs proper context, in this case, the WP Loop
I'm assuming this is a function in your functions.php.
// Gets global $post object
global $post;
$email = get_post_meta( $post->ID, 'women_email', true);
$properties = $contact_form->get_properties();
$properties['mail']['recipient'] = $email;
$contact_form->set_properties($properties);
return $contact_form;
This should do the trick.
I need to pass a product ID that is stored as var inside a filter to another function.
I tried this and it doesn't pass the ID:
PHP
$has_option_id = null;
function wc_add_to_cart_message_filter($message, $product_id = null) {
$GLOBALS['has_option_id'] = $product_id;
return $message;
}
add_filter ( 'wc_add_to_cart_message', 'wc_add_to_cart_message_filter', 10, 2 );
function above_cart_js() {
$product_id = $GLOBALS['has_option_id'];
echo $product_id; // Outputs NULL (nothing)
}
add_action ( 'woocommerce_before_cart', 'above_cart_js', 5 );
Is there a way to pass the ID to the other function?
As your given action and filter don't runt at the same HTTP request, they can't global variable to each other. One of them is usually run by AJAX, another in is just Cart Template Hook. So, you can do it by Cookie Storage. First store just added product ID to Cookie, then get it from there.
function wc_add_to_cart_message_filter($message, $product_id = null) {
setcookie('just_added_product', array_keys($product_id)[0], time() + (600), "/");
return $message;
}
add_filter ( 'wc_add_to_cart_message_html', 'wc_add_to_cart_message_filter', 10, 2 );
function above_cart_js() {
echo $_COOKIE["just_added_product"];
}
add_action ( 'woocommerce_before_cart', 'above_cart_js', 5 );
I have been using this custom function below in the previous versions of WooCommerce in order to pre-fill the City and ZIP code fields:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_city']['default'] = 'Beverly Hills';
$fields['billing']['billing_postcode']['default'] = '90210';
return $fields;
}
It has been working great until the new WC updates.
The city still works, but the default ZIP code field doesn't seem to work anymore. It doesn't automatically pre-polulate the value.
Anything changed? Is there any other workaround for this?
Thanks
Setting a value for 'post-code' fields doesn't work anymore as there is an autocomplete feature. Even when disable "autocomplete", this doesn't work. So the workaround is to use jQuery in this case.
So your code is going to be:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields', 10, 1 );
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_city']['default'] = 'Beverly Hills';
$fields['billing']['billing_postcode']['autocomplete'] = "off"; // Removing autocomplete
return $fields;
}
add_action( 'woocommerce_after_checkout_form' , 'my_custom_checkout_field_postcode' );
function my_custom_checkout_field_postcode( ) {
?>
<script>
(function($){
$('#billing_postcode').val('90210');
})(jQuery);
</script>
<?php
}
This will set correctly your the desired value in "post-code" checkout billing field.
The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Not necessary have to use java-script when you use the woocommerce callback made for the job: 'woocommerce_checkout_get_value'.
The 'woocommerce_after_checkout_form' call is made for setting the attributes of the check out fields, such as disabling the autocomplete.
The point to note is this function will be repeatedly called for each and every field within the check out form. So you switch on the field and return the value you wish assigned to the check-out form:
Based on your above code. here ya go...
function populating_checkout_fields ($fields, $input)
{
global $woocommerce;
switch($input)
{
case 'billing_city':
$FieldValue = 'Beverly Hills';
return $FieldValue;
break;
}
return $fields; // return the default value
}
add_filter( 'woocommerce_checkout_get_value', 'populating_checkout_fields', 10, 2 );
function ModifyAutoComplete($fields)
{
$fields['billing']['billing_postcode']['autocomplete'] = null;
}
add_filter( 'woocommerce_checkout_fields' , 'ModifyAutoComplete', 10, 1 );
Hello I am trying to have a default featured image set for post_type = 'post' only excluding post_type = 'page'.
I wrote the following code in the functions file of the child theme but i keep getting this error:
Notice: Trying to get property of non-object in
/home/ossettto/public_html/wp-content/themes/sport-child/functions.php
on line 18
function wpforce_featured()
{
global $post;
$post_type = get_post_type($post->ID);
if ($post_type === 'post')
{
$already_has_thumb = has_post_thumbnail($post->ID); // If post have a featured image use that.
if (!$already_has_thumb)
{
//If post does not have a featured image then get the first post image and set as featured image.
$attached_image = get_children("post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1"); // Number 1 relates to taking post image number 1 and adding it as a featured image.
if ($attached_image)
{
foreach ($attached_image as $attachment_id => $attachment)
{
set_post_thumbnail($post->ID, $attachment_id);
//$attachment_id = attachment_url_to_postid( $image_url );
//echo $attachment_id;
}
}
else
{
set_post_thumbnail($post->ID, '27264'); // Find attachment media id by right clicking the image in the Media library and selecting inspect element. Look for the data-id number. This number is then added to the post id.
}
}
}
}
//end function
add_action('the_post', 'wpforce_featured');
add_action('save_post', 'wpforce_featured');
add_action('draft_to_publish', 'wpforce_featured');
add_action('new_to_publish', 'wpforce_featured');
add_action('pending_to_publish', 'wpforce_featured');
add_action('future_to_publish', 'wpforce_featured');
Any help will be appreciated.
Thanks.
Two things are unclear:
Why you are to do this in the_post, save_post, and the other hooks all together.
It would be helpful to know which line is line 18, but I'm guessing it's this line: $post_type = get_post_type( $post->ID );.
However, the reason you are getting the notice is that these actions don't necessarily all have the $post object ready for you to global $post. Further, these actions all have different function signatures, passing $post as a parameter in different places.
Given all the filters you are hooking into, you need to create an "abstraction" or "wrapper" around your function so that you can call it properly with $post in the proper position of the arguments.
Look at the docs to see examples of where $post is being passed:
the_post action - passes as ONLY parameter
save_post action - passes as SECOND parameter
draft_to_published (and other hooks) - passes as THIRD parameter
// New function that accepts proper parameters for save action
function wpforce_featured_on_save( $post_id, $post, $update ) {
// No need to duplicate code. Instead, call your original function
// passing it the $post parameter
wpforce_featured_status( $post );
}
// New function that accepts proper parameters for these actions
function wpforce_featured_on_status_change( $new, $old, $post ) {
// No need to duplicate code. Instead, call your original function
// passing it the $post parameter
wpforce_featured( $post );
}
// Your original function with slight modifications
// NOTE: ONLY accepts $post object - no global post
function wpforce_featured( $post ) {
// REMOVED global $post - not helpful here
$post_type = get_post_type( $post->ID );
// ... the rest of your code here
}
// Your original hook, it's passing the $post_object parameter
add_action('the_post', 'wpforce_featured');
// The save hook, which passes parameters - modified to call a different function
add_action('save_post', 'wpforce_featured_save', 10, 3);
// The status change hooks, which pass parameters - modified to call a different function
add_action('draft_to_publish', 'wpforce_featured_on_status_change', 10, 3);
add_action('new_to_publish', 'wpforce_featured_on_status_change', 10, 3);
add_action('pending_to_publish', 'wpforce_featured_on_status_change', 10, 3);
add_action('future_to_publish', 'wpforce_featured_on_status_change', 10, 3);