duan5801 2015-02-04 19:48
浏览 33
已采纳

Wordpress插件不输出HTML

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;
    }
}
}
  • 写回答

2条回答 默认 最新

  • duanpo6079 2015-02-04 20:02
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站