Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* AMP Reader Theme Widgets plugin bootstrap.
*
* @package Google\AMP_Reader_Theme_Widgets
* @author Weston Ruter, Google
* @license GPL-2.0-or-later
* @copyright 2021 Google Inc.
*
* @wordpress-plugin
* Plugin Name: AMP Reader Theme Widgets
* Plugin URI: https://gist.github.com/westonruter/8a7c84360db2aff108e4fdaccf12772e
* Description: When a core theme is used as a Reader theme, expose its sidebars on the Widgets admin screen prefixed by "AMP". Widgets added to these sidebars will show up when viewing AMP pages. Please note that the AMP widget sidebars aren't made available for editing in the Customizer.
* Version: 0.2
* 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/8a7c84360db2aff108e4fdaccf12772e
*/
namespace Google\AMP_Reader_Theme_Widgets;
// phpcs:disable WordPress.WP.I18n.TextDomainMismatch
use AMP_Options_Manager;
add_action(
'init',
function () {
// Abort if AMP plugin is not active.
if (
! class_exists( 'AMP_Options_Manager' )
||
! function_exists( 'amp_has_paired_endpoint' )
) {
return;
}
// Abort if Reader template mode is not active.
if ( 'reader' !== AMP_Options_Manager::get_option( 'theme_support' ) ) {
return;
}
// Skip if using legacy AMP templates.
$reader_theme = AMP_Options_Manager::get_option( 'reader_theme' );
if ( 'legacy' === $reader_theme ) {
return;
}
$theme = wp_get_theme( AMP_Options_Manager::get_option( 'reader_theme' ) );
if ( $theme->errors() ) {
return;
}
// Abort if there are no known sidebars for the Reader theme.
$sidebars = get_theme_sidebars( $theme->get_stylesheet() );
if ( null === $sidebars && $theme->parent() ) {
$sidebars = get_theme_sidebars( $theme->get_template() );
}
if ( empty( $sidebars ) ) {
return;
}
add_action(
'widgets_init',
function () use ( $sidebars ) {
register_amp_sidebars( $sidebars );
}
);
},
0
);
/**
* Register sidebars.
*
* @param array $sidebars Sidebars.
*/
function register_amp_sidebars( $sidebars ) {
if ( is_admin() ) {
foreach ( $sidebars as $sidebar_id => $sidebar_name ) {
register_sidebar(
[
'id' => "amp-{$sidebar_id}",
'name' => esc_html( __( 'AMP:', 'amp' ) . ' ' . $sidebar_name ),
'description' => esc_html__( 'This sidebar is shown exclusively on your AMP pages.', 'amp' ),
]
);
}
} elseif ( amp_has_paired_endpoint() ) {
remove_filter( 'sidebars_widgets', '__return_empty_array', PHP_INT_MAX );
add_filter(
'sidebars_widgets',
function ( $stored_sidebars_widgets ) use ( $sidebars ) {
return filter_amp_sidebars_widgets( $stored_sidebars_widgets, $sidebars );
}
);
}
}
/**
* Filter sidebars widgets for AMP pages.
*
* @param array $stored_sidebars_widgets Stored sidebars widgets.
* @param array $amp_sidebars AMP sidebars.
* @return array Filtered sidebars widgets for AMP pages.
*/
function filter_amp_sidebars_widgets( $stored_sidebars_widgets, $amp_sidebars ) {
$sidebars_widgets = [];
foreach ( array_keys( $amp_sidebars ) as $sidebar_id ) {
if ( isset( $stored_sidebars_widgets[ "amp-{$sidebar_id}" ] ) ) {
$sidebars_widgets[ $sidebar_id ] = $stored_sidebars_widgets[ "amp-{$sidebar_id}" ];
}
}
return $sidebars_widgets;
}
/**
* Get sidebars for the given theme.
*
* @param string $theme Theme (stylesheet).
* @return array|null Mapping of sidebar ID to sidebar names, or null if no sidebars present.
*/
function get_theme_sidebars( $theme ) {
switch ( $theme ) {
case 'twentyeleven':
return [
'sidebar-1' => __( 'Main Sidebar', 'twentyeleven' ),
'sidebar-2' => __( 'Showcase Sidebar', 'twentyeleven' ),
'sidebar-3' => __( 'Footer Area One', 'twentyeleven' ),
'sidebar-4' => __( 'Footer Area Two', 'twentyeleven' ),
'sidebar-5' => __( 'Footer Area Three', 'twentyeleven' ),
];
case 'twentytwelve':
return [
'sidebar-1' => __( 'Main Sidebar', 'twentytwelve' ),
'sidebar-2' => __( 'First Front Page Widget Area', 'twentytwelve' ),
'sidebar-3' => __( 'Second Front Page Widget Area', 'twentytwelve' ),
];
case 'twentythirteen':
return [
'sidebar-1' => __( 'Main Widget Area', 'twentythirteen' ),
'sidebar-2' => __( 'Secondary Widget Area', 'twentythirteen' ),
];
case 'twentyfourteen':
return [
'sidebar-1' => __( 'Primary Sidebar', 'twentyfourteen' ),
'sidebar-2' => __( 'Content Sidebar', 'twentyfourteen' ),
'sidebar-3' => __( 'Footer Widget Area', 'twentyfourteen' ),
];
case 'twentyfifteen':
return [
'sidebar-1' => __( 'Widget Area', 'twentyfifteen' ),
];
case 'twentysixteen':
return [
'sidebar-1' => __( 'Sidebar', 'twentysixteen' ),
'sidebar-2' => __( 'Content Bottom 1', 'twentysixteen' ),
'sidebar-3' => __( 'Content Bottom 2', 'twentysixteen' ),
];
case 'twentyseventeen':
return [
'sidebar-1' => __( 'Blog Sidebar', 'twentyseventeen' ),
'sidebar-2' => __( 'Footer 1', 'twentyseventeen' ),
'sidebar-3' => __( 'Footer 2', 'twentyseventeen' ),
];
case 'twentynineteen':
return [
'sidebar-1' => __( 'Footer', 'twentynineteen' ),
];
case 'twentytwenty':
return [
'sidebar-1' => __( 'Footer #1', 'twentytwenty' ),
'sidebar-2' => __( 'Footer #2', 'twentytwenty' ),
];
case 'twentytwentyone':
return [
'sidebar-1' => __( 'Footer', 'twentytwentyone' ),
];
}
return null;
}
@westonruter
Copy link
Author

westonruter commented May 18, 2021

@westonruter
Copy link
Author

westonruter commented May 18, 2021

AMP-specific sidebars are shown on the Widgets admin screen prefixed by "AMP:"

image

Please note that the AMP widget sidebars aren't made available for editing in the Customizer.

@gaurav2328
Copy link

gaurav2328 commented Apr 26, 2022

@westonruter Hi, I discovered this plugin via one of the AMP plguin support threads on
Wordpress. I installed it but I don't see the AMP-specific sidebars on the widget admin screen. I am using the Twenty Twenty as the AMP reader theme. Plugin has been installed properly. Can you help?
Screenshot 2022-04-26 at 1 28 13 PM
Screenshot 2022-04-26 at 1 25 25 PM

@westonruter
Copy link
Author

westonruter commented Apr 26, 2022

@gaurav2328 This plugin was written specifically with the classic non-block widgets mind, not block-based widgets as you've shown here. You may want to try with the Classic Widgets plugin. Otherwise, the plugin would need to be revamped for supporting block-based widgets. I won't have availability to assist, but you may be able to get assistance on the support forum.

@gaurav2328
Copy link

gaurav2328 commented Apr 26, 2022

@westonruter Thanks. I will try the Classic Widgets plugin.

@gaurav2328
Copy link

gaurav2328 commented Apr 26, 2022

I can see the AMP-specific sidebars with the plugin. :)

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