Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* AMP Paired Block Visibility plugin initialization file.
*
* @package AMP_Paired_Block_Visibility
* @author Weston Ruter, Google
* @license GPL-2.0-or-later
* @copyright 2019 Google Inc.
*
* @wordpress-plugin
* Plugin Name: AMP Paired Block Visibility
* Plugin URI: https://gist.github.com/westonruter/0769f147e021e0ebfcb04a084987483a
* Description: For paired AMP sites (in Transitional or Reader template mode), hide any blocks with the <code>noamphtml</code> CSS class supplied via advanced block settings. Conversely, hide any blocks with the <code>amphtml</code> class name present on non-AMP pages.
* Version: 0.1
* Author: Weston Ruter, Google
* Author URI: https://weston.ruter.net/
* License: GNU General Public License v2 (or later)
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Gist Plugin URI: https://gist.github.com/westonruter/0769f147e021e0ebfcb04a084987483a
*/
namespace AMP_Paired_Block_Visibility;
add_filter(
'render_block',
function ( $block_content, $block ) {
if ( ! function_exists( 'is_amp_endpoint' ) || ! isset( $block['attrs']['className'] ) ) {
return $block_content;
}
$is_amp_endpoint = is_amp_endpoint();
if (
( $is_amp_endpoint && preg_match( '/(^|\s)noamphtml(\s|$)/', $block['attrs']['className'] ) )
||
( ! $is_amp_endpoint && preg_match( '/(^|\s)amphtml(\s|$)/', $block['attrs']['className'] ) )
) {
return '';
}
return $block_content;
},
10,
2
);
@westonruter

This comment has been minimized.

Copy link
Owner Author

@westonruter westonruter commented Apr 10, 2020

Installation instructions: https://gist.github.com/westonruter/6110fbc4bef0c4b8c021a112012f7e9c

Usage:

image

image

Frontend results:

Screen Shot 2020-04-10 at 10 53 52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment