• Resolved r1se

    (@r1se)


    Hi!

    I was wondering if it’s possible to get the id of the post where i’m displaying my form. I’ve tried everything and i’m sure i must be doing something wrong.

    I need to get the post id while doing this:

    add_action('wpcf7_before_send_mail', 'wpcf7_update_email_body');
    
    function wpcf7_update_email_body($contact_form) {
    
      $submission = WPCF7_Submission::get_instance();
      $url = $submission->get_meta( 'url' );
    
       if ( $submission ) {
       $mail = $contact_form->prop('mail');
       $mail['body'] .= '<p> '. $url .' </p>';
       $contact_form->set_properties(array('mail' => $mail));
      }
    }

    All I get is empy variables when trying to get the post’s id using queried object, global $post , etc.

    Basically I want to attach a custom field (im using ACF) to the mail body, so I just need the id so I can do something like this:

    `
    $extra_content = get_field(‘my_custom_field’ , $post_id)

    and then just add that variable to the first snippet I posted.

    Any help would be awesome.

    Thanks!

    https://www.ads-software.com/plugins/contact-form-7/

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

    (@takayukister)

    Look at the wpcf7_special_mail_tag function in includes/mail.php to see how it retrieves post id for [_post_id] special mail tag.

    Thread Starter r1se

    (@r1se)

    Hi Takayuki, thanks for the response!

    I was trying to use the code from there but had no luck.

    add_action('wpcf7_before_send_mail', 'wpcf7_update_email_body');
    
    function wpcf7_update_email_body($contact_form) {
    
      $submission = WPCF7_Submission::get_instance();
      $url = $submission->get_meta( 'url' );
      $unit_tag = $submission->get_meta( 'unit_tag' );
    
    	if ( $unit_tag
    		&& preg_match( '/^wpcf7-f(\d+)-p(\d+)-o(\d+)$/', $unit_tag, $matches ) ) {
    		$post_id = absint( $matches[2] );
    
    		if ( $post = get_post( $post_id ) ) {
    				$the_post_id = (string) $post->ID;
    				$the_post_name = $post->post_name;
    				$the_post_title = $html ? esc_html( $post->post_title ) : $post->post_title;
    				$test_variable = 'hai';
    		}
    	} 
    
      if ( $submission ) {
       $mail = $contact_form->prop('mail');
       $mail['body'] .= '<p> '. $url .' '. $the_post_id .' '. $the_post_name .''. $the_post_title .''. $test_variable .'</p>';
       $contact_form->set_properties(array('mail' => $mail));
      }
    }

    The only variable that gets printed on my mail is $url. That means that the “if ( $unit_tag….” statement is not working.

    Could you point me out on the right direction? I’m sure I must be doing something wrong.

    Thanks!

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    Can I see the form?

    Thread Starter r1se

    (@r1se)

    Sure!

    This is what I have on my form:

    [hidden hidden-300 "post_title"]
    
    [hidden hidden-144 "custom_field-contenido_noticias"]
    
    <p>Compartir con: [email compartir-email] </p>
    
    <p>[submit "Enviar"]</p>

    I’m not using the special tags since i’m outside of the main loop and this goes in my footer. I did try putting the form (php + shortcode) inside the loop, and special mail tags didn’t work.

    Im trying to add the content this way because of this problem:
    https://www.ads-software.com/support/topic/custom-field-send-as-html?replies=4

    Thanks for your help!

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    I meant the URL of the page, but it’s OK as I found the cause.

    I’m not using the special tags since i’m outside of the main loop and this goes in my footer.

    For the same reason as special mail tags, your code doesn’t work outside WP loop. The unit tag isn’t in wpcf7-f(\d+)-p(\d+)-o(\d+) format there.

    Besides, Contact Form 7 doesn’t support these “hidden” tags.

    [hidden hidden-300 "post_title"]
    
    [hidden hidden-144 "custom_field-contenido_noticias"]
    Thread Starter r1se

    (@r1se)

    Hmm I’m not sure I explained my self correctly. unit tag has a value, but it’s the form id, so it doesnt help either.

    Anyways, what I finally did to get this working is the following:

    add_action('wpcf7_before_send_mail', 'wpcf7_update_email_body');
    
    function wpcf7_update_email_body($contact_form) {
    
      $submission = WPCF7_Submission::get_instance();
      $url = $submission->get_meta( 'url' );
    
     // Super nice wordpress function
      $postid = url_to_postid( $url );
      // Get ACF content
      $custom_content = get_field('contenido_noticias' , $postid);	
    
       if ( $submission ) {
       	$mail = $contact_form->prop('mail');
       	$mail['body'] .= $custom_content;
       $contact_form->set_properties(array('mail' => $mail));
      }
    }

    Since i’m able to get the url of the post I can use the wordpress function url_to_postid to get the post id. Then I can do whatever I need! Simple as that ??

    Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Post ID Action wpcf7_before_send_mail’ is closed to new replies.