-
Notifications
You must be signed in to change notification settings - Fork 384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow AMP to be disabled by default for individual Posts #837
Comments
The ability to opt-out by default did cross my mind, but it seemed counter to the purpose of this plugin, namely that if you add the AMP plugin the goal should be AMPlification of everything by default as much as possible. Aside: The PR originally stored a binary toggle in the postmeta before switching to what was merged, a flag to just disable AMP: 4f58949 |
That makes sense. I'm finding it to be troublesome currently though using I am running a bit of code running on Right now I'm hooking into |
Good point on the page templates. Initially we were going to turn off AMP support for pages when a custom page template was used. But we decided to instead just add a flag for all posts to give granular control. Nevertheless, that makes it problematic when you have a lot of existing pages that already are assigned to custom page templates, where these page templates are required for the page to make any sense at all (and so must be excluded from AMP). If the plugin were to switch back to storing a add_filter( 'get_post_metadata', function( $value, $object_id, $meta_key ) {
static $recursing;
if ( $recursing || 'amp_status' !== $meta_key || 'page' !== get_post_type( $object_id ) ) {
return $value;
}
$override = null;
$recursing = true;
$status = get_post_meta( $object_id, $meta_key, true );
if ( empty( $status ) ) {
$override = 'disabled';
}
$recursing = false;
return $override;
}, 10, 3 ); It's a shame that the This is hacky, so maybe there should be a better way to set the default enabled/disabled state for a given post type. For pages, the default state could be disabled when there is a custom page template assigned. This default could then be overridden with a filter, which is passed the post ID being edited. |
@d4mation I've chatted with @ThierryA about what to do about this and the related issue of #867. Here's what we're suggesting: Instead of always having a default enabled status for posts and pages, the initial enabled status can be variable. For pages with custom templates assigned or for pages assigned to the front page or posts page, the default status will be disabled. For all others, the default status will be enabled. For example: $default_enabled = ! (bool) get_page_template_slug( $post ); // Because CPTs can have page templates too.
if ( $default_enabled && 'page' === $post->post_type ) {
$default_enabled = (
! (bool) get_page_template_slug( $post )
&&
(
'page' !== get_option( 'show_on_front' )
||
! in_array( (int) $post->ID, array( (int) get_option( 'page_on_front' ), (int) get_option( 'page_for_posts' ) ), true )
)
);
} The default status would then be passed into a $default_enabled = apply_filters( 'amp_post_default_enabled', $default_enabled, $post ); Which would then allow a site to override whether a given post should be enabled by default. For example, if all pages with custom templates and the front page should actually be enabled by default (but not the page for posts), then following filter could be used: add_filter( 'amp_post_default_enabled', function( $default_enabled, $post ) {
if ( 'page' === $post->post_type && get_page_template_slug( $post ) && (int) get_option( 'page_for_posts' ) !== $post->ID ) {
$default_enabled = true;
}
return $default_enabled;
}, 10, 2 ); Additionally, as mentioned above, the plugin should to switch back to storing a Lastly, the By making these changes, an upgrade to 0.6 should ensure that only generic pages automatically get AMP support. The homepage, page for posts, and pages with custom templates selected will require opt-in by default. Any further changes to the default behaviors can be controlled via the How do these changes sound to you? |
I'll give it a test later today! Reading your comment above though, the |
Ah, shoot, my bad. Wrong Issue 🤐 Had a few GitHub tabs open at once. |
@westonruter It overall works amazingly! The However, when set to Default Disable AMP for all content using that Filter, I noticed a minor UI bug. With the old implementation, when selecting "Disabled" it would update the text to enforce to the User that the status is going to be changed. This seems not work in cases where the Default is "Disabled". It works fine if the Default is "Enabled". |
Yes, I noticed that as well. I believe I've fixed that in 8c51443 Could you test the latest commit on the PR? |
That fixed it! 🎉 |
Thank you very much for testing. 🤝 |
#813 allowed AMP to be disabled Per-Post which is excellent, but it seems that there's no reliable way to invert the assumption that all Posts for each enabled Post Type should load AMP by default.
The way the Post Meta currently works only stores data if AMP is disabled, so attempting to Filter that output using the
get_{$meta_type}_metadata
Filter would not be reliable as you cannot differentiate between a Post that has not been edited since the AMP plugin was activated and a Post that has AMP enabled (As not checking "Disabled" just deletes the Post Meta, which for all intents and purposes looks the same as a Post that has not been edited since enabling the plugin).The
amp_skip_post
Filter exists, but without knowing whether the Post has AMP intentionally enabled for the Post or not it is not much help here.The text was updated successfully, but these errors were encountered: