Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active March 31, 2022 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westonruter/0769f147e021e0ebfcb04a084987483a to your computer and use it in GitHub Desktop.
Save westonruter/0769f147e021e0ebfcb04a084987483a to your computer and use it in GitHub Desktop.
<?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
Copy link
Author

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

@Triskeletor
Copy link

Please, can you consider making a version that works for WPBakery Visual composer? TIA!

@westonruter
Copy link
Author

@Triskeletor Sorry, I don't have any experience with that plugin.

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