• Resolved darnoldy

    (@darnoldy)


    Okay… so I’m a newb (I just learned about this forum at the Portland WordCamp last week).

    My site is a directory, and all of the posts only have one author (me). Each post does have a contact email field—most of those (not all) have an email address in them.

    When a comment is posted about one of the listings, I want the notice to go to the contact email, if there is one.

    I’ve cludged together this code:

    <?php
    /**
    * Plugin Name: TRT Custom Functions
    * Description: code snippets to add trt-specific functionality.
    * Version: 1.0
    **/

    if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
    }

    //// Send comment notifications to listings email contact address if it exists

    function trt_custom_comment_notification($comment_id) {
    // Get the comment details
    $comment = get_comment($comment_id);
    $post_id = $comment->comment_post_ID;
    $post = get_post($post_id);
    $meta_key = 'contact_email';
    $meta_value = get_post_meta($post_id, $meta_key, true);

    // Email to notify
    if (!empty($meta_value)) {
    $to = $meta_value;
    } else {
    $to = get_option('admin_email');
    }

    $subject = ‘People are commenting on’ . $post->post_title;

    // Construct email message
    $message = sprintf(
    "A new customer comment has been posted about '%s'.\n\n" .
    "Comment:\n%s\n\n" .
    "You can view it here: %s\n",
    $post->post_title,
    $comment->comment_content,
    get_comment_link($comment_id)
    );

    // Send the email
    wp_mail($to, $subject, $message);
    }

    add_action('comment_post', ‘trt_custom_comment_notification', 10, 1);

    Not only doesn’t it work, it won’t activate (because it has a fatal error)!

    What am I doing wrong?

    Thanks!

    –don



Viewing 12 replies - 1 through 12 (of 12 total)
  • You have 2 spelling mistakes in the code which both have the same cause. You have used the wrong single quotation marks both times.

    Wrong:

    $subject = People are commenting on . $post->post_title;
    add_action('comment_post', `trt_custom_comment_notification', 10, 1);

    Correct:

    $subject = 'People are commenting on' . $post->post_title;
    add_action('comment_post', 'trt_custom_comment_notification', 10, 1);

    The code is then at least syntactically correct and should also send an email. If the email does not arrive, check whether it is generated by WordPress at all. You can do this with mail logger plugins: https://de.www.ads-software.com/plugins/tags/email-log/

    Moderator bcworkz

    (@bcworkz)

    When checking your email account for contact notifications, don’t forget to check your spam folder. Mail generated by web servers can often be seen as spam. Unless your domain name has all the proper anti-spam header information, server mail will almost certainly end up as spam.

    Some mail clients can have a filter set up which basically prevents mail from a particular sender from being spammed. Simply placing the server’s From: address in your mail contacts isn’t 100% reliable in preventing server mail from being spammed.

    Thread Starter darnoldy

    (@darnoldy)

    @threadi —Thanks for that! I should have caught those myself (Guess my eyes are getting old).

    The script can now be activated, and it sends mail. —That’s the good news.

    However, it always sends the mail to the admin email address. Either I’m getting the meta value incorrectly, or there’s an error in the if statement! I’ll have to work on that.

    @bcworkz — Thanks for that! while I’m testing, I am sending the mail to an address with spam filtering turned off.

    Try to debug the script. Add var_dump() after some lines and exit before the email. Example:

    // Get the comment details
    $comment = get_comment($comment_id);
    var_dump($comment);
    $post_id = $comment->comment_post_ID;
    var_dump($post_id);
    $post = get_post($post_id);
    $meta_key = 'contact_email';
    $meta_value = get_post_meta($post_id, $meta_key, true);
    var_dump($meta_value);
    exit;

    You should be able to find out which value is where and why the e-mail is not being read.

    Also check directly in the database whether ‘contact_email’ is really assigned to the post in question as a post meta.

    Thread Starter darnoldy

    (@darnoldy)

    @threadi —well…. that was instructive! It seems that ‘contact_email’ is not a meta field (nor are any of the other custom fields in the post)!

    After some research, I tried:

    	$meta_value = get_post_custom_values('contact_email', $post_id);

    which returned NULL. I guess I need to go have a conversation with the support folks at the plugin I’m using to create the posts!

    Thanks for your help–I’ll report back when I get this solved.

    –don

    Thread Starter darnoldy

    (@darnoldy)

    It seems to me that this should be easier than it is turning out to be!

    I contacted Business Directory support (who has always been pretty helpful) and they replied that they don’t give coding help, and provided a list of a few developers that I could hire to do it for me.

    I also looked through their docs, and found an api call, wpbdp_get_form_field(ID) that looked promising. So I tried:

    	$meta_value = wpbdp_get_form_field( 7 );

    // Get the post details
    var_dump($meta_value);
    exit;

    and got:

    object(WPBDP_Form_Field)#2521 (14) { ["id":"WPBDP_Form_Field":private]=> int(7) ["type":"WPBDP_Form_Field":private]=> object(WPBDP_FieldTypes_TextField)#1286 (1) { ["name":"WPBDP_Form_Field_Type":private]=> string(9) "Textfield" } ["association":"WPBDP_Form_Field":private]=> string(4) "meta" ["shortname":"WPBDP_Form_Field":private]=> string(13) "contact_email" ["label":"WPBDP_Form_Field":private]=> string(13) "contact_email" ["description":"WPBDP_Form_Field":private]=> string(0) "" ["tag":"WPBDP_Form_Field":private]=> string(5) "email" ["weight":"WPBDP_Form_Field":private]=> int(31) ["validators":"WPBDP_Form_Field":private]=> array(1) { [0]=> string(5) "email" } ["display_flags":"WPBDP_Form_Field":private]=> array(1) { [0]=> string(10) "fieldlabel" } ["field_data":"WPBDP_Form_Field":private]=> array(3) { ["icon"]=> string(37) "dashicons|dashicons dashicons-archive" ["word_count"]=> int(0) ["supported_categories"]=> string(3) "all" } ["css_classes"]=> array(0) { } ["html_attributes"]=> array(0) { } ["validation_errors":"WPBDP_Form_Field":private]=> array(0) { } } 

    Which I sorta think I understand…but it occurs to me that this function doesn’t specify which post to get the info from—is that because its designed to be used within a loop?

    I also tried:

    	$meta_value = $post->wpbdp_get_form_field( 7 );

    That just gave a fatal error!

    Any advice would be very welcome!!

    Which plugin are you using? The functions don’t tell me anything, it seems to come from the plugin.

    Moderator bcworkz

    (@bcworkz)

    It looks like wpbdp_get_form_field() gets data regarding the form field object itself, not the field’s saved data for a particular listing. While it’s understandable that the plugin’s support doesn’t want to do coding help, maybe they could at least suggest which of their functions will get a listing’s email address, or at the very least where it’s kept within the database.

    You might be able to find it yourself through the phpMyAdmin app. If you know a specific email address that had been saved, use the app’s search function to find it. Being able to get the right email for any listing will not entirely resolve what you’re trying to do, but it’s an important step in the process.

    Moderator bcworkz

    (@bcworkz)

    @threadi — looks like it’s the plugin for this knowledgebase. Business Directory Plugin.

    Thread Starter darnoldy

    (@darnoldy)

    @threadi & @bcworkz

    Yeh, It is Business Directory plugin, and I had thought that I’d get more of a resonse that a copy paste from their “We don’t support” page.

    On the other hand, based on learning about variable dumps from threadi, I did some bear-poking. I managed to use “get_post_meta()” to see all of the meta tags foe the post. I found:

    ["_wpbdp[fields][7]"]=> array(1) { [0]=> string(21) "[email protected]" } 

    Which seems to be what I am looking for—I think I’m close, However, when I use:

    $meta_value = get_post_meta($post_id,'_wpbdp[fields][7]'.true);

    it returns “array(0) { }” instead of the string. Is there an “asString” command I should use?

    Look directly in the database to see what the metakey for the field where this e-mail is stored is called in the postmeta table. If this data record exists, you must use the metakey there for the query. If it doesn’t exist, this plugin stores the data somewhere else and you probably can’t access it with WordPress’ own functions.

    Thread Starter darnoldy

    (@darnoldy)

    Okay… it working now! Thank you for your help and your patience!

    –don

Viewing 12 replies - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.