• Resolved wrip

    (@wrip)


    I am trying to figure out how to get the inline css to work in AMP pages. Below is the script I’ve tried in functions.php. Note the $raw_content array that includes the inline CSS.

    add_filter('amp_post_template_data', function($data) {
            $raw_content = array(
            '<h2 class="amp-wp-title" style="background-color: '.get_field('bg_color').';">'.get_field('h2_title').'</h2>',
            get_field('content_2')
            );
            
            foreach ($raw_content as $content) {
                list($sanitized_html, $scripts, $styles) = AMP_Content_Sanitizer::sanitize(
                    $content,
                    array( 'AMP_Img_Sanitizer' => array(), 'AMP_Style_Sanitizer' => array() )
                    );
                
                $data['amp_component_scripts'] = array_merge($data['amp_component_scripts'], $scripts);
                
                $data['post_amp_styles'] = array_merge($data['post_amp_styles'], $styles);
                
                add_action('customamp', function() use ($sanitized_html) {
                    echo $sanitized_html;
                });
            }
        return $data;
    });

    In the AMP page, I get the markup for the h2 tag like this:
    <h2 class="amp-wp-title amp-wp-067014b">. It looks like the script has sanitised the inline CSS and added a new class amp-wp-067014b. However the new class isn’t added into <style amp-custom>.

    Have I done this incorrectly or is there a better way to resolve this?

    Thanks

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

    (@westonruter)

    Why don’t you output the CSS in the amp_post_template_css action instead?

    For example:

    add_action( 'amp_post_template_css', function( $post_template ) {
        printf( '.amp-wp-title { background-color: %s }', get_field('bg_color') );
    } );
    Thread Starter wrip

    (@wrip)

    Hi @westonruter

    Thanks for a quick response. Yes that seems to work. But isn’t it also supposed to work within the add_filter('amp_post_template_data'...) code in my example? I’m just trying to figure out why this wasn’t working in my code.

    I’ve also noticed that AMP_Content_Sanitizer::sanitize() function has now deprecated. Is there a replacement for this function?

    Thanks

    Plugin Author Weston Ruter

    (@westonruter)

    I recall we deprecated it in favor of calling AMP_Content_Sanitizer::sanitize_document() directly, as can be seen in the generation of the featured image:

    https://github.com/ampproject/amp-wp/blob/1bac7f7904530d534a10817e58c22117df0235fb/includes/templates/class-amp-post-template.php#L355-L364

    But I believe it’s much better to use the amp_post_template_css action here. Also, the amp_post_template_css and amp_post_template_data hooks are only relevant to the Classic mode. Going forward the recommendation is to migrate over to Paired or Native modes when possible.

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

The topic ‘How do I use inline CSS in AMP pages?’ is closed to new replies.