Search code examples
wordpresscontact-form-7

Add automatic contact form 7 to posts


I would like to insert the form automatically only in posts with the Events category

function contactform_for_every_post( $content ) {
  if ( is_singular('post') ) {
    $contactform = '[contact-form-7 id="109" title="Contact form 1"]';
    echo '<h3>Ich habe Interesse an Tobias</h3>';
    echo do_shortcode($contactform);
  }
}

add_action('x_before_the_content_end', 'contactform_for_every_post');

Solution

  • You can check if a post is within any given categories with

    in_category('catgory_name|id|array of categories')
    

    So this might might be a copy & paste solution

    function contactform_for_every_post( $content ) {
      if ( in_category('Events') ) {
        $contactform = '[contact-form-7 id="109" title="Contact form 1"]';
        echo '<h3>Ich habe Interesse an Tobias</h3>';
        echo do_shortcode($contactform);
      }
    }
    
    add_action('x_before_the_content_end', 'contactform_for_every_post');

    You can check https://developer.wordpress.org/reference/functions/in_category// for further information on this function