Preg replace in content | WordPress.org

[ad_1]

hi, I tried to fix a little bug in a custom plugin:

this snippet is for search if pattern exist, then if exist make a replace and add css code in header.

seems to work but load css in all page instead of in the page where pattern exist.

any help?

i used gist_embed_add_styles() instead oh enqueue style because this work also in plugin settings page (backend).

thanks in advance.

<?php
// Deny access if called directly
defined("ABSPATH") || die("Access denied.");

// Apply customization to the content for GitHub Gist embeds.
add_filter("the_content", "gist_embed_apply_customization");
function gist_embed_apply_customization($content)
{
// Check if the content is a singular post or page and contains HTML or Shortcode block
if ((is_singular("post") || is_singular("page")) && (has_block("core/html") || has_block("core/shortcode"))) {
// Patterns to match GitHub Gist URLs in HTML and Shortcode blocks
$patterns = [
'/\[https:\/\/gist\.github\.com\/([a-zA-Z0-9]+)\/([a-zA-Z0-9]+)(\?file=([a-zA-Z0-9.]+))?]/i', // Pattern for shortcode without .js
'/<script src="https:\/\/gist\.github\.com\/([a-zA-Z0-9]+)\/([a-zA-Z0-9]+)\.js(\?file=([a-zA-Z0-9.]+))?"><\/script>/i', // Pattern for HTML with .js
];

// Check if the content has any script tags with matching URLs
$has_matches = false;
foreach ($patterns as $pattern) {
if (preg_match_all($pattern, $content, $matches, PREG_SET_ORDER)) {
$has_matches = true;
foreach ($matches as $match) {
// Append the button HTML after the script tag
$fileparam = !empty($match[3]) ? $match[3] : ''; //suppor ?file=filename
$gist_url = "https://gist.github.com/" . $match[1] . "/" . $match[2] . "";
$add_scriptag_and_htmlbutton = "
<script src="" . $gist_url .".js" . $fileparam . "" data-no-optimize="1"></script>
<div class="gist-download-button">
<a href="" . $gist_url ."/" title="View Gist" class="print-yes" style="display:none">GoTo: " . $gist_url ."</a>
<a href="" . $gist_url ."/download" class="print-no">Download Gist ❤️</a>
</div>
<!--<br style="clear:both" />-->\n";

// Replace the original shortcode or script tag with the modified content
$content = preg_replace("/" . preg_quote($match[0], '/') . "/", $add_scriptag_and_htmlbutton, $content, 1);
}
}
}

// Add custom styles to the wp_head if there are one or more Gist embeds
if ($has_matches) { add_action("wp_head", "gist_embed_add_styles"); }
}

// Return updated content
return $content;
}

or this simplified release

<?php
// Deny access if called directly
defined('ABSPATH') || exit;

// Apply customization to the content for GitHub Gist embeds.
add_filter("the_content", "gist_embed_apply_customization");

function gist_embed_apply_customization($content) {
// Patterns to match GitHub Gist URLs in HTML and Shortcode blocks
$patterns = [
'/\[https:\/\/gist\.github\.com\/([a-zA-Z0-9]+)\/([a-zA-Z0-9]+)(\?file=([a-zA-Z0-9.]+))?]/i', // Pattern for shortcode without .js
'/<script src="https:\/\/gist\.github\.com\/([a-zA-Z0-9]+)\/([a-zA-Z0-9]+)\.js(\?file=([a-zA-Z0-9.]+))?"><\/script>/i', // Pattern for HTML with .js
];

$gist_found = false;

// Check if any pattern matches
foreach ($patterns as $pattern) {
if (preg_match($pattern, $content)) {
$gist_found = true;
break; // Stop checking patterns once a match is found
}
}

// If no GitHub Gist patterns were found, return content as is
if (!$gist_found) {
return $content;
}

// Start output buffering

// Replace GitHub Gist embeds with custom HTML
$content = preg_replace_callback($patterns, 'replace_gist_embed', $content);

// Get the buffer content and clean the buffer

// Add styles to wp_head if GitHub Gist was found
if ($gist_found) {
add_action("wp_head", "gist_embed_add_styles");
}

// Return the modified content
return $content;
}

function replace_gist_embed($matches) {
$fileparam = !empty($matches[3]) ? $matches[3] : ''; // support ?file=filename
$gist_url = "https://gist.github.com/" . $matches[1] . "/" . $matches[2];
return "
<script src="" . $gist_url . ".js" . $fileparam . "" data-no-optimize="1"></script>
<div class="gist-download-button">
<a href="" . $gist_url . "/" title="View Gist" class="print-yes" style="display:none">GoTo: " . $gist_url . "</a>
<a href="" . $gist_url . "/download" class="print-no">Download Gist ❤️</a>
</div>\n";
}

// Function to add styles for Gist Embed.
function gist_embed_add_styles()
{
// Output selected CSS file
$selected_css_file = get_option('gist_embed_selected_css');
if (!empty($selected_css_file)) {
echo "\n<!-- Gist Embed Theme Start -->\n<link rel="stylesheet" href="" . GIST_PLUGIN_URL . "Assets/CSS/StyleSheets/" . $selected_css_file . "" data-no-optimize="1" />\n<!-- Gist Embed Theme End -->";
}

// Output custom CSS with Cache burst to ensure the latest version is loaded
echo "\n<!-- Gist Embed CustomCSS Start -->\n<link rel="stylesheet" href="" . GIST_PLUGIN_URL . "Assets/CSS/GistCustom.css?" . filemtime(GIST_DIR_PATH . "Assets/CSS/GistCustom.css") . "" data-no-optimize="1" />\n<!-- Gist Embed CustomCSS End -->\n";
}

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer