在AJAX PHP文件中正确使用WordPress函数

时间:2017-02-26 作者:LaGuille

在我的custom-js.js 文件:

$(function() {

        $(".product-vote-button-up").click(function(){
            var productID = $(this).attr(\'productID\');      

            var request = $.ajax({
                url: "/wordpress/wp-content/themes/my-theme/js/ajax/vote-database-update.php",
                type: "POST",
                data: {id : productID},
            });

            request.done(function() {
                $("#product-" + productID + " .item-product-footer-vote-container").html(\'Thanks for your vote!\');
            });

            request.fail(function(jqXHR, textStatus) {
              alert( "Request failed: " + textStatus );
            });
        });

    }); 
使用AJAX,我想用SQL命令更新自定义产品数据库中的投票计数。为此,我想访问中的WordPress函数vote-database-update.php 例如$wpdb. 到目前为止,我成功实现这一目标的唯一方法是添加以下内容:

require_once( explode( "wp-content" , __FILE__ )[0] . "wp-load.php" );
在我的PHP文件中。然而,我认为这并不是很干净,在阅读了一些文档之后,我认为还有另一种方法admin-ajax.php. 在大多数文档中,它描述了如何使用admin-ajax.php. 自从我vote-database-update.php 不是真正的插件(或者我可能没有WordPress插件的正确定义)。在我的投票数据库更新中包含WordPress函数的正确方法是什么。php文件?

1 个回复
最合适的回答,由SO网友:Paul \'Sparrow Hawk\' Biron 整理而成

是,使用admin-ajax.php 就是去这里的路。要使用它,您可以执行以下操作:

在vote\\u database\\u update中。php

// hook into admin-ajax
// the text after \'wp_ajax_\' and \'wp_ajax_no_priv_\' in the add_action() calls
// that follow is what you will use as the value of data.action in the ajax
// call in your JS

// if the ajax call will be made from JS executed when user is logged into WP,
// then use this version
add_action (\'wp_ajax_call_your_function\', \'your_function\') ;
// if the ajax call will be made from JS executed when no user is logged into WP,
// then use this version
add_action (\'wp_ajax_nopriv_call_your_function\', \'your_function\') ;

function
your_function ()
{
    if (!isset ($_REQUEST[\'id\'])) {
        // set the return value you want on error
        // return value can be ANY data type (e.g., array())
        $return_value = \'your error message\' ;

        wp_send_json_error ($return_value) ;
        }

    $id = intval ($_REQUEST[\'id\']) ;
    // do processing you want based on $id

    // set the return value you want on success
    // return value can be ANY data type (e.g., array())
    $return_value = \'your success message/data\' ;

    wp_send_json_success ($return_value) ;
}

在自定义js中。js公司

(function ($) {
    $(\'.product-vote-button-up\').click (function () {
        var productID = $(this).attr (\'productID\') ;

        $.ajax ({
            url: \'/wp-admin/admin-ajax.php\',
            type: \'POST\',
            dataType: \'JSON\',
            data: {
                // the value of data.action is the part AFTER \'wp_ajax_\' in
                // the add_action (\'wp_ajax_xxx\', \'yyy\') in the PHP above
                action: \'call_your_function\',
                // ANY other properties of data are passed to your_function()
                // in the PHP global $_REQUEST (or $_POST in this case)
                id : productID
                },
            success: function (resp) {
                if (resp.success) {
                    // if you wanted to use the return value you set 
                    // in your_function(), you would do so via
                    // resp.data, but in this case I guess you don\'t
                    // need it
                    $(\'#product-\' + productID + \' .item-product-footer-vote-container\').html (\'Thanks for your vote!\') ;
                    }
                else {
                    // this "error" case means the ajax call, itself, succeeded, but the function
                    // called returned an error condition
                    alert (\'Error: \' + resp.data) ;
                    }
                },
            error: function (xhr, ajaxOptions, thrownError) {
                // this error case means that the ajax call, itself, failed, e.g., a syntax error
                // in your_function()
                alert (\'Request failed: \' + thrownError.message) ;
                },
            }) ;
        }) ;
    })(jQuery) ;
我希望这有帮助。

相关推荐

自定义模板earch.php中的搜索结果

现在我在搜索结果页面(search.php)中遇到了一个问题,结果是由索引页面模板显示的,而不是它的模板,它显示所有帖子,不管搜索者是否输入了内容我的索引。php页面<?php get_header(); ?> <?php $search = $_get[\'search\']; $args = array( \'post_title_like\' => $search ); $res = new wp_query($args