WordPress AMP Automattic Plugin Enable for Custom Post Types(CPT)


wordpress_automatic_amp_earlIf you are on the band wagon to enable AMP on your WordPress website and are using the AMP Plugin by Automattic, you may realize that it does not work for custom post types right out of the gate. For this you need to make a small tweak by adding some modification to your theme or plugin code.

In the example below lets assume that your CPT is registered as ‘resources’ which we use as the first parameter of the function add_post_type_support() and you can change it to what ever your actual CPT is, duplicate that line with the new CPT names if you have multiple CPT’s. ‘cpt_enable_amp’ is passed to add_action() is just a function name and you can name it what ever you want as long as the function name below it is the same name and there is no other function named the same.

//If on a custom theme code would go in functions.php of the theme
add_action( 'init', 'te_cpt_enable_amp', 20 );

function te_cpt_enable_amp() {
    if ( ! defined( 'AMP_QUERY_VAR' ) ) {
        return; // do not add support if AMP plugin is not detected
    add_post_type_support( 'resources', AMP_QUERY_VAR );
    add_post_type_support( 'news', AMP_QUERY_VAR ); // if you have another CPT called news

Once you are done with this, simply go to your front page URL for the post and add /amp or /?amp=1 the end of your URL. If you already have other parameters you may need to add &amp=1 and append it to the rest of the query string.




Please enter your comment!
Please enter your name here