• Resolved geertvanderheide

    (@geertvanderheide)


    I’ve been using the wpcf7_mail_components filter to modify the recipient of a form dynamically. It’s based on a custom field set for the post or page where the form is sent from. I’m extracting the post ID with wpcf7_special_mail_tag(). This works well if the form is used on any post or page, but it fails when used on a category page. I have the custom field set up for both pages / posts and categories, and I need the form to work on both.

    My question: how do I find the ID of the category page that the form is sent from?

    Code:

    function set_question_form_recipient($components, $form, $object) {
      if ($form->id() == 102) :
        // Get the post ID
        $post_id = wpcf7_special_mail_tag('', '_post_id');
      
        // Get the recipient e-mail address from an Advanced Custom Field
        $recipient_email = get_field( 'name_of_acf_field', $post_id );
    
        // Here I need a solution for finding the category page ID, if the form was sent from a category page. From there I can find the category custom field to set the recipient.
    
        if ($recipient_email) :
          // Set the recipient
          $components['recipient'] = $recipient_email;
        endif;
      endif;
      
      // Return the modified array
      return $components;
    }
    add_filter('wpcf7_mail_components', 'set_question_form_recipient', 10, 3);
Viewing 1 replies (of 1 total)
  • Thread Starter geertvanderheide

    (@geertvanderheide)

    Found an answer to my own question. To anyone who needs the category page ID that a form was sent from, inside the wpcf7_mail_components filter, this could be useful. It requires that you’ve set a category base in the permalinks. In this example, the category base is “category”.

    $url = wpcf7_special_mail_tag('', '_url');
    $cat_base = '/category/';
      
    if (strpos($url, $cat_base) !== false) :
      $cat_slug = substr($url, strpos($url, $cat_base) + strlen($cat_base));
      if ($cat_slug) :
        $cat = get_term_by('slug', $cat_slug, 'category');
        $cat_id = $cat->term_id;
      endif;
    endif;
Viewing 1 replies (of 1 total)

The topic ‘Need help getting cat ID inside wpcf7_mail_components’ is closed to new replies.