Skip to content

Instantly share code, notes, and snippets.

@ihslimn
Created November 8, 2022 11:41
Show Gist options
  • Save ihslimn/c59b2ed24d419e57265e15ed5d2312be to your computer and use it in GitHub Desktop.
Save ihslimn/c59b2ed24d419e57265e15ed5d2312be to your computer and use it in GitHub Desktop.
JetMenu Fix JetFormBuilder form in menu
<?php
class JFB_Fix_Action_URL {
public function __construct() {
add_filter( 'jet-form-builder/form-action-url', array( $this, 'store_action_url' ), -999 );
add_filter( 'jet-form-builder/form-refer-url', array( $this, 'store_referer_url' ), -999 );
add_action( 'wp_head', array( $this, 'print_inline_script' ) );
}
public function print_inline_script() {
?>
<script>
jQuery( function( $ ) {
$( window ).on( 'jet-menu/ajax/frontend-init/after', function( event, params ) {
let menu = params.$container[0];
let url = document.cookie.split( '; ' ).find((row) => row.trim().startsWith('jfb_correct_action_url='))?.split('=')[1];
$( menu ).find( 'form' ).attr( 'action', decodeURIComponent( url ) );
url = document.cookie.split( '; ' ).find((row) => row.trim().startsWith('jfb_correct_referer_url='))?.split('=')[1];
$( menu ).find( 'form input[name="_jet_engine_refer"]' ).attr( 'value', decodeURIComponent( url ) );
} );
} );
</script>
<?php
}
public function is_correct_url( $url ) {
return false === strpos( $url, '/jet-menu-api/' );
}
public function store_action_url( $url ) {
if ( $this->is_correct_url( $url ) ) {
setcookie(
'jfb_correct_action_url',
$url,
0
);
}
return $url;
}
public function store_referer_url( $url ) {
if ( $this->is_correct_url( $url ) ) {
setcookie(
'jfb_correct_referer_url',
$url,
0
);
}
return $url;
}
}
new JFB_Fix_Action_URL();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment