如何对WordPress插件目录中的php文件进行http请求

时间:2015-07-25 作者:Harish_N

I\'am new to wordpress plugin development and I\'am developing a plugin (say myplugin) and I\'am stuck in following problem.

I had kept this plugin in mywebsite/wp-content/plugin/myplugin

Inside myplugin directory i have 2 files as follows

  1. myplugin.php
  2. registration.php

Now i want to make http post request to above registration.php from mobile application. When i sent post request to http://mywebsite/wp-content/plugin/myplugin/registration.php it always says file not found. How can i make a request..

Is it a wrong way to do . If so can any one help me how to do it...

Any help is appreciated.

1 个回复
最合适的回答,由SO网友:Adam 整理而成

下面是一个快速而肮脏的插件,它向您展示了如何实现这一点(根据您自己的架构和需求调整此示例):

<?php 
/*
Plugin Name: Custom rewrite rule test
*/

add_action(
    \'plugins_loaded\', 
    array(Registration::get_instance(), \'setup\')
);

class Registration {

    protected static $instance = NULL;

    public function __construct() {}

    public static function get_instance() {
        NULL === self::$instance and self::$instance = new self;
        return self::$instance;
    }    

    public function setup() {

        add_action(\'init\', array($this, \'rewrite_rules\'));
        add_filter(\'query_vars\', array($this, \'query_vars\'), 10, 1);
        add_action(\'parse_request\', array($this, \'parse_request\'), 10, 1);

        register_activation_hook(__FILE__, array($this, \'flush_rules\' ));

    }

    public function rewrite_rules(){
        add_rewrite_rule(\'registration/?$\', \'index.php?registration=true\', \'top\');
    }

    public function flush_rules(){
        $this->rewrite_rules();
        flush_rewrite_rules();
    }

    public function query_vars($vars){
        $vars[] = \'registration\';
        return $vars;
    }

    public function parse_request($wp){
        if ( array_key_exists( \'registration\', $wp->query_vars ) ){
            include plugin_dir_path(__FILE__) . \'registration.php\';
            exit();
        }
    }

}
  • 使用add_rewrite_rule()
  • 添加自定义查询变量“registration”
  • 分析请求,检查是否存在“注册”查询变量(如果存在),包括registration.php 文件
添加internal WordPress rewrite规则是外部规则的一个,即WordPress环境将在您的registration.php 例如,如果您需要修改或检索数据库中的数据,您可以。

你内心的榜样registration.php 文件:

<?php 

global $wp, $wpdb;

//get query variable: index.php?registration=true
var_dump($wp->query_vars[\'registration\']); //true

//check if user is logged in
if ( is_user_logged_in() ) {
  //use $wpdb to get results from database etc
}

//do your registration business logic here etc

结束

相关推荐

排除Plugins.php HTTP安装路径与单个插件的HTTPS的故障

我正在尝试对插件中显示的安装路径进行故障排除。php作为我在自己的网站上安装的插件,我最初并没有编写该插件。当我的站点的其余部分是HTTPS时,此插件引用和安装的所有资源都会显示为混合内容HTTP。我一直在玩弄下面的代码段,但它似乎没有起到什么作用,也没有解决问题。有人能指出我遗漏了什么吗? /** * registers scripts and stylesheets */ public function register_assets() {&