WordPress网站不会随着对Javascript文件的更改而更新。

huangapple go评论77阅读模式
英文:

WordPress Website not updating with changes to Javascript file

问题

我正在开发一个网站,我已经为搜索栏创建了一个WordPress插件。该插件有一个js文件夹和文件,但对该文件所做的更改没有在网站上显示。

我已经清除了浏览器的数据,清除了服务器的缓存(Cloudways Varnish),并安装了一个清除WordPress缓存的插件(Cloudways的Breeze)。但似乎都没有起作用。

我可以在WordPress插件文件编辑器上看到JS文件正在更新,但更改不会在实际网站上显示。

dynamic-search-plugin.php文件:

<?php

function enqueue_ajax_scripts() {
    wp_enqueue_script( 'dynamic-search-ajax', plugin_dir_url( __FILE__ ) . 'js/dynamic-search.js', array( 'jquery' ), '1.0', true );
    wp_localize_script( 'dynamic-search-ajax', 'dynamicSearchAjax', array(
        'ajaxurl' => admin_url( 'admin-ajax.php' ),
        'nonce'   => wp_create_nonce( 'dynamic-search-nonce' )
    ) );
}

function dynamic_search_handler() {
    check_ajax_referer( 'dynamic-search-nonce', 'nonce' );

    // Retrieve the search query from the AJAX request
    $query = sanitize_text_field( $_POST['query'] );

    $results = array(
        array( 'name' => 'Test 1' ),
        array( 'name' => 'Test 2' )
    );

    // Return the search results as a JSON response
    wp_send_json( $results );
}

add_action( 'wp_enqueue_scripts', 'enqueue_ajax_scripts' );
add_action( 'wp_ajax_dynamic_search', 'dynamic_search_handler' );
add_action( 'wp_ajax_nopriv_dynamic_search', 'dynamic_search_handler' );

dynamic-search.js文件:

jQuery(document).ready(function($) {
    
    // Event handler for search form submission
    $('#search-form').on('submit', function(e) {
        console.log("On submit in JS called");
        e.preventDefault();

        var query = $('#search-input').val();

        // Make AJAX request to the server
        $.ajax({ //只有这个部分出于某种原因运行
            url: dynamicSearchAjax.ajaxurl,
            type: 'POST',
            data: {
                action: 'dynamic_search',
                nonce: dynamicSearchAjax.nonce,
                query: query
            },
            success: function(response) {
                displayResults(response);
            },
            error: function() {
                alert('Error');
            }
        });
    });

    // Function to display search results
    function displayResults(results) {
        var container = $('#results');
        container.empty();

        // Loop through the results and create buttons
        results.forEach(function(result) {
            var button = $('<button>').text("Test"); //更改此代码不会在网站上显示更改
            container.append(button);

            // Add click event handler to the button
            button.on('click', function() {
                // 按钮被点击时执行操作
                alert('Button clicked:');
            });
        });
    }
});
英文:

I am developing a website and I have created a WordPress plugin for a search bar. The plugin has a js folder and file but the changes made to that file are not appearing on the website.

I have cleared my browser's data, my server's cache (Cloudways Varnish), and have installed a plugin to clear WordPress cache (Cloudways' Breeze). None seem to work.

I can see on the WordPress Plugin file editor that the JS file is updating, but the changes are not appearing on the live website.

dynamic-search-plugin.php file:


&lt;?php

function enqueue_ajax_scripts() {
    wp_enqueue_script( &#39;dynamic-search-ajax&#39;, plugin_dir_url( __FILE__ ) . &#39;js/dynamic-search.js&#39;, array( &#39;jquery&#39; ), &#39;1.0&#39;, true );
    wp_localize_script( &#39;dynamic-search-ajax&#39;, &#39;dynamicSearchAjax&#39;, array(
        &#39;ajaxurl&#39; =&gt; admin_url( &#39;admin-ajax.php&#39; ),
        &#39;nonce&#39;   =&gt; wp_create_nonce( &#39;dynamic-search-nonce&#39; )
    ) );

}

function dynamic_search_handler() {
    check_ajax_referer( &#39;dynamic-search-nonce&#39;, &#39;nonce&#39; );

    // Retrieve the search query from the AJAX request
    $query = sanitize_text_field( $_POST[&#39;query&#39;] );

    $results = array(
        array( &#39;name&#39; =&gt; &#39;Test 1&#39; ),
        array( &#39;name&#39; =&gt; &#39;Test 2&#39; )
    );

    // Return the search results as a JSON response
    wp_send_json( $results );

}

add_action( &#39;wp_enqueue_scripts&#39;, &#39;enqueue_ajax_scripts&#39; );
add_action( &#39;wp_ajax_dynamic_search&#39;, &#39;dynamic_search_handler&#39; );
add_action( &#39;wp_ajax_nopriv_dynamic_search&#39;, &#39;dynamic_search_handler&#39; );

The dynamic-search.js file:

jQuery(document).ready(function($) {
    
    // Event handler for search form submission
    $(&#39;#search-form&#39;).on(&#39;submit&#39;, function(e) {
        console.log(&quot;On submit in JS called&quot;);
        e.preventDefault();

        var query = $(&#39;#search-input&#39;).val();

        // Make AJAX request to the server
        $.ajax({ //only this is run for some reason
            url: dynamicSearchAjax.ajaxurl,
            type: &#39;POST&#39;,
            data: {
                action: &#39;dynamic_search&#39;,
                nonce: dynamicSearchAjax.nonce,
                query: query
            },
            success: function(response) {
                displayResults(response);
            },
            error: function() {
                alert(&#39;Error&#39;);
            }
        });
    });

    // Function to display search results
    function displayResults(results) {
        var container = $(&#39;#results&#39;);
        container.empty();

        // Loop through the results and create buttons
        results.forEach(function(result) {
            var button = $(&#39;&lt;button&gt;&#39;).text(&quot;Test&quot;); //changing this code doesn&#39;t correspond to changes on website
            container.append(button);

            // Add click event handler to the button
            button.on(&#39;click&#39;, function() {
                // Perform an action when the button is clicked
                alert(&#39;Button clicked:&#39;); 
            });
        });
    }
});

答案1

得分: 1

这是翻译好的内容:

有时在WordPress中,当它缓存您的脚本时会出现这种情况。尝试像下面这样向您的脚本文件添加 time() 参数:

wp_enqueue_script( 'dynamic-search-ajax', plugin_dir_url( __FILE__ ) . 'js/dynamic-search.js?time=' . time(), array( 'jquery' ), '1.0', true );
英文:

It happens in worpdress sometimes when it caches your script, Try adding time() param to your script file like below,

 wp_enqueue_script( &#39;dynamic-search-ajax&#39;, plugin_dir_url( __FILE__ ) . &#39;js/dynamic-search.js?time=&#39;.time(), array( &#39;jquery&#39; ), &#39;1.0&#39;, true );

huangapple
  • 本文由 发表于 2023年5月31日 22:38:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76374673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定