/i','',$the_content); $the_content = preg_replace('/<\/noscript>/i','',$the_content); // Delete font tag. $the_content = preg_replace('/]*?>/i','',$the_content); $the_content = preg_replace('/<\/font>/i', '', $the_content); // Delete style attribute. $the_content = preg_replace('/ *?style=["][^"]*?["]/i', '', $the_content); $the_content = preg_replace('/ *?style=[\'][^\']*?[\']/i', '', $the_content); // Delete target attribute. $the_content = preg_replace('/ *?target=["][^"]*?["]/i', '', $the_content); $the_content = preg_replace('/ *?target=[\'][^\']*?[\']/i', '', $the_content); // Delete onclick attribute. $the_content = preg_replace('/ *?onclick=["][^"]*?["]/i', '', $the_content); $the_content = preg_replace('/ *?onclick=[\'][^\']*?[\']/i', '', $the_content); // Delete onload attribute. $the_content = preg_replace('/ *?onload=["][^"]*?["]/i', '', $the_content); $the_content = preg_replace('/ *?onload=[\'][^\']*?[\']/i', '', $the_content); // Delete marginwidth attribute. $the_content = preg_replace('/ *?marginwidth=["][^"]*?["]/i', '', $the_content); $the_content = preg_replace('/ *?marginwidth=[\'][^\']*?[\']/i', '', $the_content); // Delete marginheight attribute. $the_content = preg_replace('/ *? marginheight=["][^"]*?["]/i', '', $the_content); $the_content = preg_replace('/ *? marginheight=[\'][^\']*?[\']/i', '', $the_content); // Delete font tag. $the_content = preg_replace('/]+?>/i', '', $the_content); $the_content = preg_replace('/<\/font>/i', '', $the_content); convert_amp_img_tag( $the_content ); $pattern = '/

<\/p>/i'; $append = ''; $the_content = preg_replace($pattern, $append, $the_content); $pattern = '//is'; $append = ''; $the_content = preg_replace($pattern, $append, $the_content); $pattern = '{}i'; $append = ''; $the_content = preg_replace($pattern, $append, $the_content); $pattern = '{

}i'; $append = ''; $the_content = preg_replace($pattern, $append, $the_content); return $the_content; } endif; add_filter('the_content','ampbase\convert_content_for_amp', 999999999); // Convert img tag to amp-img tag. function convert_amp_img_tag ( &$the_content ) { $res = preg_match_all('//is', $the_content, $m); if ($res) { foreach ($m[0] as $match) { $src_attr = null; $url = null; $width_attr = null; $width_value = null; $height_attr = null; $height_value = null; $alt_attr = null; $alt_value = null; $title_attr = null; $title_value = null; $sizes_attr = null; // Get src attribute. $src_res = preg_match('/src=["\']([^"\']+?)["\']/is', $match, $srcs); if ($src_res) { $src_attr = ' '.$srcs[0]; $url = $srcs[1]; } // Get width attribut. $width_res = preg_match('/width=["\']([^"\']*?)["\']/is', $match, $widths); if ($width_res) { $width_attr = ' '.$widths[0]; $width_value = $widths[1]; } // Get height attribut. $height_res = preg_match('/height=["\']([^"\']*?)["\']/is', $match, $heights); if ($height_res) { $height_attr = ' '.$heights[0]; $height_value = $heights[1]; } // Get alt attribute. $alt_res = preg_match('/alt=["]([^"]*?)["]/is', $match, $alts); if (!$alt_res) $alt_res = preg_match("/alt=[']([^']*?)[']/is", $match, $alts); if ($alt_res) { $alt_attr = ' '.$alts[0]; $alt_value = $alts[1]; } // Get title attribute. $title_res = preg_match('/title=["]([^"]*?)["]/is', $match, $titles); if (!$title_res) $title_res = preg_match("/title=[']([^']*?)[']/is", $match, $titles); if ($title_res) { $title_attr = ' '.$titles[0]; $title_value = $titles[1]; } // Get information from images without those with width and height attributes. $class_attr = null; if ($url && (empty($width_value) || empty($height_value))) { $size = get_image_width_and_height($url); if ($size) { $class_attr = ' class="internal-content-img"'; $width_value = $size['width']; $width_attr = ' width="'.$width_value.'"'; $height_value = $size['height']; $height_attr = ' height="'.$height_value.'"'; } else { $class_attr = ' class="external-content-img"'; $width_value = 300; $width_attr = ' width="300"'; $height_value = 300; $height_attr = ' height="300"'; } } // Create the size attribute (for clean responsiveness). if ($width_value) { $sizes_attr = ' sizes="(max-width: '.$width_value.'px) 100vw, '.$width_value.'px"'; } // Create amp-img tag. $tag = ''; // Convert img tag to amp-img tag. $the_content = preg_replace( '{' . preg_quote($match) . '}', $tag , $the_content, 1 ); } } // This is unnecessary, is not it? /* $the_content = preg_replace( '//is', '', $the_content ); */ } // Get template for amp. if ( !function_exists( 'get_template_part_amp' ) ): function get_template_part_amp($template_name){ ob_start(); get_template_part($template_name); $template = ob_get_clean(); $template = convert_content_for_amp($template); echo $template; } endif; // Get permalink for amp. if ( !function_exists( 'get_amp_permalink' ) ): function get_amp_permalink(){ $permalink = get_permalink(); if (strpos($permalink,'?') !== false) { $amp_permalink = $permalink.'&amp=1'; } else { $amp_permalink = $permalink.'?amp=1'; } return $amp_permalink; } endif; // Get image width and height. function get_image_width_and_height($image_url){ if (!includes_site_url($image_url)) { return false; } $wp_upload_dir = wp_upload_dir(); $uploads_dir = $wp_upload_dir['basedir']; $uploads_url = $wp_upload_dir['baseurl']; $image_file = str_replace($uploads_url, $uploads_dir, $image_url); $imagesize = getimagesize($image_file); if ($imagesize) { $res = array(); $res['width'] = $imagesize[0]; $res['height'] = $imagesize[1]; return $res; } }