gform_after_submission content appears immediately after <body>, not in post body

I have the following function that successfully uses form fields (IDs 13 and 18) from a Gravity Form (ID 18) as parameters within wp_query.
This displays a list of posts with specific taxonomy terms that match those chosen in the form:

add_action("gform_after_submission_18", "set_post_content", 10, 2);
function set_post_content($entry, $form){

    //getting post
    $post = get_post($entry["post_id"]);
    $concern = $entry[13];
    $sensitivity = $entry[18];

    $loop = new WP_Query( array( 
        'post_type' => 'recommended',    
        'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'concern',
            'field' => 'slug',
            'terms' => $concern
        ),
        array(
            'taxonomy' => 'sensitivity',
            'field' => 'slug',
            'terms' => $sensitivity
        )
        ),

        'orderby' => 'title',
        'posts_per_page' => '-1', 
        'order' => 'ASC'
      ) 
   ); ?>

//THIS BIT DISPLAYS THE CORRECT LOOP BUT IT APPEARS IMMEDIATELY AFTER <BODY> RATHER THAN IN THE POST CONTENT
   <ul><?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
   <li><?php the_title(); ?></li>
   <?php endwhile; ?></ul>
   <?php 


//THIS BIT DISPLAYS THE CHOSEN FORM FIELD INSIDE MY POST CONTENT - WRONG CONTENT, RTIGHT POSITION
 $post->post_content = 
"<ul>" . 
$concern . 
"<br/> sensitivity: " . 
$sensitivity . 
" </ul>"
;

 }

The problem is that this is being inserted immediately after the ‘body’ tag rather than into the post body.
How do I move this to where it should be?!
I could well be going about this all wrong and I’m stumped

Related posts

Leave a Reply

2 comments

  1. Just thinking on a Friday night (so untested), but:

    You are outputing the result directly to the page. Of course it is going to display straight after the body tag. What you need to do is store that HTML snippet, and append/insert into the content using the_content filter as s_ha_dum suggested.

    This is untested, and typed after a few Pernods, but:

    class wpse_95891 {
    
        protected $post_content;
    
        public function __construct() {
            add_action("gform_after_submission_18", array($this, "set_post_content"), 10, 2);
            add_filter("the_content", array($this, "the_content"));
        }
    
        /**
        * @param array $entry the Gravity Forms entry/lead "object"
        * @param array $form the Gravity Forms form "object"
        */
        public function set_post_content($entry, $form) {
            // insert all of your query logic here...
    
            // grab the output and store in a field for later
            ob_start();
            ?>
            <ul><?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <li><?php the_title(); ?></li>
            <?php endwhile; ?>
            </ul>
            <?php
            $this->post_content = ob_get_clean();
        }
    
        /**
        * @param string $the_content post/page content
        * @return string
        */
        public function the_content($content) {
            if (!empty($this->post_content)) {
                $content .= post_content;
            }
    
            return $content;
        }
    }
    
    new wpse_95891();
    
  2. thanks for all your input..
    I just managed to achieve what I needed (with massive thanks to Alex at WPMU!), using global declarations –

    Probably not the most elegant solution but it does what I need. This is actually a much simplified version of what I’m doing and this way I know how to make it work!

    Please do point out any issues with what I’ve done if there are any!?

    add_action("gform_after_submission_18", "recommendations", 10, 2);
    function recommendations($entry, $form){
    
    global $concern;
    global $sensitivity;
    
    $post = get_post($entry["post_id"]);
    $concern = $entry[13];
    $sensitivity = $entry[18];
    
    add_filter("the_content", "recommended");
    }
    
    
    
    
    
    function recommended() {
    
    global $concern;
    global $sensitivity;
    
    echo 'Here are your recommendations, based on your chosen options: <br>';
    echo 'Sensitivity: ';
    echo $sensitivity;
    
    echo '<br>Concern: ';
    echo $concern;
    echo '<br><br>';
    
    
    $loop = new wp_query( array( 
        'post_type' => 'recommended',
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'concern',
                'field' => 'slug',
                'terms' => $concern
            ),
            array(
                'taxonomy' => 'sensitivity',
                'field' => 'slug',
                'terms' => $sensitivity
            )
        ),
        'orderby' => 'title',
        'posts_per_page' => '-1', 
        'order' => 'ASC'
    ) );
    
    
    
    while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
    <div class="">
    <?php the_title(); ?><br>
    <?php if ( has_post_thumbnail() ) { the_post_thumbnail("thumb"); } ?>
    </div>
    
    <?php endwhile; 
    }