Skip to content

Commit d80825d

Browse files
committed
removed json schema and head section contents from feed and putting before the shortcode feed
1 parent 4a94c93 commit d80825d

3 files changed

Lines changed: 68 additions & 4 deletions

File tree

includes/class-feed-shortcode.php

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,50 @@ public function register() {
1818
add_shortcode('opio_feed', array($this, 'init'));
1919
}
2020

21+
/**
22+
* Extract JSON-LD schema from HTML head section and remove it from head
23+
*/
24+
private function extract_jsonld_schema(&$html) {
25+
// Extract the JSON-LD schema script from head
26+
if (preg_match('/<head[^>]*>.*?<script[^>]*id=["\']jsonldSchema["\'][^>]*type=["\']application\/ld\+json["\'][^>]*>(.*?)<\/script>.*?<\/head>/is', $html, $matches)) {
27+
// Remove JSON-LD schema from head
28+
$html = preg_replace('/<script[^>]*id=["\']jsonldSchema["\'][^>]*type=["\']application\/ld\+json["\'][^>]*>.*?<\/script>/is', '', $html);
29+
return '<script id="jsonldSchema" type="application/ld+json">' . trim($matches[1]) . '</script>';
30+
}
31+
return '';
32+
}
33+
34+
/**
35+
* Extract all link and script tags from head section (everything except JSON-LD schema)
36+
*/
37+
private function extract_head_resources($html) {
38+
$resources = '';
39+
// Extract head content
40+
if (preg_match('/<head[^>]*>(.*?)<\/head>/is', $html, $head_matches)) {
41+
$head_content = $head_matches[1];
42+
43+
// Extract all link tags
44+
if (preg_match_all('/<link[^>]*>/i', $head_content, $link_matches)) {
45+
$resources .= implode("\n", $link_matches[0]) . "\n";
46+
}
47+
48+
// Extract all script tags with src attribute (external scripts)
49+
if (preg_match_all('/<script[^>]*src=["\'][^"\']+["\'][^>]*><\/script>/i', $head_content, $script_matches)) {
50+
$resources .= implode("\n", $script_matches[0]);
51+
}
52+
}
53+
return trim($resources);
54+
}
55+
56+
/**
57+
* Remove head section from HTML, keeping only body content
58+
*/
59+
private function remove_head_section($html) {
60+
// Remove the entire head section
61+
$html = preg_replace('/<head[^>]*>.*?<\/head>/is', '', $html);
62+
return $html;
63+
}
64+
2165
public function init($atts) {
2266
if (get_option('opio_active') === '0') {
2367
return '';
@@ -57,7 +101,27 @@ public function init($atts) {
57101
$opio_handler = new Opio_Handler($biz_id, $option, $review_type, $org_id);
58102
$reviews = $opio_handler->get_business();
59103

60-
// Wrap output with Nitropack exclusion wrapper
104+
// Extract JSON-LD schema from head and remove it (schema must come first for SEO)
105+
$jsonld_schema = $this->extract_jsonld_schema($reviews);
106+
107+
// Extract remaining head resources (links and scripts)
108+
$head_resources = $this->extract_head_resources($reviews);
109+
110+
// Remove head section from the HTML (can't have <head> tag in body)
111+
$reviews = $this->remove_head_section($reviews);
112+
113+
// Output in correct order:
114+
// 1. JSON-LD schema first (for Google crawler/SEO)
115+
if (!empty($jsonld_schema)) {
116+
echo $jsonld_schema;
117+
}
118+
119+
// 2. Head resources (links and scripts)
120+
if (!empty($head_resources)) {
121+
echo $head_resources;
122+
}
123+
124+
// Wrap body content with Nitropack exclusion wrapper
61125
echo '<div data-nitro-exclude="all" data-nitro-ignore="true" data-nitro-no-optimize="true" data-nitro-preserve-ws="true">';
62126

63127
if($option == "allReviewFeeds" && $review_type == "singles") {

opio.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Plugin Name: Widget for OPIO Reviews
44
Plugin URI:
55
Description: Instantly add OPIO Reviews on your website to increase user confidence and SEO.
6-
Version: 1.0.94
6+
Version: 1.0.95
77
Author: Dhiraj Timalsina <dhiraj@n49.com>
88
Text Domain: widget-for-opio-reviews
99
Domain Path: /languages
@@ -22,7 +22,7 @@
2222

2323
require(ABSPATH . 'wp-includes/version.php');
2424

25-
define('OPIO_PLUGIN_VERSION' , '1.0.94');
25+
define('OPIO_PLUGIN_VERSION' , '1.0.95');
2626
define('OPIO_PLUGIN_FILE' , __FILE__);
2727
define('OPIO_PLUGIN_URL' , plugins_url(basename(plugin_dir_path(__FILE__ )), basename(__FILE__)));
2828
define('OPIO_ASSETS_URL' , OPIO_PLUGIN_URL . '/assets/');

readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Author: Dhiraj Timalsina
33
Tags: Widget for OPIO Reviews, opio, reviews, rating, widget, google business, testimonials
44
Tested up to: 6.4
5-
Stable tag: 1.0.94
5+
Stable tag: 1.0.95
66
License: GPLv2 or later
77
License URI: http://www.gnu.org/licenses/gpl-2.0.html
88

0 commit comments

Comments
 (0)