Coder Social home page Coder Social logo

wp-routes's Introduction

#WP Routes

Easy WordPress routing plugin with klein.php

Code Example

In my plugin or theme functions.php file:

add_action('wp-routes/register_routes', function(){

	// easy to remember admin links
	respond('/admin', function(){
		wp_safe_redirect(admin_url());
		die();
	});
	respond('/login', function(){
		wp_safe_redirect(wp_login_url());
		die();
	});
	
	// an easy JSON route		
	with('/posts', function(){
		respond('GET', '/[i:id]', function($request){
			$post = get_post($request->id);
			if ($post) {echo json_encode($post);}
			else {echo json_encode(array('error' => 'Not a valid post ID'));}
		});
		respond('POST', '/new', function($request){
			if(!wp_verify_nonce($_POST['wp_nonce'], 'add-new-post')) {
				echo json_encode(array('error' => 'Not authorized to create new posts'));
				return;
			}	
			$id = wp_insert_post('post_title' => $request->title);
			if ($post) {echo json_encode(get_post($id));}
			else {echo json_encode(array('error' => 'There was an error creating the post'));}
		});
	});
});

Thanks Klein

The possibilities above are possible thanks to the klein.php library by Chris O'Hara; I've merely ported v. 1.2.0 of the library to be back compatible with PHP 5.2 and used the goodness.

Installation

Download the .zip file and put in WordPress plugin folder.

Usage

The plugin packs the klein52 library and allow plugins and themes developers to use any of its methods.
An action, wp-routes/register_routes, is fired before WordPress parses and handles the current request, see the code example above.
If a route echoes something and it's not a catch-all route, one that matches on "*", then WordPress normal handling flow will be interrupted and the request will be responsibility of the route.
The routing happens when WordPress is fully loaded along with its plugins and the current theme so any WordPress defined function will be available.

wp-routes's People

Contributors

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