diff --git a/eth-simple-shortlinks.php b/eth-simple-shortlinks.php index debc7560df21ee3080ee5067ea8423676e2a9a7b..048c6d8a79f99ae97199b75119119e75aa2636e2 100644 --- a/eth-simple-shortlinks.php +++ b/eth-simple-shortlinks.php @@ -53,22 +53,27 @@ class ETH_Simple_Shortlinks { /** * Class properties */ + private $slug = 'p'; private $qv = 'eth-shortlink'; /** * */ private function __construct() { + // Request add_action( 'init', array( $this, 'add_rewrite_rule' ) ); add_filter( 'query_vars', array( $this, 'filter_query_vars' ) ); add_action( 'parse_request', array( $this, 'action_parse_request' ) ); + + // Shortlink + add_filter( 'get_shortlink', array( $this, 'filter_get_shortlink' ), 10, 2 ); } /** * Register rewrite rule */ public function add_rewrite_rule() { - add_rewrite_rule( '^p/([\d]+)/?$', 'index.php?p=$matches[1]&' . $this->qv . '=1', 'top' ); + add_rewrite_rule( '^' . $this->slug . '/([\d]+)/?$', 'index.php?p=$matches[1]&' . $this->qv . '=1', 'top' ); } /** @@ -101,6 +106,25 @@ class ETH_Simple_Shortlinks { exit; } } + + /** + * Override shortlinks with this plugin's format + */ + public function filter_get_shortlink( $shortlink, $id ) { + if ( empty( $id ) ) { + $_p = get_post(); + + if ( ! empty( $_p->ID ) ) { + $id = $_p->ID; + } + } + + if ( empty( $id ) ) { + return $shortlink; + } + + return user_trailingslashit( home_url( sprintf( '%s/%d', $this->slug, $id ) ) ); + } } ETH_Simple_Shortlinks::get_instance();