尝试通过快捷代码添加邮件头时已发送的邮件头

时间:2014-10-21 作者:Elian ten Holder

在向我的一个插件添加功能时,我遇到了一个headers ready send问题。我希望在一些检查之后发送我的301重定向标题,以确定页面内容是否过时。我有一个wordpress页面,其中只包含短代码[短代码]。然后在我的插件中,我钩住init,使用以下代码片段注册我的短代码

function __construct()
{
    //register an activation hook for the plugin
    register_activation_hook(__FILE__, array(
        &$this,
        \'install_bnw\'
    ));
    //Hook up to the init action
    add_action(\'init\', array(
        &$this,
        \'start_session\'
    ), 1);
    add_action(\'init\', array(
        &$this,
        \'class_loader\'
    ));
    add_action(\'init\', array(
        &$this,
        \'init_bnw\'
    ));
    add_filter(\'wpseo_canonical\', array(
        &$this,
        \'wpseo_canonical\'
    ));
}


/**
 * Runs when the plugin is activated
 */
function install_bnw()
// TODO: Add check if the *** plugin is currently activated and if it can be reached from this plugin.
{

    // do not generate any output here
}

/**
 * Starts a session before any output is generated.
 * TODO: Might not be the most efficient way to handle sessions, needs improvement.
 */
public function start_session()
{
    if (!session_id()) {
        session_start();
    }

}

/**
 * Runs when the plugin is initialized.
 * Adds filters to wordpress functions and calls functions that need to be executed.
 */
function init_bnw()
{     // Adds all the shortcodes that need to be added.
    $this->add_shortcodes();
  // -- Some more code -- 
}

/**
 * Autoloader that uses template_autoloader to load class files from a predetermined location.
 */
function class_loader()
{
    // register an autoloader function for template classes
    spl_autoload_register(array(
        &$this,
        \'template_autoloader\'
    ));
}


/**
 * Used by the class_loader function to load classes from files but only if the files exist to ensure this plugin works with other plugins.
 * @param $class
 * @return bool that tells if the include worked.
 * @throws Exception
 *
 */
function template_autoloader($class)
{
    $classPath = "includes/$class.php";
    // Checks if the file exists for compatibility with other plugins using the spl_autoload functionality.
    if (file_exists(__DIR__ . \'/\' . $classPath)) {
        include_once $classPath;
        return true;
    } else {
        return false;
    }

}



/**
     * Contains all the shortcodes that need to be added to wordpress in order for this plugin to function.
     */
    private function add_shortcodes()
    {
       // Add all the shortcodes that need to be added here.
     // --Some shortcodes here --
    add_shortcode(\'shortcode\', array(
            &$this,
            \'shortcodeDesinationFunction\'
        ));
       // -- More shortcodes --        
    }
函数shortcodeDestinationFunction使用空构造函数加载一个类,然后调用以下函数:

function showTraining()
{
    // -- Do some data getting and business logic here but no outputting --
    // If training is in the past
    if($startdate < $currentdate)
    {
// -- Some more data getting function that don\'t output anthing
        $newLocation = "/text/text/$subjectLabel/$trainingLevel";
        // Disable caching for testing
         header(\'Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0\');
         header(\'Expires: Sat, 26 Jul 1997 05:00:00 GMT\'); // past date to encourage expiring immediately
        header (\'HTTP/1.1 301 Moved Permanently\');
        header (\'Location:\'.$newLocation); 
    }
 // Some more code
如您所见,我希望有此短代码的页面能够在特定条件适用时发送301重定向。

当我运行代码时,我得到以下错误(来自blackbox wordpress插件):

Warning Cannot modify header information - headers already sent by (output started at /var/www/vhosts/bndev.int.bijlesnetwerk.nl/httpdocs/wp-includes/class.wp-styles.php:122) on line 183 in file /var/www/vhosts/bndev.int.bijlesnetwerk.nl/httpdocs/wp-content/plugins/BNWPweb/includes/examenTraining.php
Warning Cannot modify header information - headers already sent by (output started at /var/www/vhosts/bndev.int.bijlesnetwerk.nl/httpdocs/wp-includes/class.wp-styles.php:122) on line 184 in file /var/www/vhosts/bndev.int.bijlesnetwerk.nl/httpdocs/wp-content/plugins/BNWPweb/includes/examenTraining.php
Warning Cannot modify header information - headers already sent by (output started at /var/www/vhosts/bndev.int.bijlesnetwerk.nl/httpdocs/wp-includes/class.wp-styles.php:122) on line 186 in file /var/www/vhosts/bndev.int.bijlesnetwerk.nl/httpdocs/wp-content/plugins/BNWPweb/includes/examenTraining.php
Warning Cannot modify header information - headers already sent by (output started at /var/www/vhosts/bndev.int.bijlesnetwerk.nl/httpdocs/wp-includes/class.wp-styles.php:122) on line 187 in file /var/www/vhosts/bndev.int.bijlesnetwerk.nl/httpdocs/wp-content/plugins/BNWPweb/includes/examenTraining.php 
第183行是第一个标题语句,wordpress核心文件引用的行包含以下内容:

} else {
            echo $tag;
            $this->print_inline_style( $handle );
        }
现在我的问题是,如何通过插件短代码发送标题,而不让标题已经发送警告?

我已经在google上搜索了不少,我读了一些stackexchange的答案,但似乎没有一个是适用的,我认为随机空白或我的一个插件输出不应该输出的东西不是问题。我也读过Wordpress FAQ about headers already sent 无济于事。

1 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

内容已发送到浏览器后,不能添加标题。如果您尝试,您将看到“警告无法修改标题信息-标题已由…发送”消息。

短代码在the_content 过滤器,并且该过滤器在页面加载中运行良好。这意味着,在您尝试添加标题时,内容早已发送到浏览器。要使用短代码实现此功能,您需要执行以下操作done here, 但老实说,我建议不要使用短代码。这不是更改早期页面加载内容的正确工具。

我不能确切地说出你在做什么,但我的直觉是custom meta field 会是一个更好的选择。

结束

相关推荐

How to modify admin headers

我正在尝试创建一个主题设置导出脚本,该脚本base\\u 64对主题选项进行编码,并允许用户将其下载为文本文件。我看到一个帖子(What is the best way to export and import theme options?) 这涉及检查查询变量,然后使用“template\\u redirect”操作重定向用户。然而,看起来这个操作只在站点的前端可用(不在管理中)。将操作添加到选项框架的构造函数中并没有任何作用。我可以通过将我的函数绑定到“admin\\u init”来启动它,但到那时,