Skip to content

Instantly share code, notes, and snippets.

@amirhp-com
Last active November 10, 2024 23:57
Show Gist options
  • Save amirhp-com/b622a6e9a0ce1db5d66459af8c0d271f to your computer and use it in GitHub Desktop.
Save amirhp-com/b622a6e9a0ce1db5d66459af8c0d271f to your computer and use it in GitHub Desktop.
Fixing SEO Issues in the Woodmart Theme by Converting H3 Tags to P Tags
<?php
/*
:: Fixing SEO Issues in the Woodmart Theme by Converting H3 to P
>> https://www.linkedin.com/posts/activity-7254435929468100608-t3gk
-----------------------------------------------------------------
If you’re using the Woodmart theme and notice h3 tags being used
in places where they shouldn’t be (like regular content sections),
it can hurt your SEO. Here’s a quick PHP solution to dynamically
convert those h3 tags with the class wd-entities-title into p tags,
preserving the structure and content but helping your SEO at the _
same time! A Snippet by Amirhp.Com -- at 1403-08-01. keep sharing.
*/
add_action("template_redirect", function() {ob_start("amirhp__modify_output_before_sent");});
add_action("shutdown", function() {ob_end_flush();});
/* method 1: using preg_replace, most compatible */
function amirhp__modify_output_before_sent($content) {
// Use regex to find h3 elements with the class wd-entities-title
$pattern = '/<h3([^>]*class=["\']wd-entities-title["\'][^>]*)>(.*?)<\/h3>/is';
// Replace them with <p> tags, keeping the content and attributes
$content = preg_replace_callback($pattern, function($m) {return '<p'.$m[1].'>'.$m[2].'</p>';}, $content);
return $content;
}
<?php
/*
:: Fixing SEO Issues in the Woodmart Theme by Converting H3 to P
>> https://www.linkedin.com/posts/activity-7254435929468100608-t3gk
-----------------------------------------------------------------
If you’re using the Woodmart theme and notice h3 tags being used
in places where they shouldn’t be (like regular content sections),
it can hurt your SEO. Here’s a quick PHP solution to dynamically
convert those h3 tags with the class wd-entities-title into p tags,
preserving the structure and content but helping your SEO at the _
same time! A Snippet by Amirhp.Com -- at 1403-08-01. keep sharing.
*/
add_action("template_redirect", function() {ob_start("amirhp__modify_output_before_sent");});
add_action("shutdown", function() {ob_end_flush();});
/* method 2: using DOMDocument, might have problem with Elementor and Persian Lang */
function amirhp__modify_output_before_sent($content) {
$dom = new DOMDocument();
libxml_use_internal_errors(true);
@$dom->loadHTML($content);
$xpath = new DOMXPath($dom);
$h3_nodes = $xpath->query('//h3[contains(@class, "wd-entities-title")]');
foreach ($h3_nodes as $h3) {
$p = $dom->createElement('p');
foreach ($h3->attributes as $attr) {
$p->setAttribute($attr->nodeName, $attr->nodeValue);
}
while ($h3->firstChild) {
$p->appendChild($h3->firstChild);
}
$h3->parentNode->replaceChild($p, $h3);
}
return "<!DOCTYPE html>\n" . $dom->saveHTML($dom->documentElement);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment