Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the following code that runs two wp-query loops upon form submission to display specific posts depending on the users choices in the form (The form fields are radio buttons with values that match the possible taxonomy terms for the posts):

add_action("gform_after_submission_18", "recommendations", 10, 2);
function recommendations($entry, $form){
global $age;
global $skintype;

$post = get_post($entry["post_id"]);
$skintype = $entry[24];
$age = $entry[25];

add_filter("the_content", "recommended");
}


function recommended() {
global $age;
global $skintype;

/* CLEANSER / TONER LOOP */
$cleansetoneloop = new wp_query( array( 
    'post_type' => 'recommended',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'age_group',
            'field' => 'slug',
            'terms' => $age
        ),
        array(
            'taxonomy' => 'skintype',
            'field' => 'slug',
            'terms' => $skintype
        ),
        array(
            'taxonomy' => 'recommendation_type',
            'field' => 'slug',
            'terms' => 'cleanser-toner'
        )
    ),
    'orderby' => 'title',
    'posts_per_page' => '-1', 
    'order' => 'ASC'
) );

while ( $cleansetoneloop->have_posts() ) : $cleansetoneloop->the_post(); ?>
<div class="recommend cleansertoner">
<h2>Your recommended cleansing / toning option:</h2>
<?php the_title(); ?><br>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail("thumb"); } ?>
</div>
<?php endwhile; 

/* EXFOLIATOR LOOP */
$exfoliatorloop = new wp_query( array( 
    'post_type' => 'recommended',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'age_group',
            'field' => 'slug',
            'terms' => $age
        ),
        array(
            'taxonomy' => 'skintype',
            'field' => 'slug',
            'terms' => $skintype
        ),
        array(
            'taxonomy' => 'recommendation_type',
            'field' => 'slug',
            'terms' => 'facial-exfoliator'
        )
    ),
    'orderby' => 'title',
    'posts_per_page' => '-1', 
    'order' => 'ASC'
) );

while ( $exfoliatorloop->have_posts() ) : $exfoliatorloop->the_post(); ?>
<div class="recommend">
<h2>Your recommended facial exfoliation:</h2>
<?php the_title(); ?><br>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail("thumb"); } ?>
</div>
<?php endwhile; 

} 

This works perfectly.. However, I now need to perform the same wp-query loops to return the same post titles within the Gravity Forms notification email. I understand that shortcodes can be used within the notifications and so I guess this is my best bet.

So the question is "(How) Can I perform the loops (that rely on the "global $age" and "global $skintype" within a shortcode that works inside the Gravity Forms notification!?

nb. I have tested a very simple shortcode in the notification and it works. But if I try to simply define the above function as a shortcode like this:

add_shortcode('myshortcode', 'recommendations'); 

it returns nothing.

share|improve this question
    
That's because your shortcode function adds a filter to the content (=> add_filter('the_content'...), while what you actually want to do is printing some data. –  t f Apr 24 '13 at 11:42
    
Thanks for your reply t f. Could you give me any sort of example to work from? I understand your answer but don't know how to print the output of the loop using the shortcode.. –  Madebymartin Apr 24 '13 at 11:50
    
Just another short question before I provide my answer: this shortcode is used after the global $age and $skintype variables have been set to the right values (i.e., the mail is sent after the form was submitted), right? –  t f Apr 24 '13 at 12:08
    
Good question - I've looked into the Gravity Forms hook that I'm using: "gform_after_submission" and realised that this is executed AFTER the notification! I'm thinking therefore, I need to hook the function to "gform_pre_submission" so that the global variables are set before the notification..? –  Madebymartin Apr 24 '13 at 12:21
add comment

2 Answers

Respecting/Following your last comment, this is how I'd (try to) do it:

add_action('gform_pre_submission_18', 'recommendations');
function recommendations($form) {
    $GLOBALS['skintype'] = $_POST['input_24'];
    $GLOBALS['age'] = $_POST['input_25'];

    add_filter('the_content', 'recommended');
}

add_shortcode('gform_recommendations', 'recommended');
function recommended() {
    global $age;
    global $skintype;

    /* CLEANSER / TONER LOOP */
    $cleansetoneloop = new WP_Query(array( 
        'post_type' => 'recommended',
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'age_group',
                'field' => 'slug',
                'terms' => $age,
            ),
            array(
                'taxonomy' => 'skintype',
                'field' => 'slug',
                'terms' => $skintype,
            ),
            array(
                'taxonomy' => 'recommendation_type',
                'field' => 'slug',
                'terms' => 'cleanser-toner',
            ),
        ),
        'orderby' => 'title',
        'posts_per_page' => '-1', 
        'order' => 'ASC',
    ));

    while ($cleansetoneloop->have_posts()) : $cleansetoneloop->the_post(); ?>
        <div class="recommend cleansertoner">
            <h2>Your recommended cleansing / toning option:</h2>
            <?php the_title(); ?><br />
            <?php if (has_post_thumbnail()) the_post_thumbnail('thumb'); ?>
        </div>
    <?php endwhile;

    /* EXFOLIATOR LOOP */
    $exfoliatorloop = new WP_Query(array(
        'post_type' => 'recommended',
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'age_group',
                'field' => 'slug',
                'terms' => $age,
            ),
            array(
                'taxonomy' => 'skintype',
                'field' => 'slug',
                'terms' => $skintype,
            ),
            array(
                'taxonomy' => 'recommendation_type',
                'field' => 'slug',
                'terms' => 'facial-exfoliator',
            ),
        ),
        'orderby' => 'title',
        'posts_per_page' => '-1', 
        'order' => 'ASC',
    ));

    while ($exfoliatorloop->have_posts()) : $exfoliatorloop->the_post(); ?>
        <div class="recommend">
            <h2>Your recommended facial exfoliation:</h2>
            <?php the_title(); ?><br />
            <?php if (has_post_thumbnail()) the_post_thumbnail('thumb'); ?>
        </div>
    <?php endwhile; 
}

Now you should be able to use [gform_recommendations] inside your e-mail notification.

Note: I did not test this, though.

share|improve this answer
    
I'm getting " Warning: Missing argument 2 for recommendations() in ....functions.php on line 3" and "Notice: Undefined index: post_id in functions.php on line 4".. These are lines: function recommendations($entry, $form) { and $post = get_post($entry['post_id']); –  Madebymartin Apr 24 '13 at 13:54
    
Well, this is your code which I just copied. ;) To be honest, I was asking myself what you were needing line 3 for (the $post = ...) because you don't even use it later. As for the Missing argument 2 warning... This might have to do with the hook that we changed now to pre submission. So have a look at what this hook should be used like. –  t f Apr 24 '13 at 14:08
    
Yeh, going to look into this later when I have more time.. I don't get any warnings or notices with my original functions. My code was a reincarnation of this: gravityhelp.com/documentation/page/Gform_after_submission –  Madebymartin Apr 24 '13 at 14:43
    
Ah.. "gform_pre_submission" is executed BEFORE data is saved by GF! –  Madebymartin Apr 24 '13 at 14:54
    
I just read a bit through the documentation and updated my answer. As far as I understood, the gform_pre_submission hook is the correct one, and it is indeed fired before cerating the entry. However, you can access the $_POST data. In my answer you might have to adjust the 'input_24' and 'input_25' according to your form. –  t f Apr 24 '13 at 15:30
add comment

I'd implement it as a custom merge tag. Use filters gform_custom_merge_tags and gform_replace_merge_tags.

Something like this (untested, probably need to handle the $url_encode, $esc_html and $nl2br flags also):

add_filter('gform_custom_merge_tags', array($this, 'wpse_97053_gformCustomMergeTags'), 10, 4);
add_filter('gform_replace_merge_tags', array($this, 'wpse_97053_gformReplaceMergeTags'), 10, 7);

/**
* add custom merge tags
* @param array $merge_tags
* @param int $form_id
* @param array $fields
* @param int $element_id
* @return array
*/
function wpse_97053_gformCustomMergeTags($merge_tags, $form_id, $fields, $element_id) {
    $merge_tags[] = array('label' => 'Recommended', 'tag' => '{wpse_97053_recommended}');

    return $merge_tags;
}

/**
* replace custom merge tags
* @param string $text
* @param array $form
* @param array $lead
* @param bool $url_encode
* @param bool $esc_html
* @param bool $nl2br
* @param string $format
* @return string
*/
function wpse_97053_gformReplaceMergeTags($text, $form, $lead, $url_encode, $esc_html, $nl2br, $format) {
    if (strpos($text, '{wpse_97053_recommended}') !== false) {
        ob_start();
        recommended();
        $recommended = ob_get_clean();
        $text = str_replace('{wpse_97053_recommended}', $, $text);
    }

    return $text;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.