0

I have a shortcode. With this one, i get the ID (number) of a formular $num_formular. Then, with this one, i had to send all the data in the database.

When I run the following code, it works:

function ss_get_appbenutzer($atts, $content = null){
//global $num_formular;
/* Récupération du numéro du formulaire*/   
$atts = shortcode_atts(
    array(
        'num_formular' => 'NUM_formular'
    ), $atts);
$num_formular = $atts['num_formular']; 
echo 'NUM Formulaire DANS SHORTCODE ='.$num_formular;
}
add_shortcode( 'app-benutzer' , 'ss_get_appbenutzer' );
add_action('gform_after_submission_192', 'msk_gform_process_user_registration_192', 10, 2);

function msk_gform_process_user_registration_192 ($entry, $form) {
echo 'PASSAGE DANS FONCTION msk_gform_process_user_registration_MB --> ok ';
}};

and it prints :

NUM Formulaire DANS SHORTCODE =192 PASSAGE DANS FNCTION msk_gform_process_user_registration_MB --> ok

So it works and the data are in the database.

BUT, i get the number of the formular because it will be various number of formular.

So, i need to have $num_formular as a variable. Then I must use call add_action like this :

function ss_get_appbenutzer($atts, $content = null){
//global $num_formular;
/* Récupération du numéro du formulaire*/   
$atts = shortcode_atts(
    array(
        'num_formular' => 'NUM_formular'
    ), $atts);
$num_formular = $atts['num_formular']; 
echo 'NUM Formulaire DANS SHORTCODE ='.$num_formular;
}
add_shortcode( 'app-benutzer' , 'ss_get_appbenutzer' );

$gform = "'gform_after_submission_".$num_formular."'";
add_action($gform, 'msk_gform_process_user_registration_MB', 10, 2);

function msk_gform_process_user_registration_MB ($entry, $form) {
echo 'PASSAGE DANS FONCTION msk_gform_process_user_registration_MB --> ok ';
}};

But the $num_formular is unknown outside the funtion ss_get_appbenutzer. I tried to use global, but i didn't find something working. And i realized that global is not really a good solution.

Then, i tried to include add_action inside the ss_get_appbenutzer function, like this (i took the code which was working)

function ss_get_appbenutzer($atts, $content = null){
//global $num_formular;
/* Récupération du numéro du formulaire*/   
$atts = shortcode_atts(
    array(
        'num_formular' => 'NUM_formular'
    ), $atts);
$num_formular = $atts['num_formular']; 
echo 'NUM Formulaire DANS SHORTCODE ='.$num_formular;
add_action('gform_after_submission_192', 'msk_gform_process_user_registration_192', 10, 2);
}

add_shortcode( 'app-benutzer' , 'ss_get_appbenutzer' );

function msk_gform_process_user_registration_192 ($entry, $form) {
echo 'PASSAGE DANS FONCTION msk_gform_process_user_registration_MB --> ok ';
}};

but it prints only NUM Formulaire DANS SHORTCODE =192

That means that the add_action, placed here in the ss_get_appbenutzer function, doesn't work (or is not called).

COuld you please give me some hint to make this add_action working?

I am really really stuckked and i tried so many things that i don't know where to search now.

Many thanks for your help

Timama

  • The reason it's not working is because gform_after_submission_192 has presumably already run by the time the shortcode is printed. Honestly, what you're trying to do doesn't make a lot of sense. Why are you using a shortcode for this> It looks like you want to do something after a Gravity Form is submitted, but why would you use a shortcode to do that? Can you explain in more detail what you're actually trying to accomplish. – Jacob Peattie Feb 20 at 10:16
  • many thanks for your comment. Okay, i didn't give a good explanation. The simple example (for only one form) : I have a page (built with wordpress). In this page, there is a registration form. I use shortcode in wordpress, for this page, to have the registration form in this page. So I need the shortcode (it is the way to put the form in a page for gravity form). – Timama Feb 20 at 10:54
  • Then, i used the shortcode to get the number of this registration form. With this one, I can use add_action with the good numer of registration form and save the datas in the databank. The thing is that I have various number of registration form. That's why i am trying to have this "automatic" and not create code each time I have a new form. – Timama Feb 20 at 10:58
  • Gravity Forms has a hook, gform_after_submission, that runs for every form. Can't you just use that? And what is the app-benutzer shortcode for? You've described what the Gravity Forms shortcode is for, but not this one. I don't know why you need this shortcode at all, when you can just use add_action() in your functions file outside of a shortcode. – Jacob Peattie Feb 20 at 11:03
  • thanks for this comment. app-benutzer is the tag (the name) of the shortcode which uses the function ss_get_appbenutzer. So, the shortcode app-benutzer is the one I explained (there is only one shortcode). I use gform_after_submission (as you can see in my code below). I want it to run for specific form, not for all. can I just use add_action('gform_after_submission', 'msk_gform_process_user_registration', 10, 2); ? how does it connect the add_action with the form ? – Timama Feb 20 at 11:14
0

After some discussing in the comments, I can surmise that this is your situation:

  1. You have functionality for processing a user registration in some way when a form is submitted.
  2. You need a way in the CMS to specify which forms this processing needs to happen for.

What it looks like you're attempting to do is add a shortcode to the page with a matching form ID so that when the form is submitted the processing occurs for that form.

There are several problems with this:

  1. Shortcodes are intended for displaying dynamic contnet inside a post or page. They are not intended for back-end events like form processing.
  2. When a Gravity Form is submitted, it is processed before the page content is reloaded. This means that it's too late to attempt to hook into the form submission hook, because it has already run and finished.

The 'proper' way to develop this would be to develop a "feed", which is how Gravity Forms supports developers providing a way to add their functionality to specific forms via the UI. The documentation for creating custom feed types is available here. In your case you would put your registration processing into the process_feed method of your custom feed.

However, as you've suggested, it is quite a lot of work to create an add-on feed. So, I'm going to suggest a workaround:

  1. Add a hidden field to the forms that you wish to process.
  2. Hook into gform_after_submission for all forms and check for this hidden field. If the hidden field is present, process the registration.

So, for the first step, add a Hidden field to the forms that you need to be processed. Give it a label that makes sense for you. I'll assume "App Benutzer", just based on the shortcode name.

Now, this is what your code will be:

function msk_gform_process_user_registration_MB( $entry, $form ) {
    $fields = $form['fields'];

    // Check every field in the form.
    foreach ( $fields as $field ) {
        // If our hidden field has been added to this form, process the user registration.
        if ( $field->label = 'App Benutzer' ) {
            // Process user registration here.
        }
    }
}
add_action( 'gform_after_submission', 'msk_gform_process_user_registration_MB', 10, 2 );

So this solves the problem of allowing non-technical users a way to tell Gravity Forms which forms the user registration should be processed for: They just need to add a Hidden field with the label "App Benutzer".

  • oh that is a good idea ! the hidden field ! Many thanks Jacob ! I m tring this right now and tell if it works... i'l be back – Timama Feb 20 at 13:05
  • Keep in mind that // Process user registration here. won't be occurring in the page content, so any messages you try to print might not appear. I suggesting logging messages to the error log with error_log( 'message here' ) if you want to see if it's working. – Jacob Peattie Feb 20 at 13:11
  • Jacob, it works ! So many thanks ! I really didn't think about this hidden filed.. and it is so simple ! Don't you think it can slow down the website speed if there are too many forms ? as here, every forms are tested .... – Timama Feb 20 at 13:23
  • I can't imagine any performance problems. Whenever a form is submitted the code will need to check every field looking for the hidden field, but this should be pretty quick, since there's no database calls involved. If you have forms with lots of fields, then it might take slightly longer for them to submit, but the rest of the site would be unaffected. – Jacob Peattie Feb 20 at 13:28
  • 1
    thanks for this answer. So, I put the hidden field on the top. How can I thank you ! you really helped me ! Thank you for insiting at the begining, that made me look somewhere else and Then your idea to take the problem in another way is perfect ! SO many thanks ! – Timama Feb 20 at 13:32

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.