Skip to content

Instantly share code, notes, and snippets.

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/8285137fd3d458a4a6984a30cd9576a4 to your computer and use it in GitHub Desktop.
Save westonruter/8285137fd3d458a4a6984a30cd9576a4 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: AMP add hide-content class name to nav.main_menu on page with ID 2906.
* Plugin URI: https://wordpress.org/support/topic/how-to-use-custom-script-in-amp-page/
* Author: Weston Ruter
*/
namespace Add_Hide_Content_Class_Name_To_Main_Menu_On_Page_2906;
add_filter(
'amp_content_sanitizers',
function ( $sanitizers ) {
if ( is_page( 2906 ) ) {
require_once __DIR__ . '/class-sanitizer.php';
$sanitizers[ __NAMESPACE__ . '\Sanitizer' ] = [];
}
return $sanitizers;
}
);
<?php
namespace Add_Hide_Content_Class_Name_To_Main_Menu_On_Page_2906;
class Sanitizer extends \AMP_Base_Sanitizer {
function sanitize() {
$main_menu = $this->dom->xpath->query( '//nav[ contains( @class, "main_menu" ) ]' )->item( 0 );
if ( $main_menu instanceof \DOMElement ) {
$main_menu->setAttribute( 'class', $main_menu->getAttribute( 'class' ) . ' hide_content' );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment