public
Last active

Wordpress Rewrite Endpoint Example

  • Download Gist
endpoint-example.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
<?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;

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.

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.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.