Changing form action path

So I have to configure this Gravity forms thing and it has the wrong base path for the form action. It does not seem to use the wordpress site base URL but instead chooses to a set a completely different path.

A search for this seems to indicate that a javascript function is needed to simply change the form action path (Setting form ACTION to a specific URL - Get Help - Gravity Forms) .. awkward, but .. ok .. However your settings seems so locked down it is not obvious how to even get that to work.

So, is there not any SIMPLE way to alter the form action? One of the most basic things you might want to do for a form?

Out of the box, Gravity Forms does not provide a setting for the action URL because changing it can prevent Gravity Forms processing the submission, so it isn’t validated or saved.

The value used in the action attribute of the form tag is provided by WordPress, which gets it from the PHP $_SERVER['REQUEST_URI'] variable.

You can use the gform_form_tag filter in your theme functions.php file, a custom plugin, or a code snippets plugin to change it.

There’s also the following from the team at Gravity Wiz:

I have a function to alter the form action path modelled after what you linked as the gform_form_tag filter, but it doesn’t work. The regex when tested should be altering things as intended. All I want to do is prepend the string ‘/blog’ to the form action tag for any forms created. Here’s what I have so far:

add_filter( 'gform_form_tag', 'form_tag', 10, 2 );
function form_tag( $form_tag, $form ) {

	$pattern = '/\baction\s*=\s*([\'"])(.*?)\1/i';
	$replacement = 'action=$1/blog$2$1';
	$form_tag = preg_replace($pattern, $replacement, $form_tag);

    return $form_tag;
}

Can you help?

Hi Derek,

Could you please try the following code and let me know if it helps?

add_filter( 'gform_form_tag', 'gf_prefix_blog_in_action', 10, 2 );
function gf_prefix_blog_in_action( $form_tag, $form ) {
	return preg_replace_callback(
		"/action='([^']+)'/",
		function( $matches ) {
			$current = $matches[1];
			$parts   = wp_parse_url( $current );

			$path     = isset( $parts['path'] ) ? $parts['path'] : '';
			$query    = isset( $parts['query'] ) ? '?' . $parts['query'] : '';
			$fragment = isset( $parts['fragment'] ) ? '#' . $parts['fragment'] : '';

			if ( strpos( $path, '/blog' ) === 0 ) {
				return $matches[0];
			}

			$prefixed_path = '/blog' . $path;
			$new_action    = home_url( $prefixed_path ) . $query . $fragment;

			return "action='{$new_action}'";
		},
		$form_tag,
		1
	);
}

Please change the filter name to gform_form_tag_123 (replace 123 with the form ID). After adding the snippet, clear the cache in your caching plugin and your browser, then reload the page to confirm the action now reads https://example.com/blog/… If submissions fail, remove the snippet immediately; changing the action can bypass Gravity Forms’ normal processing.