• Resolved Chigolo

    (@fitnsexy)


    Hello,
    I’m having an issue with WPFORMS related to minifying Java files.
    I’d like to prevent the Enter key from submitting my forms, but unfortunately, the snippet code isn’t being executed due to minification. Is there a way to exclude this?

    /** * Disable the Enter key WPForms forms * * @link https://wpforms.com/developers/how-to-stop-the-enter-key-from-submitting-the-form/ */ function wpf_dev_disable_enter_all_wpforms( ) { ?> <script type="text/javascript"> // Run this snippet on all WPForms forms jQuery( 'form.wpforms-form' ).bind( 'keypress keydown keyup', function(e) { if (e.keyCode == 13) { e.preventDefault(); } }); </script> <?php } add_action( 'wpforms_wp_footer_end', 'wpf_dev_disable_enter_all_wpforms', 30 );
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Damilare

    (@deabiodun)

    Hi @fitnsexy!
    The best way to exclude the code script is by enqueuing it as an external script using wp_enqueue_script. For example, you can have your code in wp-content/uploads/my_files/my_script.js. Then, using wp_enqueue_script() as described below, your custom external script will be applied to the footer section of your site

    function enqueue_custom_jquery_script() {
    $script_url = content_url('uploads/my_files/my_script.js');
    wp_enqueue_script('my-custom-script', $script_url, array('jquery'), null, true );
    }
    add_action('wp_enqueue_scripts', 'enqueue_custom_jquery_script');

    With that, you can exclude your JavaScript file by adding wp-content/uploads/my_files/my_script.js to the Exclude JavaScript from processing field at WP-Optimize > Minify > JavaScript.

    With that, your forms should work as expected.

    Let us know if that works for you.

    Regards.

    Thread Starter Chigolo

    (@fitnsexy)

    Thx @deabiodun

    Where do I have to enter the code above?

    function enqueue_custom_jquery_script() {
    $script_url = content_url('uploads/my_files/my_script.js');
    wp_enqueue_script('my-custom-script', $script_url, array('jquery'), null, true );
    }
    add_action('wp_enqueue_scripts', 'enqueue_custom_jquery_script');
    Plugin Support Damilare

    (@deabiodun)

    You can use a code snippet plugin or paste it in your themes functions.php file.

    Note that you could loose the code with the theme approach, when the theme gets updated. So consider using a child theme.

    Regards.

    Thread Starter Chigolo

    (@fitnsexy)

    Allright, thx for your support @deabiodun

    Thread Starter Chigolo

    (@fitnsexy)

    If anyone is interested, I created a JS file with the following code:

    /**
    * Disable the Enter key WPForms forms
    *
    * @link https://wpforms.com/developers/how-to-stop-the-enter-key-from-submitting-the-form/
    */

    jQuery(function($) {
    $('form.wpforms-form').on('keydown', function(e) {
    if (e.key === 'Enter') {
    e.preventDefault();
    }
    });
    });

    and placed it in a “custom-js” folder in my theme, then created a snippet like this:

    /** Disable the Enter key WPForms forms */

    function wpf_enqueue_disable_enter_script() {
    // Prüfen, ob WPForms aktiv ist
    if (function_exists('wpforms')) {
    // Script registrieren und laden
    wp_enqueue_script(
    'wpf-disable-enter', // Handle
    get_template_directory_uri() . '/custom-js/wpforms_disable-enter.js', // Pfad zur JS-Datei
    ['jquery'], // Abhängigkeit
    null, // Version (optional)
    true // Im Footer laden
    );
    }
    }
    add_action('wp_enqueue_scripts', 'wpf_enqueue_disable_enter_script');
Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Excluding Java-Code’ is closed to new replies.