Personal email in comments for CPT - wordpress

I have a snippet that when a user write a comment, send an author email.
But I need that it happens only when some comment in a Custom Post type. ¿How change this code in my case?
$comment = get_comment( $comment_id );
$post_ID = $comment->comment_post_ID;
$author_ID = get_post_field( 'post_author', $post_ID );
$user_info = get_userdata($author_ID);
$email = $user_info->user_email;
$pTitle = $comment->post_title;
$cAuthor = $comment->comment_author;
$cContent = $comment->comment_content;
$cUrl = get_comment_link( $comment_id );
$to = $email;
$subject = '¡Tu examen, "'.$pTitle.'", ha sido corregido!' ;
$headers[] = 'Desde: mi email';
$headers[] = 'Content-Type: text/html; charset=UTF-8';
ob_start();
include("template/email_header.php");
?>
<p>Ya te tienes tu examen corregido <?php echo $pTitle; ?>. Esta es tu nota y comentario:</p>
<p><?php echo $pTitle; ?></p>
<p><strong>Comentario:</strong><br>
<?php echo $cContent; ?></p><br><br>
<p>Si necesitas volver a intentarlo vuelve a enviar el audio desde aquí. <strong>Volver a repetirlo</strong><br>
<p><strong>Singerfit</strong><br>
<?php
include("template/email_footer.php");
$message = ob_get_contents();
$message = stripslashes($message);
ob_end_clean();
//return $message;
wp_mail( $to, $subject, $message, $headers );
}
add_action('comment_post', 'notify_postauthor_comment', 11, 2);```

get post type of a post ID get_post_type( $post_ID );. You can set conditions based on the post type.

Related

Wordpress: Trigger a function when a new post published

I want a function that triggers when a new WordPress post publish.
add_action( 'publish_post', 'post_published_notification', 10, 2 );
function post_published_notification( $post_id, $post ) {
$author = $post->post_author; /* Post author ID. */
$name = get_the_author_meta( 'display_name', $author );
$email = get_the_author_meta( 'user_email', $author );
$title = $post->post_title;
$permalink = get_permalink( $post_id );
$edit = get_edit_post_link( $post_id, '' );
$to[] = sprintf( '%s <%s>', $name, $email );
$subject = sprintf( 'Published: %s', $title );
$message = sprintf( 'Congratulations, %s! Your article "%s" has been published.' . "\n\n", $name, $title );
$message .= sprintf( 'View: %s', $permalink );
$headers[] = '';
wp_mail( $to, $subject, $message, $headers );
}
I tried this one. I tried also different statuses such as save_post but I didn't get any email when the user add a new post and publish it. so it doesn't work. how can I solve this problem?
You use the publish_post hook. If get_post_meta() returns a value, it will trigger your actions to occur.
function run_on_post_publish( $post_id ) {
$post_data = get_post_meta( $post_id, 'run_once', true );
if ( ! empty( $post_data ) ) {
return;
}
// Run the action you wish to occur when a post is published
update_post_meta( $post_id, 'run_once', true );
}
add_action( 'publish_post', 'run_on_post_publish' );

Why isn't the URL the same for echo and for response in WordPress Remote

I've got this little shortcode
function starlight_count() {
$url = 'https://herokuapp.com/creditcount?contact_id=' . do_shortcode('[i4w_db__FirebaseID]');
$response = wp_remote_get( esc_url_raw( $url ) );
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
$response_message = wp_remote_retrieve_response_message( $response );
ob_start(); ?>
<?php
echo $url;
echo $response_message;
print_r($response); ?>
<?php
return ob_get_clean();
}
add_shortcode( 'starlight-count', 'starlight_count' );
The first echo returns
https://herokuapp.com/creditcount?contact_id=-KhwKGq4EYNf0GCiw8Kn
But the actual call returns a 500 error, and is trying to request data from
[url] => https://herokuapp.com/creditcount?contact_id=%5Bi4w_db__FirebaseID%5D
Why isn't it using the merged data URL?
This is because you are calling shortcode inside shortcode.
You need to do the logic like the following :
function starlight_count($atts) {
$params = shortcode_atts(array(
'pass' => '0',
), $atts );
$url = 'https://herokuapp.com/creditcount?contact_id='.$params['pass'];
$response = wp_remote_get( esc_url_raw( $url ) );
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
$response_message = wp_remote_retrieve_response_message( $response );
ob_start(); ?>
<?php
echo $url;
//echo $response_message;
// print_r($response); ?>
<?php
return ob_get_clean();
}
add_shortcode( 'starlight-count', 'starlight_count' );
$variable = do_shortcode('[i4w_db__FirebaseID]');
echo do_shortcode('[starlight-count pass='.$variable.']');

How to get the vendor_ids for each of the vendors with products on the cart page in woocommerce

global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
echo "<b>".$_product->post_title.'</b> <br> Quantity: '.$values['quantity'].'<br>';
$price = get_post_meta($values['product_id'] , '_price', true);
echo " Price: ".$price."<br>";
I have used the following to get data about the products on the cart and display it. is it possible to get the vendor of each product too and how can I do that in woo commerce? again from the cart page because I want to echo the vendor address back under each product on the cart page.
add_filter( 'woocommerce_get_item_data', 'wc_add_address_to_cart', 10, 2 );
function wc_add_address_to_cart( $other_data, $cart_item ) {
$post_data = get_post( $cart_item['product_id'] );
$post_data->post_author;
$vendor_id = $post_data->post_author;
echo '<br>';
$Add = 'Address: ';
$city = 'City: ';
$tel = 'Phone: ';
echo $test;
$user_id = $vendor_id;
// Get all user meta data for $user_id
$meta = get_user_meta( $user_id );
// Filter out empty meta data
$meta = array_filter( array_map( function( $a ) {
return $a[0];
}, $meta ) );
echo $Add;
print_r( $meta ['_vendor_address_1'] );
echo '<br>';
echo $city;
print_r( $meta['_vendor_city'] );
echo '<br>';
echo $tel;
print_r( $meta['_vendor_phone'] );
return $other_data;
}
Finally got what I was looking for. Just add that to the functions.php

WordPress Contact Form 7 Redirect via Functions.php

I have the following code as a function to add the form data to the database usermeta table then send an email which all works. The issue is the form stays stuck on the loading image and I can't find anyway to get it to redirect or display a confirmation message, any help would be much appreciated. I've tried using another function with wpcf7_mail_sent but nothing happens, tried additional settings for the form and am stuck.
add_action('wpcf7_before_send_mail', 'cf7import',1);
function cf7import() {
$title = $contact_form->title;
$submission = WPCF7_Submission::get_instance();
if ( $submission )
{
$posted_data = $submission->get_posted_data();
$formtitle = $cfdata->title; }
if ( $formtitle == 'Apply Form') {
}
global $wpdb;
$user_id = get_current_user_id();
update_user_meta( $user_id, 'prefix', $posted_data['prefix'] );
update_user_meta( $user_id, 'first_name', $posted_data['first-name'] );
update_user_meta( $user_id, 'middle_name', $posted_data['middle-name'] );
global $current_user;
get_currentuserinfo();
$email_address = 'contact#website.com';
// write the email content
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: text/html; charset=utf-8\n";
$header .= "From:" . $email_address;
$subject = 'New Application Form';
$message = "Hi,<br/><br/>".$posted_data['first-name'].' '.$posted_data['last-name']."<br/><br/>";
$message .= "Your application has been submitted successfully";
wp_mail($current_user->user_email, $subject, $message, $header);
}
The problem with your code is that $contact_form isn't defined.
You can use something like this:
add_action('wpcf7_before_send_mail', 'cf7import', 1);
function cf7import($contact_form) {
$submission = WPCF7_Submission::get_instance();
if ( $submission ){
$posted_data = $submission->get_posted_data();
$formtitle = $contact_form->title();
}
if ( $formtitle == 'Apply Form') {
global $wpdb, $current_user;
$user_id = get_current_user_id();
update_user_meta( $user_id, 'prefix', $posted_data['prefix'] );
update_user_meta( $user_id, 'first_name', $posted_data['first-name'] );
update_user_meta( $user_id, 'middle_name', $posted_data['middle-name'] );
get_currentuserinfo();
$email_address = 'contact#website.com';
// write the email content
$header = "MIME-Version: 1.0\n";
$header .= "Content-Type: text/html; charset=utf-8\n";
$header .= "From:" . $email_address;
$subject = 'New Application Form';
$message = "Hi,<br/><br/>".$posted_data['first-name'].' '.$posted_data['last-name']."<br/><br/>";
$message .= "Your application has been submitted successfully";
wp_mail($current_user->user_email, $subject, $message, $header);
}
}
Also made the code more readable and fixed some other issues (empty if conditional and undefined $header).
The code actually works as it should it was a conflict with the "WP Job Manager - Contact Listing" plugin that was stopping it from working

Get customers name in confirmation email

I am working with Woocommerce and I am about to make the order confirmation email.
It want the mail to say:
"Hi [customers name]"
How do you get Woocommerce to print the customers name?
You need the order object, so depending on what hook you are using it should be there. Try something like this:
add_action('woocommerce_order_status_completed','my_woo_email');
function my_woo_email($order_id){
$order = new WC_Order( $order_id );
$to = $order->billing_email;
$subject = 'this is my subject';
$message = 'Hi '.$order->billing_first_name.' '.$order->billing_email;$order->billing_last_name.', thanks for the order!';
woocommerce_mail( $to, $subject, $message, $headers = "Content-Type: text/htmlrn", $attachments = "" )
}
This is untested but should get you started
As of WooCommerce 3.x the $order object's properties should not be accessed directly as suggested in the previous answers. The proper way is now:
echo 'Hi ' . $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
Here's my starter example of getting a bunch of the WooCommerce customer info inside a WP_Query loop:
$args = array(
'post_status' => 'any',
'post_type' => 'shop_order',
'posts_per_page' => -1,
'order' => 'ASC'
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) {
$query->the_post();
$order = new WC_Order( $query->post->ID );
echo $order->billing_address_1;
echo $order->billing_address_2;
echo $order->billing_city;
echo $order->billing_company;
echo $order->billing_country;
echo $order->billing_email;
echo $order->billing_first_name;
echo $order->billing_last_name;
echo $order->billing_phone;
echo $order->billing_postcode;
echo $order->billing_state;
echo $order->cart_discount;
echo $order->cart_discount_tax;
echo $order->customer_ip_address;
echo $order->customer_user;
echo $order->customer_user_agent;
echo $order->order_currency;
echo $order->order_discount;
echo $order->order_key;
echo $order->order_shipping;
echo $order->order_shipping_tax;
echo $order->order_tax;
echo $order->order_total;
echo $order->payment_method;
echo $order->payment_method_title;
echo $order->shipping_address_1;
echo $order->shipping_address_2;
echo $order->shipping_city;
echo $order->shipping_company;
echo $order->shipping_country;
echo $order->shipping_first_name;
echo $order->shipping_last_name;
echo $order->shipping_method_title;
echo $order->shipping_postcode;
echo $order->shipping_state;
}
wp_reset_postdata();
Hopefully this will be helpful for someone else.
you can get customer data from $order object, something like this.
$customer = get_userdata( $order->get_customer_id() );
$name = $customer->display_name;

Resources