WordPress development tutorial: the original ecology of the wordpress ajax

Recently small V (site friends) in the development of an adaptive wordpress blog theme, the wordpress theme of a large number of data display and submission are used ajax technology, so small V specially written a tutorial record, to facilitate other wordpress developers do wordpress ajax application development time to do reference. If some small partners do not know what is ajax, please Baidu Baidu ~ ~, gossip is not to say, the tutorial began.
Many developers in the write plug-in like the following way to achieve ajax:

  1. require_once( ‘../../../../wp-config.php’ );

Even the Baidu webmaster platform official development of the wordpress structured data submitted plug-in is also the realization of ajax, which is a very irrational wording, because once the user set the directory is different, the relative path will be invalid. And if you Your plugin is using object-oriented writing, you will not be able to directly use some variables and private methods. The biggest drawback is that will lead to the entire WordPress framework to reload again, seriously affecting the efficiency of the site.
In fact, wordpress in order to facilitate the development of developers with ajax function plug-in theme has been pre-defined ajax method, we only need to use the official reservation method to use just fine. In the development of wordpress ajax you first understand that, whether it is the background or foreground any Ajax requests are in admin-ajax.php deal, send a request to admin-ajax.php, there is a required parameter is $ _REQUEST [‘action’] because admin-ajax.php needs to trigger different hooks depending on whether the user is logged in.

  1. if ( is_user_logged_in() ) {
  2.     /**
  3.      * Fires authenticated AJAX actions for logged-in users.
  4.      *
  5.      * The dynamic portion of the hook name, `$_REQUEST[‘action’]`,
  6.      * refers to the name of the AJAX action callback being fired.
  7.      *
  8.      * @since 2.1.0
  9.      */
  10.     do_action( ‘wp_ajax_’ . $_REQUEST[‘action’] );
  11. else {
  12.     /**
  13.      * Fires non-authenticated AJAX actions for logged-out users.
  14.      *
  15.      * The dynamic portion of the hook name, `$_REQUEST[‘action’]`,
  16.      * refers to the name of the AJAX action callback being fired.
  17.      *
  18.      * @since 2.8.0
  19.      */
  20.     do_action( ‘wp_ajax_nopriv_’ . $_REQUEST[‘action’] );
  21. }

The above code from the wordpress system file admin-ajax.php, from the above code we can see that we want to initiate a request first to know whether this request is initiated in the login state? Different state trigger hook is not the same, the following is a theme in the use of jquery achieve ajax example code:

  1. add_action (‘wp_head’, ‘v7v3AjaxScript’);
    function v7v3AjaxScript () {
    print ‘<script> var v7v3_ajaxurl = “‘. admin_url (‘admin-ajax.php’). ‘”‘. “; </ script> n”;
    print “<script>
    $ (docunment) .ready (function () {
    $ .ajax ({
    url: v7v3_ajaxurl,
    data: {
    action: ‘is_login’
    },
    type: ‘post’,
    cache: false,
    success: function (data) {
    alert (data);
    }
    });
    });
    </ scrip> “;
    }

    add_action (‘wp_ajax_nopriv_is_login’, ‘v7v3_ajax_not_login’);
    add_action (‘wp_ajax_is_login’, ‘v7v3_ajax_is_login’);

    function v7v3_ajax_is_login () {
    global $ current_user;
    get_currentuserinfo ();
    printf (‘Login User Hello:% s!’, $ current_user-> display_name);
    / / Die before the end of the die or exit is very important, we must add.
    die;
    }

    function v7v3_ajax_not_login () {
    print ‘Dear Visitors Hello! ‘;
    / / Die before the end of the die or exit is very important, we must add.
    die;
    }

In the development of wordpress ajax application is the most important point before the end of the function must be added die or exit, if not add the wordpress framework will be reloaded again.