Home > CMS Tutorial > WordPress > How to prevent page redirection in WordPress?

How to prevent page redirection in WordPress?

PHPz
Release: 2024-03-05 09:33:04
Original
1140 people have browsed it

How to prevent page redirection in WordPress?

How to implement the page non-jump setting in WordPress?

In website development, sometimes we hope to implement a page non-jump setting in WordPress, that is, during certain operations, the page content can be updated without refreshing the entire page. This improves user experience and makes the website smoother. Next, we will share how to implement the page non-jump setting in WordPress and provide specific code examples.

First of all, we can use Ajax to achieve the function of page not jumping. Ajax is a technology that loads data asynchronously in the background without reloading the entire page. In WordPress, we can implement the page non-jump setting through hook functions and Ajax requests.

The following are the steps and code examples to implement the page non-jump setting:

Step 1: Add the following code to the theme’s functions.php file for registering Ajax scripts and setting up Ajax processing Requested callback function:

add_action('wp_enqueue_scripts', 'enqueue_ajax_script');
function enqueue_ajax_script(){
    wp_enqueue_script('custom-ajax-script', get_template_directory_uri().'/js/custom-ajax-script.js', array('jquery'), '1.0', true);
    wp_localize_script('custom-ajax-script', 'ajax_object', array('ajax_url' => admin_url('admin-ajax.php')));
}

add_action('wp_ajax_nopriv_custom_ajax_request', 'custom_ajax_request');
add_action('wp_ajax_custom_ajax_request', 'custom_ajax_request');

function custom_ajax_request(){
    // 在这里处理Ajax请求
    $response = array('message' => '这是通过Ajax请求返回的数据');
    wp_send_json($response);
}
Copy after login

Step 2: Create a js file custom-ajax-script.js in the theme folder, and then add the following code to send Ajax requests:

jQuery(document).ready(function($){
    $('#my-button').click(function(){
        $.ajax({
            url: ajax_object.ajax_url,
            type: 'post',
            data: {
                action: 'custom_ajax_request'
            },
            success: function(response){
                alert(response.message);
            }
        });
    });
});
Copy after login

Step 3: Add a button to the WordPress page or article to trigger an Ajax request:

<button id="my-button">点击我发送Ajax请求</button>
Copy after login

Through the above steps and code examples, we can implement the page non-jump setting in WordPress. When the user clicks the button, the page will request updated data through Ajax, but the entire page will not be refreshed, thus achieving the effect of no page jump.

Summary:

The page non-jump setting is very common in website development, which can improve the user experience and make the website more interactive. The key to achieving page non-jump settings in WordPress is to use Ajax technology and handle Ajax requests in the background. I hope the above content can help you successfully implement page non-jump settings in WordPress.

The above is the detailed content of How to prevent page redirection in WordPress?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template