• Resolved JS Morisset

    (@jsmoriss)


    I have a ‘language_attributes’ filter that needs to check amp_is_request().

    Before calling amp_is_request(), I use if ( did_action( 'parse_query' ) ) like so:

    
    $ret = false;
    
    if ( function_exists( 'amp_is_request' ) ) {
    
        if ( did_action( 'parse_query' ) ) {
    
            $ret = amp_is_request();
        }
    
    }
    

    Unfortunately, I’m getting an amp_is_available was called <strong>incorrectly</strong> error:

    amp_is_available was called incorrectly. amp_is_available() (or amp_is_request(), formerly is_amp_endpoint()) was called too early and so it will not work properly. WordPress is currently doing the language_attributes hook. Calling this function before the wp action means it will not have access to WP_Query and the queried object to determine if it is an AMP response, thus neither the amp_skip_post() filter nor the AMP enabled toggle will be considered.

    If did_action( 'parse_query' ) is true, wouldn’t that mean the WP_Query has been parsed?

    js.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Weston Ruter

    (@westonruter)

    Check amp_is_request() inside the filter callback like so:

    add_filter( 'language_attributes', function ( $attributes ) {
    	if ( function_exists( 'amp_is_endpoint' ) && amp_is_endpoint() ) {
    		// Modify $attributes for AMP...
    	} else {
    		// Modify $attributes for non-AMP...
    	}
    	return $attributes;
    } );
    Plugin Author Weston Ruter

    (@westonruter)

    The parse_query action is too early still. It’s the wp action you need if you’re going to conditionally add such a filter.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘No support for ‘language_attributes’?’ is closed to new replies.