在向我的一个插件添加功能时,我遇到了一个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 无济于事。