I have a homegrown Wordpress plugin I've inherited that spits out a section of code and enqueues a stylesheet depending on what the user specifies in their settings. It was working fine a up until a few days ago and now the style sheet is still getting enqueue and loaded fine, but the html ($identityWrapper and $footerLogo) are not being loaded on the page. This tells me it's not a permissions error on the server since the script is doing something. I've redacted a some identifying information and I've also left out the settings part as that all seems to be working fine.
I know this is a lot of code, but I didn't want to leave a portion that may be important out. I've spent a good amount of time trying to figure this out myself and found out that EOS is "end of string", but I haven't seen any examples of using that in any other Wordpress plugins... I think that might be the problem.
<?php
/**
* @package Branding Bar
* @version 1.0
*/
/*
Plugin Name: [Redacted]
Description: Uses output buffering to insert the branding bar after the body tag opens.
Version: 1.0
*/
add_action('wp_enqueue_scripts', array('BrandingBar', 'enqueue_stylesheet'), 10, 1);
add_action('wp_head', array('BrandingBar', 'echo_styles'), 1000, 1);
add_action('wp_footer', array('BrandingBar', 'add_real_logo'), 1000, 1);
// Start output buffering in wp_head and try to flush ASAP. It will be
// flushed when the request ends if, for some strange reason, no further
// actions are called.
add_action('wp_head', array('BrandingBar', 'start_output_buffering'), 10, 1);
add_action('get_search_form', array('BrandingBar', 'end_output_buffering'), 10, 1);
add_action('loop_start', array('BrandingBar', 'end_output_buffering'), 10, 1);
add_action('get_sidebar', array('BrandingBar', 'end_output_buffering'), 10, 1);
add_action('dynamic_sidebar', array('BrandingBar', 'end_output_buffering'), 10, 1);
add_action('wp_meta', array('BrandingBar', 'end_output_buffering'), 10, 1);
add_action('wp_footer', array('BrandingBar', 'end_output_buffering'), 10, 1);
class BrandingBar
{
private static $styles = array(
"body" => "background-position-y:60px",
"#footerLogo h1 a" => "background-size: 280px 30px",
"#footerLogo h2 a" => "background-size: 280px 15px",
);
public static $identityWrapper =<<<EOS
<div id="IdentityWrapper">
<header id="Identity">
<hgroup>
<h1>
<a target="_blank" href="#">[Redacted]</a>
</h1>
<h2>
<a target="_blank" href="#">[Redacted]</a>
</h2>
</hgroup>
</header>
</div>
EOS;
public static $footerLogo =<<<EOS
<div id="footerLogo">
<hgroup>
<h1>
<a target="_blank" href="#">[Redacted]</a>
</h1>
<h2>
<a target="_blank" href="#">[Redacted]</a>
</h2>
</hgroup>
</div>
EOS;
public function enqueue_stylesheet()
{
$color = get_option('branding_bar_color', 'black');
$format = get_option('branding_bar_format', 'responsive');
wp_enqueue_style('BrandingCss', plugins_url("widgets/branding/$color/$format/css/branding-main-2.0.css", __FILE__));
}
public function start_output_buffering()
{
ob_start(array(self, 'insert_branding_bar'));
}
public function insert_branding_bar($buffer)
{
return (preg_replace('/(<body[^>]+>)/', "$1
".self::$identityWrapper, $buffer));
}
function end_output_buffering()
{
$status = ob_get_status();
if ($status['name'] === 'self::insert_branding_bar') {
ob_end_flush();
}
}
function echo_styles()
{
echo "<style>
";
foreach (self::$styles as $selector => $declaration) {
printf("%s{%s}
", $selector, $declaration);
}
echo "@media screen and (max-width: 767px) {";
echo<<<EOS
#Identity h1 a { background-size: 99px 35px !important; }
EOS;
echo "</style>
";
}
public function add_real_logo()
{
if ('responsive' === get_option('BrandingBarFormat', 'responsive')) {
echo self::$footerLogo;
}
}
}
The syntax you're seeing:
$var =<<<EOS OR echo <<<EOS
...
EOS;
is php's heredoc syntax for strings.
It's a nice way to embed a large, string in your code, but it requires a very specific syntax to work.
The terminator (EOS;
in your case), can have no leading or trailing whitespace. So, make sure the E
is in column 1 of your editor, and make sure that there is no whitespace between the ;
and the end-of-line.
If your editor has a "show control nonprinting characters" option, turn this on. It helps with debugging heredocs. A good syntax highlighter should catch an invalid terminator and show the rest of your code as part of the string.
It appears that some code you haven't provided is actually causing the output by calling these functions (enqueue_stylesheet, start_output_buffering, end_output_buffering). So it seems that that code is doing something different for some reason we can't guess by looking at this.