Support » Plugin: Jetpack by WordPress.com » Jetpack redirect after contact module submition

  • Resolved lideachetimanca

    (@lideachetimanca)


    I have see this code on Jetpack support page, but I’m a dog whit code… 😀

    Everyone can explain me WHERE I put this code and WHAT change?

    Thanks a lot!

    /**
     * Jetpack Contact Form Custom Redirections.
     *
     * @param  string $redirect Post submission URL.
     * @param  int    $id       Contact Form ID.
     * @param  int    $post_id  Post ID.
     *
     * @return string $redirect Custom Post submission URL.
     */
    function jetpackcom_custom_form_redirect( $redirect, $id, $post_id ) {
        /**
         * Create a list of pages where you've inserted forms.
         * For each contact Form ID (found via the id attribute on the form),
         * set up a custom URL where the user will be redirected.
         */
        $redirects = array(
            '1370' => home_url( 'page_on_your_site' ),
            '2239' => home_url( 'another_page' ),
            '1370' => home_url( 'page_on_your_site' ),
        );
      
        // Let's loop though each custom redirect.
        foreach ( $redirects as $origin => $destination ) {
            if ( $id == $origin ) {
                return $destination;
            }
        }
      
        // Default Redirect for all the other forms.
        return $redirect;
    }
    add_filter( 'grunion_contact_form_redirect_url', 'jetpackcom_custom_form_redirect', 10, 3 );
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support KokkieH

    (@kokkieh)

    Hi there,

    You’re referring to the code snipped that appears at https://jetpack.com/support/contact-form/#customization

    To use that code, first you’ll need to install the Code Snippets plugin on your site. You’ll add this code in that plugin.

    But you’ll need to make some changes to the code. Specifically this section:

    $redirects = array(
            '1370' => home_url( 'page_on_your_site' ),
            '2239' => home_url( 'another_page' ),
            '1370' => home_url( 'page_on_your_site' ),
        );

    Replace the numbers with the contact form IDs of the different forms you have. If you only have one page with a contact form, you only need one line there and can delete the rest, so in that case you only need:

    $redirects = array(
            '1370' => home_url( 'page_on_your_site' ),
        );

    To get the form ID, you’ll have to use the browser inspector to find the id-attribute for the specific form. Right-click on the form and select Inspect. That will open the browser inspector showing your site’s code, with the contact form’s code highlighted. You might need to go up a few lines in the code, but look for the line that contains a div attribute followed by id="contact-form-###"

    The number in that ID is the one you want.

    Inside the quotes in parentheses, replace the page_on_your_site with the URL of the actual page where you want someone to be redirected for that specific contact form. Save that code in the Code Snippets plugin, and it should redirect someone who submits your contact form.

    Ok thanks, I have a child theme…

    without install CODE SNIPPET PLUGIN I can insert this code on function.php?

    Yes into the function.php it work! Many thanks @kokkieh

    Plugin Support KokkieH

    (@kokkieh)

    You can add it directly to functions.php in your child theme, yes.

    Glad I could help 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.