WordPress: Use Ajax on public site

Other language site
ja ja
Google Translate
  • -

    シェア
  • ---

    LINEで送る
  • -

    はてなブックマーク
    ブックマーク
  • -

    pocket
  • -

    rss
WordPress logo

It is a way to implement Ajax on WordPress publishing site.

WordPress is not assuming too much to use Ajax on publishing sites. However, there is a way to implement it and its way is very easy.

Does WordPress do not assume Ajax implementation on the public site?

In WordPress, the url specified by Ajax is ***/admin-ajax.php.
However, as it is about admin and php filenames, I do not think it is initially supposed to be used on public sites.

Since WordPress is not a web framework but a CMS framework, it is attractive to be able to build a website even if there is not much programming knowledge.

If you can program enough to implement Ajax, other web frameworks may be more convenient. In that sense I think that it was not supposed to be implemented on the public site.

Well then, it is not so troublesome to implement Ajax on the public website of WordPress.
Certainly, trying to prepare a php file apart from admin-ajax.php is a bit cumbersome.

I think that it is quite a brute force method, but WordPress has a method to implement Ajax on the public site using admin-ajax.php.
(I would like you to prepare another file such as user-ajax.php ... ...)

It is almost same as Ajax implementation method of management screen

How to implement Ajax on a public site is almost the same as how it is implemented on the administration screen.
The only difference is the part of add_action() when receiving an Ajax request.

Let's start with the sample code for the management screen.

Sample code is based on the posting of "Use Ajax with WordPress administration screen (plugin)". If you do not know how to implement Ajax in WordPress, we recommend that you refer to it once.

Server processing (php)

function clear_cache() {
    $cache_file = '../cache/11111111111111111.cache';
    $success = __('Complete clear cache');
    $error = __('Clear cache failer');
    $result = '';

    if(file_exists($cache_file)) {
        if(unlink($cache_file)) {
            $result = $success;
        } else {
            $result = $error;
        }
    } else {
        $result = $success;
    }
    echo $result;
    die();
}
add_action('wp_ajax_clear_cache', 'clear_cache');

The hook name of the last line is "wp_ajax _***". Just add add_action with this as "wp_ajax_nopriv _***". You can now implement Ajax on the publishing site.

The changed hook name is for enabling Ajax communication without authenticating the user registered on the administration screen.

It is the form which diverted the one for the management screen to the last. In that sense you can also agree with the file name admin - ajax.php.

If you do not like having an admin in the Ajax request, you will need to prepare a php file separately.
However, WordPress contains considerable proprietary processing between receiving a request and creating HTML. So you need to consider them.

If you make one mistake, you should be careful as functions such as DB access functions provided by WordPress are frequently used in WordPress. That is a troublesome work in that sense.

First of all, you will need to grasp the flow of WordPress processing.

SNS also distributes articles.
Leave a Reply

*

If you like this article, share it!