如何使用Word preSS功能于一身的A​​JAX调用 [英] How to use wordpress functions in an ajax call

查看:116
本文介绍了如何使用Word preSS功能于一身的A​​JAX调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以在Ajax调用中使用像query_post()函数?

I wanted to know if there is a way to use function like query_post() in an ajax call ?

让我们说我调用文件_inc / ajax.php

Let's say i'm calling the file _inc/ajax.php

我想abble用字preSS的功能,但我不知道为什么。 谁能帮我吗?

I want to be abble to use the wordpress functions, but i don't know why. Can someone help me for that ?

感谢很多:)

推荐答案

字preSS提供了一个Ajax地址,你应该使用以及一个完整的 AJAX API

WordPress provides an Ajax Url that you should use along with a complete Ajax API.

您需要创建一个jQuery函数。

You need to create a jQuery function.

例如:

jQuery(document).ready(function($) {

    var data = {
        action: 'my_action',
        whatever: 1234
    };

    jQuery.post(ajaxurl, data, function(response) {
        alert('Got this from the server: ' + response);
    });
});

该ajaxurl变种是始终可用的管理方。如果你使用它的前端,你需要定义它。

The ajaxurl var is always available on the admin side. If your using it on the front end you need to define it.

然后一个PHP函数,你可以运行你的查询。 PHP的功能必须得到重视的 wp_ajax_your_action 操作。

Then a PHP function where you can run your query. The PHP function must get attached to the wp_ajax_your_action action.

例如:

add_action('wp_ajax_my_action', 'my_action_callback');

function my_action_callback() {
    global $wpdb; // this is how you get access to the database

    $whatever = intval( $_POST['whatever'] );

    $whatever += 10;

        echo $whatever;

    die(); // this is required to return a proper result
}

wp_ajax_your_action 的行动是管理员,如果你需要使用它的前端的行动将是 wp_ajax_nopriv_your_action

The wp_ajax_your_action action is for admin if you need to use it on the front end the action would be wp_ajax_nopriv_your_action

这篇关于如何使用Word preSS功能于一身的A​​JAX调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆