Wordpress plugin: Hook on custom url

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 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.

Answers


A simple

if ($_SERVER["REQUEST_URI"] == '/mycustomplugin/myurl.php') {
  echo "<my ajax code>";
}

Should work wonders.


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();
   }
}

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.


Need Your Help

Looking for a Command Line Argument Parser for .NET

c# .net command-line arguments

I'm looking for a command line argument parser, such as "Command line parser" from http://www.sellsbrothers.com/tools/Genghis/ .