Support » Plugin: Contact Form 7 » How to use special mail tag [_post_id] in hook before_send_mail

  • Resolved mileu


    Here’s what I want to accomplish:

    I have a page with contact form.
    I want to get the post id of the page and include that data in the mail using the hook before send:

    function modify_wpcf7_before_send_mail ( $components, $number ) {
    $components[ ‘body’ ] .= get_the_ID();
    return $components;

    add_action(“wpcf7_mail_components”,’modify_wpcf7_before_send_mail’, 10, 2 );

    Replacing the subject and recipient works for me but getting the post id is giving me an empty value.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Takayuki Miyoshi


    You don’t need to do that because the special mail tag [_post_id] does the same thing. Simply add [_post_id] into the message body.

    See Special Mail Tags

    Actually, what I was trying to do is to get the post id to get the custom field “email” i added in the post where the contact form 7 is.

    I would like to create 1 contact form only. Then modify the email recipient depends on the email added in a post.

    Plugin Author Takayuki Miyoshi


    Look at contact-form-7/includes/mail.php and how it retrieves [_post_id] value.

    Got it! Thanks for the help!

    Hi thank you for your plugin I use it all the time. I cannot get the [_post_name] to work, it just writes the shortcode. I’m using it on a form in a woocommerce template, should this be a problem? I have entered the tag into the subject and message body in the mail section.

    Any help greatly appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to use special mail tag [_post_id] in hook before_send_mail’ is closed to new replies.