Skip to content

Custom Post Type Support

Weston Ruter edited this page Apr 10, 2018 · 5 revisions

By default, the plugin only creates AMP content for posts and (as of 0.6) pages. Also as of 0.6, you can enable AMP support for other post types via the AMP settings admin screen. Just enable the checkbox with each post type which is able to be served as AMP.

You can also add support for other post types by adding add_post_type_support() calls in PHP for the desired post types. For example, assuming our post type is xyz-review you can add AMP support by adding the following to a theme or plugin:

add_action( 'amp_init', 'xyz_amp_add_review_cpt' );
function xyz_amp_add_review_cpt() {
	add_post_type_support( 'xyz-review', amp_get_slug() );
}

(Note: Previous to v0.7 this example used AMP_QUERY_VAR instead of amp_get_slug(). Use of this constant and amp_query_var filter is now discouraged and overriding the amp slug it may be deprecated in the future.)

You'll need to flush your rewrite rules after this. You can do this by accessing the Permalinks admin screen or via running wp rewrite flush in WP-CLI.

If you want to add post type support but have AMP be disabled by default, you can use the following plugin code:

add_filter( 'amp_post_status_default_enabled', function( $default, $post ) {
	if ( 'xyz-review' === $post->post_type ) {
		$default = false;
	}
	return $default;
}, 10, 2 );

If you want a custom template for your post type:

add_filter( 'amp_post_template_file', 'xyz_amp_set_review_template', 10, 3 );

function xyz_amp_set_review_template( $file, $type, $post ) {
	if ( 'single' === $type && 'xyz-review' === $post->post_type ) {
		$file = dirname( __FILE__ ) . '/templates/my-amp-review-template.php';
	}
	return $file;
}

We may provide better ways to handle this in the future.