1

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.

  • Is there a reason for not using standard login with registration allowed? Without seeing your code it is hard to determine the cause for your problem. – Frank P. Walentynowicz Jul 25 '15 at 6:30
  • Iam creating a plugin which sends notification to all subscribed users when a new post is created. User can subscribe by making http post request to register.php. since i placed the registration.php inside plugin directory iam not able to access it from outstide.. How to do that – Harish_N Jul 25 '15 at 6:43
1

Here is a quick and dirty plugin that shows you how to achieve this (adapt this example to your own architecture and needs):

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

}
  • register your custom rewrite rule "registration" using add_rewrite_rule()
  • add a custom query variable "registration"
  • parse the request, check for "registration" query variable, if exists, include a registration.php file

The advantage of adding an internal WordPress rewrite rule isntead of an external one is that the WordPress environment will be available to you in your registration.php file, so for example if you need to modify or retrieve data from the database you can.

Example inside your registration.php file:

<?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
  • Which URL should i use to make a call – Harish_N Jul 25 '15 at 11:03
  • example.com/registration – Adam Jul 25 '15 at 11:08
  • i have installed wordpress in my pc. I installed it in a folder called fwp. When i make the following request 127.0.0.1/fwp/registration it says The requested URL /fwp/registration.php was not found on this server – Harish_N Jul 25 '15 at 12:29
  • @Harish_N registration, not registration.php. You're using an internal rewrite to route the request, as you should not be loading php files directly. – Milo Jul 25 '15 at 15:41
  • @Harish_N Make sure the plugin file above is in the same directory as your registration.php file, if not, you need to edit the path in the parse_request callback method. – Adam Jul 26 '15 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.