Created
February 18, 2020 12:57
-
-
Save pierlon/11cc7bf6703e391b85cda96079b44bf7 to your computer and use it in GitHub Desktop.
Replaces amp query with /amp/ endpoint slug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Custom AMP endpoint | |
* Description: Replaces ?amp=1 with /amp/ for posts and pages | |
* Author: Pierre Gordon | |
* Version: 0.1.0 | |
* License: GPLv2 or later | |
*/ | |
namespace Custom_AMP_Endpoint; | |
add_filter( 'amp_override_url', '__return_true' ); | |
function get_amp_url( $url, $slug ) { | |
return trailingslashit( $url ) . $slug; | |
} | |
function init() { | |
add_filter( 'amp_url', __NAMESPACE__ . '\get_amp_url', 10, 2 ); | |
} | |
add_action( 'init', __NAMESPACE__ . '\init' ); | |
function is_amp_url( $is_amp_url, $url, $slug ) { | |
return (bool) preg_match( "#/{$slug}/?$#", $url ); | |
} | |
add_filter( 'is_amp_url', __NAMESPACE__ . '\is_amp_url', 10, 3 ); | |
function set_rewrite_rule( $slug ) { | |
add_rewrite_endpoint( $slug, EP_PERMALINK|EP_PAGES ); | |
} | |
add_action( 'amp_set_rewrite_rule', __NAMESPACE__ . '\set_rewrite_rule' ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment