WordPress中的jQuery.post() [英] jQuery.post() in Wordpress

查看:139
本文介绍了WordPress中的jQuery.post()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为Wordpress编写一个聊天框插件.
该插件使用jQuery.post()方法将数据发送到php文件,以便将输出内容写入html文件,该文件随后将使用jQuery加载到聊天窗口中.

I am currently writing a chatbox plugin for Wordpress.
This plugin uses a jQuery.post() method to send data to a php file in order to write the output to an html file, which in turn will be loaded to the chat window using jQuery.

在我用来将聊天记录写入html文件的php文件中,我需要使用Wordpress API函数,但是Wordpress不包含此php文件,因此无法访问Wordpress API.

Inside the php file I use to write the chat log to an html file I need to use Wordpress API functions, but this php file is not included by Wordpress, and therefore has no access to the Wordpress API.

有没有一种方法可以将此聊天数据发送到Wordpress随附的php文件中,以便可以使用Wordpress API?解决这个问题的最佳方法是什么?

Is there a way to send this chat data to a php file that is included by Wordpress so I can use the Wordpress API? What would be the best way to tackle this problem?

推荐答案

您可以使用wp_localize_script将JavaScript对象中的ajax url发送到页面:

You can use wp_localize_script for sending ajax url in a JavaScript object to the pages:

function my_localized_vars() { 
    return array(
      'site_url' => get_bloginfo('url'), 
      'ajax_url' => admin_url('admin-ajax.php')
    );
} 
wp_localize_script('needle', 'object_name', my_localized_vars());

然后,您可以添加操作并处理请求. http://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_%28action%29

Then you can add an action and handle the request. http://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_%28action%29

要使用JavaScript获取网址,您可以编写以下代码:

And for getting the url using JavaScript you can code:

var url = object_name.ajax_url;

这篇关于WordPress中的jQuery.post()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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