I want to make a plugin, that I will use for some jQuery AJAX loading of table data.
I have a function that prints the data开发者_StackOverflow correctly, but how do I "hook" into a specific url?
Like say, I want the function to be run, and the data to be printed whenever a request to /mycustomplugin/myurl.php is run? (Please note that the url/file should not exist)
I have no experience with WP plugins.
To filter your custom URL before Wordpress starts executing queries for other things use something like this:
add_action('parse_request', 'my_custom_url_handler');
function my_custom_url_handler() {
if($_SERVER["REQUEST_URI"] == '/custom_url') {
echo "<h1>TEST</h1>";
exit();
}
}
A simple
if ($_SERVER["REQUEST_URI"] == '/mycustomplugin/myurl.php') {
echo "<my ajax code>";
}
Should work wonders.
If you wanted to return regular wordpress data you could just include wp-blogheader.php into your custom php file like so
//Include Wordpress
define('WP_USE_THEMES', false);
require('Your_Word_Press_Directory/wp-blog-header.php');
query_posts('showposts=10&cat=2');
Just use regular theming tags to return the content you desire. This
Where is your table data coming from though? Are you trying to show this information on the admin side or the viewer side?
Also see for a full breakdown of calling hooked functions with wp_ajax http://codex.wordpress.org/AJAX_in_Plugins
add_action( 'init', 'my_url_handler' );
function my_url_handler() {
if( isset( $_GET['unique_hidden_field'] ) ) {
// process data here
}
}
using add_action( 'init', 'your_handler')
is the most common way in plugins since this action is fired after WordPress has finished loading, but before any headers are sent. Most of WP is loaded at this stage, and the user is authenticated.
精彩评论