Code Monkey home page Code Monkey logo

wp_route's Introduction

WP_Route

A simple way to make custom routes in WordPress.

WP_Route is a simple way to create custom routes in WordPress for listening for webhooks, oAuth callbacks and basic routing. WP_Route is a single class solution that does not require any set-up and supports route parameters and redirects.

Introduction: Medium Post

WP_Route::get('flights',                        'listFlights');
WP_Route::post('flights/{flight}',              'singleFlight');
WP_Route::put('flights/{flight}/book/{date}',   'bookFlight');
WP_Route::delete('flights/{flight}/delete',     'deleteFlight');

WP_Route::any('flights/{flight}',   array('Class', 'staticMethod'));
WP_Route::patch('flights/{flight}', array($object, 'method'));
WP_Route::match(['get', 'post'],    'flights/{flight}/confirm', 'confirmFlight');
WP_Route::redirect('from/here',     '/to/here', 301);

Installation

Require WP_Route with composer

$ composer require anthonybudd/WP_Route

Or

Download the WP_Route class and require it at the top of your functions.php file. This is not recommended.

require 'WP_Route/src/WP_Route.php';

GET Started

Simply define a route by calling any of the static methods get(), post(), put(), patch(), delete() or any(). This will bind the specified URL to a callable. When a HTTP request bound for that URL is detected, WP_Route will call the callable.

WP_Route::get('flights', 'listFlights');

// http://example.com/flights
function listFlights(){
  
   // Your Code Here!  
  
}

Parameters

If you need to extract route parameters from the request URI you can do this by wrapping the value to be extracted in curly brackets. The extracted values will be provided to the callable as function arguments as shown below.

WP_Route::get('flights/{flight}', 'singleFlight');

function singleFlight($flight){
    echo $flight; // 1
}

Methods

get($route, $callable)

any(), post(), put(), patch(), delete()

All of these methods are used for binding a specific route and HTTP request method to a callable. The method any() will bind a route to a callable but will be HTTP method agnostic.

WP_Route::get('flights',           'listFlights');
WP_Route::post('flights/{flight}', array('FlightController', 'singleFlight'));

function listFlights(){
	// Your Code Here
}
  
Class FlightController{
	public static function singleFlight($flight){
		// Your Code Here
	}
}

match($methods, $route, $callable)

If you want to bind a callable to multiple HTTP methods but you do not want to use any(), you can use match(). The first parameter must be an array of HTTP request methods. The arguments $route and $callable work the same as get().

WP_Route::match(['get', 'post'], 'flights/{flight}/confirm', 'confirmFlight');

function confirmFlight($flight){
	// Your Code Here
}

redirect($route, $redirect, $code = 301)

The redirect() method will redirect the user to the argument $redirect when they navigate to the route. To set a custom HTTP response code use the 3rd argument $code.

WP_Route::redirect('open-google', 'https://google.com', 301);

wp_route's People

Contributors

anthonybudd avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.