To explain the joke before we get too far, Jetpack’s contact form was originally called Grunion. The book “Memory” by Lois McMaster Bujold uses the phrase “light fingered fish” to talk about fish who elude hooks.
I was building a site for my father and he wanted the contact form to redirect to another page. Thankfully you can do this with a filter on grunion_contact_form_redirect_url
(see? Grunion? Fish?)
The Code
If you use the official code, then you’re going to need to know two things:
1) What is the page ID you’re redirecting from
2) What is the page slug you’re redirecting to
Yes, it’s weird that you have to know those, but … well. That’s what we’ve got. I tried to come up with a reason why, and I think it’s just that searching for posts by slug is hard.
function EXAMPLE_grunion_custom_form_redirect_url( $redirect, $id, $post_id ){ $redirected_urls = array( '123' => home_url( 'contact' ), '456' => home_url( 'about' ), '789' => 'https://wordpress.org/surprise/', ); foreach ( $redirected_urls as $source => $destination ) { if ( $id == $source ) { return $destination; } } // If there's no custom redirect, return the default return $redirect; } add_filter( 'grunion_contact_form_redirect_url', 'EXAMPLE_grunion_custom_form_redirect_url', 10, 3 );
Buuuuut what if you wanted to do it by slug?
$redirected_urls = array( 'contact' => home_url( 'contact-success' ), 'about' => home_url( 'about-success' ), 'surprise' => 'https://wordpress.org/surprise/', ); $slug = get_post_field( 'post_name', $id ); foreach ( $redirected_urls as $source => $destination ) { if ( $slug == $source ) { return $destination; } }
The benefit to this is you can change the post ID and, as long as it has the same slug, you’re good to go. Also let’s say you have a bunch of separate contact pages (contact-me, contact-mom and so on). You could use the logic to redirect all pages that have the word ‘contact’ or ‘about’ or ‘surprise’ …
foreach ( $redirected_urls as $source => $destination ) { if ( strpos( $source, $slug ) !== false return $destination; } }