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";
}
]]>The problem:
Somehow this meta tag is added within the <head>-section:
<meta name=”robots” content=”noindex, nofollow”>
I appreciate your support to fix this problem, thanks.
]]>This makes the styling of the pages poor. This issue does not occur on the other pages. The stray style tag is added just after the title
]]>I did not made any change in the hook function that I use previously. Few codes are present under the head, few are not.
add_action( 'wp_head', 'tu_add_head' );
function tu_add_head() {
?>
---- Code ----
>
]]>The cause seems to be the code add_action('wp_head', 'spice_fse_script_header');
in functions.php, which inserts a <div> inside <head>, forcing the start of the <body> element and skipping any subsequent code in <head> (including favicon links).
I believe this can be fixed just by replacing wp_head
with wp_body_open
in the above line of code.
get_header() function doesn't inject the
header.phpcode, while
get_footer()` works fine.header.php
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<?php
wp_head();
?>
</head>
<body>
<header>
header
</header>
front-page.php
<?php
get_header();
?>
front page content
<?php
get_footer();
?>
footer.php
<?php
wp_footer();
?>
<footer>
footer
</footer>
</body>
</html>
The odd part is that when creating a new header-a.php
file , with the exact same code as header.php
I can load it using get_header('a')
and it works fine;
Any idea why it happens and how to fix it?
Thanks.
I would like to use several wp_head custom hooks for the purpose of inserting one code above another code in the head.
To do this, I set the custom hooks as follows (hook name —> action —> priority):
Header —> wp_head —> 1
Header New —> wp_head —> 2
As you can see in the screenshot, both hooks are enabled.
https://i.ibb.co/S7nC7BK/hooks-01.png
Thus only one hook named Header is used in the block settings.
The second hook named Header New is not used neither in this block nor in any other.
https://i.ibb.co/nc3HhZD/hooks-02.png
And now begins the most interesting part.
If we look at the source code of the page, the code of the block, in which the hook named Header is used, starts to be doubled, as if this block also used the hook named Header New.
If I uncheck the option with hook named Header New then everything is fine.
https://i.ibb.co/Jcp0S53/hooks-03.png
Question: Why is it duplicated if I am not actually using a hook named Header New, but only pointing out that it can be used? Is this the way it should be?
In other words, should a plugin call a custom hook that is not in use?
]]>add_action( ‘wp_head’, ‘add_custom_meta_des’, 4 );
Thank you
]]>