Skip to content

Instantly share code, notes, and snippets.

@hakre
Created November 18, 2010 20:20
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save hakre/705545 to your computer and use it in GitHub Desktop.
Wordpress Rewrite Endpoint Example
<?php
/**
* Author Endpoints Example
*
* Copyright (C) 2010 hakre <http://hakre.wordpress.com/>
*
* 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
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* 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 <http://www.gnu.org/licenses/>.
*
* USAGE:
*
* Activate the plugin and the author page has new endpoints.
*
* @author hakre <http://hakre.wordpress.com>
* @see http://wordpress.stackexchange.com/questions/4243/how-to-set-up-sub-categories-for-author-pages/4256
*/
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);
$GLOBALS['wp_rewrite']->flush_rules(false);
}
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;
printf(
'<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()
);
}
}
#EOF;
@jessegavin
Copy link

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.

@hakre
Copy link
Author

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