Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Wordpress Rewrite Endpoint Example
* Author Endpoints Example
* Copyright (C) 2010 hakre <>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <>.
* Activate the plugin and the author page has new endpoints.
* @author hakre <>
* @see
return AuthorEndpointPlugin::bootstrap();
* Endpoint
* @author hakre
class Endpoint {
private $name;
private $places;
public function __construct($name, $places) {
$this->name = $name;
$this->places = $places;
add_rewrite_endpoint($this->name, $this->places);
public function getName() {
return $this->name;
public function getValue() {
global $wp_query;
return $wp_query->get($this->name);
* AuthorEndpointsPlugin
* @author hakre
class AuthorEndpointPlugin {
/** @var AuthorEndpointPlugin */
protected static $instance;
static public function bootstrap() {
null === self::$instance
&& self::$instance = new static()
return self::$instance;
/** @var Endpoint */
private $endpoint;
public function __construct() {
add_filter('init', array($this, 'init'));
add_filter('template_redirect', array($this, 'template_redirect'));
public function init() {
$this->endpoint = new Endpoint('tab', EP_AUTHORS);
public function template_redirect() {
$endpoint = $this->endpoint;
'<div%s><b>%s</b>-endpoint value is <i>%s</i></div>'
, ' style="border:0.25em solid #000; background: #ff0; color:#000; font-size:4em; line-height:2em; text-align:center;"'
, $endpoint->getName()
, $endpoint->getValue()

This comment has been minimized.

Copy link

@jessegavin jessegavin commented Nov 24, 2010

I get an error on line 63. I don't know PHP, but if I change it to:

&& self::$instance = new AuthorEndpointPlugin()

It seems to work.


This comment has been minimized.

Copy link
Owner Author

@hakre hakre commented Nov 25, 2010

That's correct, this relates to the PHP version, static was introduced in PHP 5.3, if you're below that version you get an error there. Use it with your change, it does the same for your use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment