Gravity Forms: How to add PHP function to confirmation conditional shortcode?

admin2025-01-08  4

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?

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?

Share Improve this question asked Aug 14, 2020 at 17:31 MichaelMichael 2811 silver badge14 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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!

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');
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736265909a1079.html

最新回复(0)