• Hi,

    First of all thanks for this great plugin! It works like a charm.

    The problem I’m having is the following. I want to set up a goal on google analytics when a users submits my contact form. Now, to do so I need a different url on the resulting page (so, then a http POST is sent). Unfortunately there is no way (AFAIK) to check the http method.

    So, I was thinking to add a new parameter. If set, it will just append that parameter to the post url. Something like this:

    
    diff --git a/vscf-form.php b/vscf-form.php
    index b2537ca..0345fbf 100644
    --- a/vscf-form.php
    +++ b/vscf-form.php
    @@ -26,7 +26,8 @@ function vscf_shortcode($vscf_atts) {
                    "error_email" => __('Please enter a valid email', 'very-simple-contact-form'),
                    "message_error" => __('Please fill the required fields', 'very-simple-contact-form'),
                    "message_success" => __('Thank you! You will receive a response as soon as possible.', 'very-simple-contact-form'),
    -               "hide_subject" => ''
    +               "hide_subject" => '',
    +               "submit_query_param" => ''
            ), $vscf_atts);
    
            // Set variables
    @@ -148,8 +149,14 @@ function vscf_shortcode($vscf_atts) {
                    $hide = true;
            }
    
    +    $url = $_SERVER['REQUEST_URI'];
    +    $submit_query_param = @$vscf_atts['submit_query_param'];
    +    if ($submit_query_param) {
    +        $url .= (strpos($url, '?') > 0 ? '&' : '?') . $submit_query_param;
    +    }
    +
            // Contact form
    -       $email_form = '<form class="vscf" id="vscf" method="post">
    +       $email_form = '<form action="' . $url . '" class="vscf" id="vscf" method="post">
                    <p><label for="vscf_name">'.esc_attr($vscf_atts['label_name']).': <span class="'.(isset($error_class['form_name']) ? "error" : "hide").'" >'.esc_attr($vscf_atts['error_name']).'</span></label></p>
                    <p><input type="text" name="vscf_name" id="vscf_name" '.(isset($error_class['form_name']) ? ' class="error"' : '').' maxlength="50" value="'.esc_attr($form_data['form_name']).'" /></p>
    
    @@ -182,4 +189,4 @@ function vscf_shortcode($vscf_atts) {
     }
     add_shortcode('contact', 'vscf_shortcode');
    
    -?>
    \ No newline at end of file
    +?>
    

    This is just an idea. It still only for one form (there is also the widget form). And it has a problem, it will trigger the goal even if the form validation fails.

    Maybe a better idea would be to setup a specific page, and have a redirect_on_successfull_submit. Then the plugin will redirect there.

    What do you think?

Viewing 15 replies - 1 through 15 (of 23 total)
  • Plugin Author Guido

    (@guido07111975)

    Hi,

    What I can try(!) to do is to add a page ID attribute, so when user adds this attribute (+ page ID) it will redirect to this page after submitting the form.. Is that what you want?

    Guido

    Thread Starter puzz

    (@puzz)

    Yes, that would be perfect!

    Now, I’m not much of an wordpress developer (more into Java/Golang), but I think the normal location() redirect can’t be done from a plugin when part of the page is already sent. At least I think it is. But maybe even a <javascript>document.location=...</javascript> will be enough?

    Plugin Author Guido

    (@guido07111975)

    Meanwhile you can create your own (hardcoded) redirect very easily.

    Open files vscf-form and vscf-widget-form, look for and remove this:

    
    return $info;
    

    Add this instead:

    
    header('Location: http://www.example.com/');
    

    Done!

    Guido

    Plugin Author Guido

    (@guido07111975)

    Hi again,

    Please ignore previous reply because it doesn’t work properly. You will get the famous “headers already sent” message. Detailed explanation here.

    Will work on it.

    Guido

    Thread Starter puzz

    (@puzz)

    Hey, yes, that’s exactly what I was thinking.

    Anyway, if you need any help just let me know.

    Plugin Author Guido

    (@guido07111975)

    I don’t have much knowledge about β€œheaders already sent” but I now know it’s never gonna work on this location in my file. Besides this there’s a session holding captcha data and I did not fully destroy it before calling the header. Guess this is bad for the header as well. Any help appreciated but I strongly prefer a solution with php.

    Guido

    Plugin Author Guido

    (@guido07111975)

    Hi again,

    I think using the js window.location function is the most easy solution.. I need to add the PHP variable, but js is not my thing.

    My php header, which I’m not gonna use:

    
    header('Location: '.home_url('/?page_id='.$vscf_atts['page_id'].'').'');
    

    Maybe you can help me with that part?

    Guido

    Thread Starter puzz

    (@puzz)

    I’m not on my dev machine now, but I’ll try to experiment with it tomorrow.

    Plugin Author Guido

    (@guido07111975)

    Hi,

    Normally I could use a function called wp_localize_script to add a php variable (in my case page id) to the js window.location. But, because wp_localize_script is running outside the shortcode file I’m not able to get the variable. See for an example this topic.

    Will work on it..

    Guido

    Plugin Author Guido

    (@guido07111975)

    Hi,

    Found a solution: echo the javascript:

    
    $page = home_url('/?page_id='.$vscf_atts['page_id'].''); 
    echo '<script type="text/javascript">'; 
    echo 'window.location="'.$page.'";'; 
    echo '</script>';
    

    Have added 2 extra shortcode attributes to set a redirect:
    redirect = not set or set on “true”
    page_id = page ID to redirect to

    It doesn’t redirect very quickly on my local install, when clicking submit. But did not test it on live server yet.

    What are your thoughts about this solution?

    Guido

    Thread Starter puzz

    (@puzz)

    Yeah, that should work. The redirect is slow because it needs to load the page, with header redirect it would be almost instant, I suppose. But, I don’t mind if it’s slow, the only important thing for me is to have this one specified url requested (sooner or later).

    BTW, the cleanest way to do the redirect properly is probably to register a hook like:

    add_action('init', 'your_function');

    This is done before WP starts the output, and you are still in time to send a header there. But I think that would need a bigger refactoring of the code. In the meantime, the javascript redirect is definitely enough for me.

    PS. Just sent a “Thank you” donation. Not huge, but I think it should be enough for a pizza and a beer (maybe even two) in Netherlands πŸ™‚

    Plugin Author Guido

    (@guido07111975)

    Hi @puzz

    First, thank you very much for your generous donation πŸ™‚ I really appreciate it.

    Because the contact form is wrapped in a shortcode it’s kind of difficult to get a value outside the shortcode. The redirect action hook should be in the functions file of my plugin (vscf.php), so that means I have to get the page ID value from the shortcode file.. So I’m having the same problem as using wp_localize_script (see a previous reply). I guess there must be a solution somehow, but I’m not a hardcore programmer.

    Guido

    Plugin Author Guido

    (@guido07111975)

    Hi again,

    You know, if I’m NOT able to find a proper solution within several days I will refund 50% of your donation.

    The redirect with inline js works, but isn’t the proper way.

    Guido

    Thread Starter puzz

    (@puzz)

    I caught a bad flu last week and haven’t had the time to experiment with it. If nothing works, I’ll just have to live with it.

    And, don’t worry about my donation. That was for your efforts (and nobody can deny that you work hard, I appreciate that!). If you ever come to Croatia, ping me I owe you a beer for your efforts!

    • This reply was modified 7 years, 3 months ago by puzz.
    Thread Starter puzz

    (@puzz)

    Hey, here I am again, I think this should work:

    add_action('init', 'do_output_buffer');
    function do_output_buffer() {
        ob_start();
    }

    …it makes sure no content is sent before you. The content is kept in memory and WordPress makes sure to flush all the ob_ cache later.

    And now wp_redirect() (https://developer.wordpress.org/reference/functions/wp_redirect/) can be used from any place in your plugin.

    I’m still reading if that is OK to do from a wp plugin, but it works.

    • This reply was modified 7 years, 3 months ago by puzz.
    • This reply was modified 7 years, 3 months ago by puzz.
Viewing 15 replies - 1 through 15 (of 23 total)
  • The topic ‘Submit query param’ is closed to new replies.