• When submitting a contact form I get an error in the console
    when I deactivate the plugin it starts working again I tried uninstalling and reinstalling the actual plugin functionality is working fine I am able to login using the API has anyone else had the same problem?
    I have left the Plugin activated if you need more info on the error just submit the form at the bottom of the home page https://www.wpscreators.com

    Uncaught TypeError: Cannot read property ‘replace’ of undefined

    The page I need help with: [log in to see the link]

Viewing 11 replies - 1 through 11 (of 11 total)
  • Same error when submitting a contact form i get an error in the console.
    Uncaught TypeError: Cannot read property ‘replace’ of undefined https://prnt.sc/xf7icp
    If I deactivate the jwt auth plugin and working perfect.

    I really appreciate If you have any solution for this error please let me know

    Try to use this code to whitelist the contact form 7 api request.
    To whitelist any API json endpoints for JWT Auth just use the following filter:

    add_filter('jwt_auth_default_whitelist', 'custom_jwt_auth_default_whitelist', 10, 1);
    function custom_jwt_auth_default_whitelist($whitelist) {
      $rest_api_slug = home_url('/wp-json', 'relative');
      $whitelist[] = $rest_api_slug . '/contact-form-7/';
      return $whitelist;
    }
    Thread Starter rahuljain1994

    (@rahuljain1994)

    @haidarhammoud I tried the solution dint work for me ??

    I am also facing the same issue. I also tried the solution @haidarhammoud suggested and added the code snippet to JWT plugin file named as class-setup.php

    Please correct me if I did anything wrong and the solution worked for you. And if not then please suggest some solution.

    Hi There, having the same issue on this site on this form https://anytimepro.io/#register

    Tried the snippet provided by @haidarhammoud but sadly that’s not solved it.
    Disabling this plugin resolves the issue. Any word on a fix

    Kind Regards,
    David Tattersall

    Just modify the main plugin file
    jwt-auth.php

    replace
    new JWTAuth\Setup();

    to
    add_action( ‘plugins_loaded’, function() { new JWTAuth\Setup(); } );

    and
    Add Contacts Form 7 for the white list

    This solves the problem for me

    • This reply was modified 3 years, 7 months ago by design.1.

    I tried both solutions but it is not working for me can you let us know.

    @ggauravbhandari same I’ve now tried several solutions including @design1-1 and @haidarhammoud ‘s suggestions. I am still met with the same issue.

    Currently sat at this stage with this in the top of functions.php in the theme:

    add_filter(
    	'jwt_auth_whitelist',
    	function ( $endpoints ) {
    		$rest_api_slug = home_url('/wp-json', 'relative');
    		$whitelists = array(
    			$rest_api_slug . '/contact-form-7/',
    			$rest_api_slug . '/jwt-auth/v1/token',
    			$rest_api_slug . '/jwt-auth/v1/token/validate',
    			$rest_api_slug . '/jwt-auth/v2/token',
    			$rest_api_slug . '/jwt-auth/v2/token/validate'
    		);
    		foreach ( $whitelists as $whitelist ) {
    			if ( ! in_array( $whitelist, $endpoints, true ) ) {
    				array_push( $endpoints, $whitelist );
    			}
    		}
    		return $endpoints;
    	}
    );

    and this in the jwt-auth.php in the plugin:

    add_action( 'plugins_loaded', function() { new JWTAuth\Setup(); } );

    Any further suggestions would be heavily appreciated

    Kind Regards,
    David Tattersall

    @daviddorsettech You’re not going to be able to whitelist anything from the theme. Your call to plugins_loaded hook happens after the hook has fired. From the plugin docs –

    “Every call to the server (except the token creation some default whitelist) will be intercepted. However, you might need to whitelist some endpoints. You can use jwt_auth_whitelist filter to do it. Please simply add this filter directly (without hook). Or, you can add it to plugins_loaded. Adding this filter inside init (or later) will not work.

    If you’re adding the filter inside theme and the whitelisting doesn’t work, please create a small 1 file plugin and add your filter there.

    Here is the code that I got to work by calling inside plugin without hook, note the different filter (it’s the only one that worked for me):

    \add_filter('jwt_auth_default_whitelist', function ($whitelist){
                
                $base = home_url( '/wp-json/', 'relative');
                $new_whitelist = array(
                    $base . 'contact-form-7',
                );
                return array_unique( array_merge( $whitelist, $new_whitelist ) );
    
            });
        }

    @benjamintoth the plugins_loaded hook was actually being called in the JWT plugin as suggested above but didn’t make a difference anyway!

    That has actually solved the issue though! Thank you!

    To any Googlers who have made it this far with this issue. The above solution works I’ve got it sat in its own one file plugin as suggested works like a treat.

    Thanks again Ben!

    @daviddorsettech @benjamintoth I tried both solutions but not working for me can you find any other solution

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Contact form 7 not working when JWT is active’ is closed to new replies.