接收和处理帖子数据(投票)的WordPress文件


wordpress file which receives and processes post data (voting)

我有jquery/ajax函数,它应该启动jquery upvote插件:

$('#topic').upvote();
var callback = function(data) {
    $.ajax({
        url: '/vote', //this url should point to the adequate file
        type: 'post',
        data: { up: data.upvoted, down: data.downvoted, star: data.starred }
    });
};

此函数中应指向什么文件和 url?

为此,您需要在wordpress安装的根目录中创建一个文件夹(即wp-config.php出现的地方和文件夹wp-content等)在这里创建一个名为投票的文件夹。在此文件夹中创建一个 index.php 文件并将相关代码放入其中。然后,您可以输入网址 http://www.example.com/vote/index.php。

或者,从WordPress仪表板创建一个名为say vote的新页面。为此页面创建自定义模板。然后,您可以通过以下网址访问它。http://www.example.com/vote然后,您可以通过模板php页面完成所有工作

但是,第二种方法必须确保它不会出现在您的站点地图等中。对于两者,建议向机器人添加一些东西.txt以阻止这些页面被索引。

wordpress中执行此操作的正确方法是将其直接发布到admin-ajax.php。在执行此操作之前,您需要注册脚本并创建一个将在 jquery 示例中的"Action"调用中使用的函数

jQuery.ajax ({
    url:  ajaxurl,
    type: "POST", 
    data: {
        postCommentNonce: postCommentNonce,
                    //this is your action function that wp will used for your call
        action:           "stg_AjaxRetRequestl",
                    //used to pass a var this can be anything
        typekey:          "cgetList",  
    });

这是您需要的其余内容的教程。 http://www.andrewmpeters.com/blog/how-to-make-jquery-ajax-json-requests-in-wordpress/。如果你想知道什么是ajaxurl,你可以通过像这样本地化你的脚本来获得它。

    wp_localize_script ("stgjsfiles", "stgjs", array ("ajaxurl" => admin_url ("admin-ajax.php"), 
                        "postCommentNonce" => wp_create_nonce ("stg-post-nonce")));

搜索谷歌,有很多例子