Passing result of a different shortcode into param through

By: Phil Turner | Asked: 06/24/2024
ForumsCategory: Code HelpPassing result of a different shortcode into param through [frm-set-get]
Phil Turner asked 4 weeks ago

Hi, in Wordpress, we are running a shortcode that queries through PHP the users email address, strip it to the domain, check that against a Formidable form and return the organisation name. The code for this works well. I then want to use the results from that to additionally query a form using frm-set-get and display-frm-data, but this doesn't work well. If I enter the organisation name manually into the frm-set-get atttribute it works, and I can see that the string returned from the organisation_name shortcode also works fine. I have tried many different things to get this to parse correctly, but can't seem to nail it. Can anyone assist with helping me getting this to work please? I am happy to create another PHP call, but not quite certain how to replicate the frm-set-get function.

[frm-set-get org_name="[organisation_name]"]
[display-frm-data id=4448 param="org_name"]
function get_organisation_name() {
    // Get the form by key
    $form = FrmForm::getOne('member-organisation-details');
    if (!$form) {
        return 'Error: Form not found.';
    }
    // Get the entries in the form
    $entries = FrmEntry::getAll(array('form_id' => $form->id), '', '', true, false);
    if (!$entries) {
        return 'Error: No entries found in form.';
    }
    // Loop through the entries
    foreach ($entries as $entry) {
        // Get the value of the domain_list field
        $domain_list = FrmEntryMeta::get_entry_meta_by_field($entry->id, 'domain_list');
        if (!$domain_list) {
            continue;
        }
        // Get the value of the org_name field
        $org_name = FrmEntryMeta::get_entry_meta_by_field($entry->id, 'org_name');
        if ($org_name) {
            return $org_name;
        }
    }
    // Return an error message if no matching entry was found
    return 'Error: No matching entry found.';
}
function register_my_shortcodes(){  
    add_shortcode('email_domain', 'email_domain_shortcode');
    add_shortcode('organisation_name', 'get_organisation_name');
}
add_action( 'init', 'register_my_shortcodes');

 

2 Answers
Best Answer
Phil Turner answered 4 weeks ago
I ended up resolving this issue by calling the shortcode directly within php, to create a new shortcode. Below is the final product that appears to be working.  
function get_email_domain() {
    // Get current user data
    $current_user = wp_get_current_user();

    // Check if the user is logged in
    if ( ! $current_user->exists() ) {
        return 'Error: User is not logged in.';
    }

    // Get the email address
    $email = $current_user->user_email;

    // Split the email address into a user and domain
    $parts = explode('@', $email);

    // Check if the email address contains '@'
    if ( count($parts) < 2 ) {
        return 'Error: Invalid email address.';
    }

    // Get the domain part
    $domain = $parts[1];

    // Return the domain
    return $domain;
}


function get_organisation_name() {
    // Get the email domain
    $email_domain = get_email_domain();

    // Get the form by key
    $form = FrmForm::getOne('member-organisation-details');
    if (!$form) {
        return 'Error: Form not found.';
    }

    // Get the entries in the form
    $entries = FrmEntry::getAll(array('form_id' => $form->id), '', '', true, false);
    if (!$entries) {
        return 'Error: No entries found in form.';
    }

    // Loop through the entries
    foreach ($entries as $entry) {
        // Get the value of the domain_list field
        $domain_list = FrmEntryMeta::get_entry_meta_by_field($entry->id, 'domain_list');
        if (!$domain_list) {
            continue;
        }

        // Check if the domain_list contains the email domain
        $domains = explode(',', $domain_list);
        if (!in_array($email_domain, $domains)) {
            continue;
        }

        // Get the value of the org_name field
        $org_name = FrmEntryMeta::get_entry_meta_by_field($entry->id, 'org_name');
        if ($org_name) {
            return $org_name;
        }
    }

    // Return an error message if no matching entry was found
    return 'Error: No matching entry found.';
}

function get_training_credits_history(){

    // Define the attributes for the shortcode
    $id = '4448';  // calls the table 
    $param = get_organisation_name();  // adds the organsation name parameter

    // Call the shortcode
    $output = do_shortcode('[display-frm-data id="' . $id . '" org_name="' . $param . '"]');

    // Return the output
    return $output;


}


function register_my_shortcodes(){  
    add_shortcode('email_domain', 'get_email_domain');
    add_shortcode('organisation_name', 'get_organisation_name');
	add_shortcode('return_training_credit_history', 'get_training_credits_history');
}
add_action( 'init', 'register_my_shortcodes');
 
Bobby Clapp replied 1 week ago

Even if it could have been done natively, this gives you insight in extending FF and that may very well be it's best capability. Well done!

Bobby Clapp answered 4 weeks ago
Sounds like error in the code. If you just post into your view [organisation_name], does that return a value? If not you'll have to troubleshoot your code.
Phil Turner replied 4 weeks ago

Yes [organisation_name] returns as expected, as does manually typing the organisation name into frm-set-get param. The problem is when I try and get the two to play together nicely, it doesn't come through.
I have also tried setting it with Javascript, which I can set a variable and use console.log() to check it, which shows that the information is there. It is really strange and after a day of working on it, I can't figure out why it doesn't parse.

Phil Turner replied 4 weeks ago

The result I get (below) seems to indicate that the ] for the [organisation_name] shortcode closes off the [frm-set-get org_name="[organisation_name]"] shortcode, thus missing the point.
"]
No Entries Found

Bobby Clapp replied 4 weeks ago

So the filter is set for "org_name" in a view, correct? Your parameter name in the display shortcode should match that param. Using exactly what you have, would it not be this:

[frm-set-get org_name="[organisation_name]"]
[display-frm-data id=4448 org_name="org_name"]

That or your view needs to have a filter for "param".

Phil Turner replied 4 weeks ago

<p>When I use the actual data,eg.</p><p>[frm-set-get org_name="Accident Compensation Corporation"]</p><p>[display-frm-data id=4448 param="org_name"]</p><p>It returns as expected.</p><p>This is the exact same string (copy and paste) as I can get returned from the [organisation_name] shortcode. So the view and param seem to  be working as expected.</p>

Bobby Clapp replied 4 weeks ago

With [display-frm-data id=4448 param="org_name"], you are saying that you are trying to filter a view looking for a parameter named "param" with a value of "org_name" exactly like it looks.

[display-frm-data id=4448 org_name="[get param=org_name]"] or [display-frm-data id=4448 param="[get param=org_name]"] seems like it would do it. Once you set a value, you still have to recall it with get param.

Phil Turner replied 4 weeks ago

Hi, I know that the [display-frm-data id=4448 param="org_name"] is coded in a way that works as if I do [frm-set-get org_name="Accident Compensation Corporation"] I get the expected results. The problem is when I use the extra shortcode in the frm-set-get.

Bobby Clapp replied 4 weeks ago

Yeah. Ok. What does this do?

[display-frm-data id=4448 param="[get param=org_name]"]

According to this, you'd have to call the set param this way: https://formidableforms.com/knowledgebase/set-values-to-be-used-in-custom-displays-or-default-values/#kb-in-pages-or-posts

Phil Turner replied 4 weeks ago

I get the following
"]
No Entries Found
"]

from
[frm-set-get org_name="[organisation_name]"]
[display-frm-data id=4448 param="[get param="org_name"]"]

Bobby Clapp replied 4 weeks ago

It's the doubling of quotes. It should be this: [display-frm-data id=4448 param='[get param="org_name"]'].

Do you get a value if you just post [get param="org_name"] someplace? Is it an accurate value for what you are expecting or just part of an array of values?

Phil Turner replied 4 weeks ago

Thanks for all your help looking into this.

[get param="org_name"] does not work outside of the display-frm-data shortcode, but if I set the string manually in frm-set-get it parses through as expected in display-frm-data.

Phil Turner replied 4 weeks ago

And i have played around with all iterations of single and double quotes, to no effect.

Bobby Clapp replied 4 weeks ago

If [get param="org_name"] does not produce a result, then the frm-set-get part isn't happening and that's why it is failing.

Phil Turner replied 4 weeks ago

That is what I originally thought, but if I manually set the organisation name in [frm-set-get org_name="****"] instead of using the shortcode to call the name, I don't get a response from [get param="org_name"] but the [display-frm-data id=4448 param="org_name"] works fine. I am starting to believe that the problem is more to do with the timing of the return from the [organisation_name] shortcode, which means that it is not passed in to the code in time.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right