• Hi,

    I have BetterLinks installed in a new site that was created by migrating a copy of an existing site.

    In the new site, I am not using BetterLinks. So I deactivated it and then all my javascript code stopped working.
    For example, something as simple as the below is not working when BetterLinks is deactivated but it works OK when its activated.
    This simply takes the entry of a text field and populates another field.

    When BetterLinks is active, I get the ALERT of 1 and ALERT of 2 as the page loads.
    When I deactivate it, I get no ALERT messages.

    Doesn’t that seem weird? Any ideas?

    I have since removed the ALERT messages so you won’t see them. But I deleted BetterLinks and the JS would not work. I reinstalled it and now it works. So why do I need BetterLinks for JS to work?

    Thank you

    jQuery(document).ready(function($) {
    alert(1);
    // Function to update text field when dropdown is changed
    function updateTextField(selectField, textField) {
    alert(2);
    selectField.on('change', function() {
    textField.val($(this).val());
    });
    }

    // Update location text field when location dropdown changes
    updateTextField(
    $('.wpcf7-form [name="location-select"]'),
    $('.wpcf7-form [name="location-typed"]')
    );

    // Update work text field when work dropdown changes
    updateTextField(
    $('.wpcf7-form [name="work-select"]'),
    $('.wpcf7-form [name="work-typed"]')
    );
    });


    • This topic was modified 1 week, 1 day ago by flatword.

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

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Pial

    (@iapial)

    Hello @flatword

    Thank you for reaching out and providing the detailed explanation of the issue you’re facing. I understand how puzzling this situation must be. Let me break down what’s likely happening and offer some suggestions.

    The behavior you’re observing could be due to one of the following:

    1. Script Dependencies:
      BetterLinks might be loading certain JavaScript files, such as jQuery, or resolving dependencies that your custom code indirectly relies on. When BetterLinks is deactivated, those dependencies may not be loaded, causing your code to stop functioning.
    2. Script Loading Order:
      BetterLinks might be influencing the order in which scripts are loaded on your site, ensuring everything works correctly. When it’s removed, the script order might change, resulting in your custom code not executing as expected.
    3. Caching or Optimization:
      If your site uses caching or optimization plugins, they might be optimized differently when BetterLinks is active. This can result in unexpected behavior when the plugin is deactivated.

    Suggested Solutions

    1. Ensure jQuery is Loaded:
      If your script depends on jQuery, it’s essential to ensure it’s being loaded properly when BetterLinks is deactivated. If you’re adding custom code, please confirm it’s properly enqueued in your theme or plugin using this code snippet:
       wp_enqueue_script('custom-script', 'path-to-your-script.js', array('jquery'), null, true);
    1. Check the Console for Errors:
      After deactivating BetterLinks, check the browser console for any errors (Press F12 in your browser, then go to the “Console” tab). This can help identify what’s breaking.
    2. Temporarily Disable Optimization Plugins:
      If you’re using caching or optimization plugins, try disabling them temporarily to see if the issue persists without BetterLinks.
    3. Test with a Basic Script:
      Here’s a simple test you can run to confirm if the issue is related to jQuery:
       jQuery(document).ready(function($) {
           alert("Testing jQuery");
       });

    Add this to your site after deactivating BetterLinks to see if it works.

    Best regards,
    Pial

    Plugin Support Pial

    (@iapial)

    Hi @flatword

    I just wanted to follow up regarding the issue you reported with your JavaScript not working after deactivating BetterLinks. I shared a few possible causes and troubleshooting steps in my earlier response, and I wanted to check if you’ve had a chance to review them or test any of the solutions.

    Let me know how things are going

    Best regards,
    Pial

    Thread Starter flatword

    (@flatword)

    I tried all 4 of your suggestions and here are the results.

    This alert message works when BetterLinks is active but not when it is deactivated.

    jQuery(document).ready(function($) { alert("Testing jQuery"); });

    When I deactivate BetterLinks, the console displays this error for 3 different lines:
    jmlcontractingapp/:265 Uncaught ReferenceError: jQuery is not defined

    I am adding the javascript using a plugin for wordpress called WPCode.

    I added this code:

       wp_enqueue_script('custom-script', 'path-to-your-script.js', array('jquery'), null, true);

    …and with betterlinks not activated, that line gives an error

    Uncaught ReferenceError: wp_enqueue_script is not defined

    …and then the other 3 errors display.

    Even with betterlink active, I still get that 1 message

    jmlcontractingapp/:267 Uncaught ReferenceError: wp_enqueue_script is not defined

    And I disabled the speed optimization plugin but that made no difference.

    • This reply was modified 3 days, 23 hours ago by flatword.
    Plugin Support Pial

    (@iapial)

    Hi @flatword

    The issue occurs because WordPress doesn’t automatically load jQuery unless explicitly enqueued. BetterLinks likely enqueues it as part of its functionality, which is why your code works when the plugin is active but breaks when it’s deactivated. Additionally, wp_enqueue_script is a PHP function and cannot be directly used in WPCode’s JavaScript snippets.

    To resolve this, you need to enqueue both jQuery and your custom script using WordPress’s standard wp_enqueue_script function. Here’s how to do it: Step 1: Add a PHP Snippet in WPCode

    Switch to a PHP Snippet in WPCode and use the following code:

    add_action('wp_enqueue_scripts', function () {
        // Enqueue WordPress's built-in jQuery
        wp_enqueue_script('jquery');
    
        // Enqueue your custom script
        wp_enqueue_script(
            'custom-script', // Unique handle for your script
            get_template_directory_uri() . '/path-to-your-script.js', // Replace with your script's file path
            array('jquery'), // Declare jQuery as a dependency
            null, // Optional version number
            true // Load in the footer
        );
    });

    Step 2: Replace the Script Path

    Update /path-to-your-script.js with the actual path to your custom script. For example:

    get_template_directory_uri() . '/js/my-custom-script.js'

    Why This Works

    • wp_enqueue_script: This function registers and loads JavaScript files.
    • array('jquery'): Ensures jQuery is loaded before your custom script.
    • true (in footer): Ensures the script loads at the bottom of the page for better performance.

    Additional Notes

    You can learn more about the wp_enqueue_script function here in the WordPress Developer Reference.

    Best regards,
    Pial

    Thread Starter flatword

    (@flatword)

    Thanks for all the information.

    But it’s weird in that I was going to test your suggestions and I deactivated BetterLinks and the javascript still worked without me doing anything.

    So I deleted BetterLinks and it still works.

    I did clear the cache and it still works.

    I’m cautiously optimistic because it just doesn’t make sense.

    Plugin Support Pial

    (@iapial)

    Hi @flatword

    Thank you for the update! That is indeed unusual, but it’s great to hear that your JavaScript is working now, even after deactivating and deleting BetterLinks.

    It’s possible that the issue was related to cached files or a temporary conflict that resolved itself after clearing the cache and removing the plugin. Sometimes, scripts or dependencies can linger in cache, causing inconsistencies until they’re fully cleared.

    I understand your cautious optimism—it’s good to monitor the behavior for a bit to ensure it remains stable. If the issue arises again or if anything seems off, feel free to reach out, and I’ll be happy to assist further.

    Fingers crossed it stays resolved!

    Best regards,
    Pial

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.