Home › Forums › Add-ons › Options Page › Use shortcode in textarea on options page in loop
I have an option page where you can save an email body text to send out to customers. To make it personal I use shortcodes within the body text like this:
Dear [yl_first_name],
How are you today?
Kind regards,
Team X
————————-
The email sends out with WordPress cronjob and all works fine except for 1 thing:
Because ‘officially’ the FIRST NAME is NOT called in the below WordPress loop >> Its the shortcode that is called (via the option page) in the loop…
I always get the same name value (latest post in line).
Here’s my EMAIL SEND code:
// Follow-up e-mail met reviewverzoek
function yl_follow_up_email_review_function() {
$review_days_after = get_field(‘booking_settings_review_delay’, ‘booking_settings’);
$review_url = get_field(‘booking_settings_review_url’, ‘booking_settings’);
if ($review_days_after && $review_url) {
$bookings = array(
‘posts_per_page’ => -1,
‘post_type’ => ‘booking’,
‘meta_query’ => array(
‘relation’ => ‘AND’,
array(
‘key’ => ‘booking_status’,
‘value’ => array(‘confirmed’, ‘changed’),
‘compare’ => ‘IN’,
),
array(
‘key’ => ‘booking_send_mail’,
‘value’ => ‘ja’,
‘compare’ => ‘=’,
),
),
);
// De Query
$query = new WP_Query($bookings);
// De Loop
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// Controleer of de poststatus ‘publish’ is
if (get_post_status() == ‘publish’) {
// Verkrijg aangepaste velden van de huidige boeking
$to = get_field(‘booking_email’);
// Zorg ervoor dat deze velden bestaan in je booking_settings
$booking_settings_id = ‘booking_settings’; // De ID van de booking settings post
$sender_name = get_field(‘booking_email_from_name’, $booking_settings_id);
$sender_email = get_field(‘booking_email_from_email_address’, $booking_settings_id);
$subject = get_field(‘booking_email_subject_review’, $booking_settings_id);
$message = get_field(‘booking_email_message_review’, $booking_settings_id); // the above body text with the shortcode inside
$headers = array(
‘From: ‘ . $sender_name . ‘ <‘ . $sender_email . ‘>’,
‘Content-Type: text/html; charset=UTF-8’
);
// Verzend de e-mail en log eventuele fouten
if (wp_mail($to, $subject, $message, $headers)) {
error_log(“Email successfully sent to $to for booking ID: ” . get_the_ID());
} else {
error_log(“Failed to send email to $to for booking ID: ” . get_the_ID());
}
// Update met nieuwe waarde
update_field(‘booking_status’, ‘completed’);
// Pauzeer 10 seconden voordat de volgende e-mail wordt verzonden
sleep(10);
}
// Reset de postdata
wp_reset_postdata();
}
}
}
}
add_action(‘yl_follow_up_email_review’, ‘yl_follow_up_email_review_function’);
The Shortcode code:
// Get ACF ‘First name’ values via shortcode
function yl_booking_first_name_shortcode() {
global $post; // Haal de globale $post variabele op
return get_field(‘booking_first_name’, $post->ID);
}
add_shortcode(‘yl_booking_first_name’,’yl_booking_first_name_shortcode’);
—————-
So how can I change the code so the correct name in each email is showing?
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.