0

In Gravity Forms, I have a confirmation set up with various conditional shortcodes. Example:

[gravityforms action="conditional" merge_tag="{:3:value}" condition="contains" value="E"]*** GET A CATEGORY ***[/gravityforms]

Additionally, I have a PHP function that gets all posts by category, and I would like to use this function inside the conditional shortcode. I'm not figuring out a way to do this, so any help would be appreciated.

My function: useful_tools_list(array( 'type' => 'documents', desc => 'true' ))

I also made it into a shortcode: [useful-tools type="documents" desc="true"]

I tried using <?php ?>, but I realized I can't embed PHP into the editor. The conditional shortcode doesn't support nesting other shortcodes inside of it, so that doesn't work.

I'm wondering if there is a way to manipulate the GF conditional shortcode to allow nesting? Or another way of calling the function that I'm not aware of?

2 Answers 2

0

Instead of use shortcode in your confirmation, you can use the filter hook "gform_confirmation" for manage your confirmation. After, you'll be able to use your shortcode [useful-tools] with do_shortcode() with your confirmation.

Sorry if I misunderstood your problem!

1
  • That is a possibility. Thank you for this. I would rather not have to create an entire confirmation out of PHP only because I have coworkers that aren't familiar with PHP and would like to make changes to the confirmation from the GF editor if possible. I suppose if all else fails, this will be the solution.
    – Michael
    Aug 14, 2020 at 18:33
0

I came up with a solution that will work without having to build out an entire confirmation via PHP. This won't work for everyone's situation, but since I did build out my own function/shortcode, it works for me.

I added the conditional logic to my shortcode and removed the GF shortcode altogether. To do so, I added merge_tag and value parameters like so:

[useful-tools type="documents" desc="true" merge_tag="{:3:value}" value="B"]

For those that want to see my shortcode, I'm including it here. It is set up to list all custom post types matching a custom taxonomy. It can also be used to list them outside of GF by removing the parameters.

function useful_tools_list($atts){
    $atts = shortcode_atts(
        array(
            'type'  => '',
            'desc'  => '',
            'ul'    => '',
            'li'    => '',
            'merge_tag' => '',
            'value' => '',
         ), 
        $atts
    );
    
    $results = '';
    if (($atts['merge_tag']) && (!preg_match('/\b'.$atts['value'].'\b/', $atts['merge_tag']))) {
        $results .= '';
    } else {
        $all_terms = get_terms('types');

        foreach($all_terms as $term) {
            wp_reset_query();
            if (($atts['type']) && ($atts['type'] != "")) {
                $args = array('post_type' => 'useful-tools',
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'types',
                            'field' => 'slug',
                            'terms' => $term->slug,
                        ),
                    ),
                    'types' => $atts['type'],
                 );
            } else {
                $args = array('post_type' => 'useful-tools',
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'types',
                            'field' => 'slug',
                            'terms' => $term->slug,
                        ),
                    ),
                 );
            }

            $loop = new WP_Query($args);
            if($loop->have_posts()) {
                if ((!$atts['type']) || ($atts['type'] == "")) {
                    $results .= '<h2>'.$term->name.'</h2>';
                }
                if (($atts['ul']) && ($atts['ul'] == "true")) {
                    $results .= '<ul class="tools-type-ul">';
                }
                while($loop->have_posts()) : $loop->the_post();
                $postID = get_the_ID();
                $desc = '';
                $actualDesc = get_post_meta($postID, '_post_desc', true);
                if ($atts['desc'] == 'true' && $actualDesc != '') {
                    $desc = ' - '.get_post_meta($postID, '_post_desc', true);
                }
                if (($atts['li']) && ($atts['li'] == "true")) {
                    $results .= '<li><a href="'.get_post_meta($postID, '_post_url', true).'">'.get_the_title().'</a>'.$desc.'</li>';
                } else {
                    $results .= '<a href="'.get_post_meta($postID, '_post_url', true).'">'.get_the_title().'</a>'.$desc.'<br>';
                }
                endwhile;
                if (($atts['ul']) && ($atts['ul'] == "true")) {
                    $results .= '</ul>';
                }
            }
        }
    }
    return $results;
}
add_shortcode('useful-tools', 'useful_tools_list');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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