• Hi guys,

    I want to check if the user is viewing an AMP-Page or not, because another plugins functions are depending on it.

    For the AMP-Plugin by automattic you could use

    if (function_exists( ‘is_amp_endpoint’ ) && is_amp_endpoint()) {
    // do stuff
    }

    Is there a similar way for the AMPforWP Plugin?

    Thank you!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor ampforwp

    (@ampforwp)

    Hi @seriou5,

    Yes, we have a similar way to check. You can use ‘ampforwp_is_amp_endpoint’ function.

    Like this:

    if (function_exists( ‘ampforwp_is_amp_endpoint’ ) && ampforwp_is_amp_endpoint()) {
    // do stuff
    }

    Hope it helps.

    Thread Starter seriou5

    (@seriou5)

    Thank you!

    Nearly there…

    The 3rd party plugin (wp-worthy) is adding an amp-pixel tag. It’s working fine for the automattic amp-plugin but unfortunately not yet for the AMPforWP plugin, which we are using.

    I found out that the plugin is checking for the main-query and only appends the pixel if the main query is used. It is checking this with “is_main_query()” and I assume that AMPforWP is using a custom or secondary query.

    Can you point me in the right direction?

    Thank you!

    • This reply was modified 5 years, 9 months ago by seriou5.
    Plugin Contributor ampforwp

    (@ampforwp)

    Hi @seriou5,

    Can you please let me know which hook or filter is using the 3rd party plugin (wp-worthy) to add an amp-pixel tag?

    so that I can tell you which hook or filter to be used for AMPforWP plugin.

    Thank you

    Thread Starter seriou5

    (@seriou5)

    Hi @ampforwp,

    I will try!

    wp-worthy uses the ‘the_content’ hook to filter the content and add a pixel to the output:

    add_filter ('the_content', array ($this, 'onContent'));

    Inside the ‘onContent’ function another function (‘addMarker’) is triggered only IF the main query is used. It is checked with:

    if (!$this->onMainQuery)
         return $content;

    When using AMPforWP this condition is not passed (= we are not on the main query) and so the “addMarker” function inside the “onContent” function is never triggered.

    As soon as I delete the above mentioned condition the pixel is added successfully.

    If you want I can also provide you with the relevant file (or you download wp-worthy on your own and check the onContent function starting on line 466 inside the main plugin file).

    Thank you

    Plugin Contributor ampforwp

    (@ampforwp)

    Hi @seriou5,

    It seems, automattic amp-plugin was using the is_main_query() function for the static front page. And are you trying to add the amp-pixel for the static front page? If yes, to check the front page in AMPforWP, you can use ampforwp_is_front_page function.

    Hope it helps.

    Thread Starter seriou5

    (@seriou5)

    Hi @ampforwp

    no, the wp-worthy plugin is adding pixels only to posts.

    Is there a similar check for posts too?

    Plugin Contributor ampforwp

    (@ampforwp)

    Hi @seriou5,

    Right now, for the single post, we don’t have a similar check. But the alternative solution you can do is, we can customize the code for the sigle post only. For testing purpose please copy and paste the below code in the theme’s functions.php file and check whether the solution is working or not.

    
    add_action('ampforwp_body_beginning','ampforwp_custom_amp_pixel');
    function ampforwp_custom_amp_pixel(){
    	if(is_single( )){
    		add_filter ('the_content', 'ampforwp_with_worthy_onContent');
    		add_filter ('the_content_export', 'ampforwp_with_worthy_onContent');
    	}
    }
     
    function ampforwp_with_worthy_onContent ($content) {
          // Check if there is an export running
           if ((current_filter () == 'the_content_export') && ($GLOBALS ['wp_the_query']->post))
            return $this->markerAdd ($content, $GLOBALS ['wp_the_query']->post, 'export');
          
         /* // Skip this function if we are not on the main-query
          if (!$this->onMainQuery)
            return $content;*/
          
          // Only output markers on single pages
          if (!($GLOBALS ['wp_the_query']->is_single || $GLOBALS ['wp_the_query']->is_page))
            return $content;
          
          // Try to append marker to content
          return $this->markerAdd ($content, $GLOBALS ['wp_the_query']->post, null);
        }
    

    Hope it helps.

    Thank you

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Check AMP state of post’ is closed to new replies.