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.
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.
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?
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());
Then you can add an action and handle the request.
http://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_%28action%29
And for getting the url using JavaScript you can code:
var url = object_name.ajax_url;
Instead of having a separate PHP file, wrap it in WordPress plugin. WordPress Codex will tell you how to write a plugin and how to wire it up to handle your ajax request.
Once you have it working as a plugin you will be able to access all WordPress APIs.