简体   繁体   English

Wordpress插件:挂钩自定义网址

[英]Wordpress plugin: Hook on custom url

I want to make a plugin, that I will use for some jQuery AJAX loading of table data. 我想制作一个插件,我将用于一些jQuery AJAX加载表数据。

I have a function that prints the data correctly, but how do I "hook" into a specific url? 我有一个正确打印数据的函数,但我如何“挂钩”到特定的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? 比方说,我希望运行该函数,并且每当运行对/mycustomplugin/myurl.php的请求时都要打印数据? (Please note that the url/file should not exist) (请注意,url /文件不应该存在)

I have no experience with WP plugins. 我没有使用WP插件的经验。

To filter your custom URL before Wordpress starts executing queries for other things use something like this: 要在Wordpress开始执行其他事物的查询之前过滤您的自定义URL,请使用以下内容:

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 如果你想返回常规wordpress数据,你可以将wp-blogheader.php包含在你的自定义php文件中,就像这样


//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 另请参阅使用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. 使用add_action( 'init', 'your_handler')是插件中最常用的方式,因为在WordPress加载完成后但在发送任何标头之前触发此操作 Most of WP is loaded at this stage, and the user is authenticated. 在此阶段加载大多数WP,并对用户进行身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM