web-dev-qa-db-fra.com

le contenu de gform_after_submission apparaît immédiatement après <body>, pas dans le corps de la publication

J'ai la fonction suivante qui utilise avec succès les champs de formulaire (ID 13 et 18) à partir d'un formulaire Gravity (ID 18) en tant que paramètres dans wp_query. Ceci affiche une liste de publications avec des termes de taxonomie spécifiques qui correspondent à ceux choisis dans le formulaire:

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>"
;

 }

Le problème est que cela est inséré immédiatement après la balise 'body' plutôt que dans le post-body. Comment puis-je déplacer ceci à l'endroit où il devrait être?! Je pourrais bien être aller à ce sujet tout faux et je suis perplexe

1
Madebymartin

Je pense juste un vendredi soir (donc non testé), mais:

Vous exportez le résultat directement sur la page. Bien sûr, il va s'afficher juste après la balise body. Ce que vous devez faire, c'est stocker cet extrait HTML et ajouter/insérer dans le contenu à l'aide du filtre the_content comme suggéré par s_ha_dum.

Ceci n’a pas été testé, et tapé après quelques Pernods, mais:

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();
1
webaware

merci pour tous vos commentaires .. J'ai juste réussi à réaliser ce dont j'avais besoin (avec un grand merci à Alex de WPMU!), en utilisant des déclarations globales -

Ce n’est probablement pas la solution la plus élégante, mais c’est ce dont j'ai besoin. Ceci est en fait une version très simplifiée de ce que je fais et de cette façon, je sais comment le faire fonctionner!

Veuillez signaler tout problème avec ce que j'ai fait s'il y en a !?

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; 
} 
0
Madebymartin