Skip to content

Instantly share code, notes, and snippets.

@graciano
Last active June 12, 2020 19:03
Show Gist options
  • Save graciano/b2523dd28a3d9091821f77457ccd3fff to your computer and use it in GitHub Desktop.
Save graciano/b2523dd28a3d9091821f77457ccd3fff to your computer and use it in GitHub Desktop.
wordpress AMP plugin using amp.example.com subdomain
<!doctype html>
<html amp="" lang="pt-br">
<head>
<meta charset="utf-8" />
<title><?php echo $this->get( 'document_title' ) ?></title>
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<?php do_action( 'amp_post_template_head', $this ); ?>
<style amp-custom="">
<?php echo file_get_contents(get_template_directory()."/amp/amp_style.css"); ?>
</style>
<script async="" custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>
<script async="" custom-element="amp-social-share" src="https://cdn.ampproject.org/v0/amp-social-share-0.1.js"></script>
<script async="" custom-element="amp-ad" src="https://cdn.ampproject.org/v0/amp-ad-0.1.js"></script>
<script async="" custom-element="amp-iframe" src="https://cdn.ampproject.org/v0/amp-iframe-0.1.js"></script>
<script async custom-element="amp-sticky-ad" src="https://cdn.ampproject.org/v0/amp-sticky-ad-1.0.js"></script>
</head>
<body class="amp-page">
<header>
<a href="<?= home_url() ?>" title="<?= bloginfo() ?>">
<amp-img alt="<?= bloginfo() ?>" height="42" src="logo.png" width="273"></amp-img>
</a>
</header>
<nav class="tab-menu">
<div class="tab-menu-item">
<a href="<?= home_url() ?>">item menu</a>
</div>
<div class="tab-menu-item">
<a href="<?= home_url() ?>">item menu</a>
</div>
<div class="tab-menu-item">
<a href="<?= home_url() ?>">item menu</a>
</div>
<div class="tab-menu-item">
<a href="<?= home_url() ?>">item menu</a>
</div>
</nav>
<div class="content">
<h1>
<?php echo wp_kses_data( $this->get( 'post_title' ) ); ?>
</h1>
<div class="social">
<amp-social-share data-param-app_id="317976714899182" height="44" type="facebook" width="60"></amp-social-share>
<amp-social-share height="44" type="twitter" width="60"></amp-social-share>
<amp-social-share height="44" type="pinterest" width="60"></amp-social-share>
<amp-social-share data-param-text="<?php echo wp_kses_data( $this->get( 'post_title' ) ); ?> - <?php echo $this->get( 'canonical_url' ) ?>" data-share-endpoint="whatsapp://send" height="44" type="whatsapp" width="60"></amp-social-share>
</div>
<div class="article-content">
<?= $this->get( 'post_amp_content' ); ?>
</div>
</div>
</body>
</html>
<?php
//**************************************************************************************************
// AMP
//**************************************************************************************************
//
// Disable amp customizer
add_filter( 'amp_customizer_is_enabled', '__return_false' );
// Change amp template to use the /amp/ inside theme folder
add_filter( 'amp_post_template_file', 'tdg_amp_custom_template', 10, 3 );
function tdg_amp_custom_template( $file, $type, $post ) {
if ( 'single' === $type ) {
$file = dirname( __FILE__ ) . '/amp/amp_post.php';
}
return $file;
}
//**************************************************************************************************
// Render amp page with subdomain
//**************************************************************************************************
//
function render_amp() {
$url = preg_replace("/http(s?):\/\//", "amp.", get_site_url());
if ($_SERVER["HTTP_HOST"] == $url) {
if (is_single()) {
amp_prepare_render();
} else {
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: ".get_site_url().$_SERVER["REQUEST_URI"] );
}
}
}
add_action("wp", "render_amp");
// Change amphtml link tag
add_filter('amp_pre_get_permalink', function($bol, $id) {
return preg_replace(array("/http:\/\//", "/https:\/\//"), array("http://amp.", "https://amp."), get_permalink($id)) ;
}, 10, 2);
@hansdrop
Copy link

@graciano Very nice sharing but can you give information about where to add these codes?

Where can I find amp_post.php?

Thanks!

@graciano
Copy link
Author

I'm sorry I don't remember. I'm not currently maintaining any wordpress sites. But I'd try to put it in the root folder of your active theme if I were you.

@graciano
Copy link
Author

And, of course, add the functions bit in the functions.php

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