WordPress插件和wp_头
我重新编辑了这个问题:是否可以在显示点2中的输出之前将变量传递给全局颜色(点3),就像全局变量或其他什么WordPress插件和wp_头,wordpress,plugins,Wordpress,Plugins,我重新编辑了这个问题:是否可以在显示点2中的输出之前将变量传递给全局颜色(点3),就像全局变量或其他什么 class myclass { public function init() { global $shortcode_tags; add_shortcode( MYSHORTCODE, array( 'myclass', 'sho
class myclass
{
public function init()
{
global $shortcode_tags;
add_shortcode( MYSHORTCODE, array( 'myclass', 'shortcode' ) );
// * point 1
return;
}
public function shortcode( )
{
// *point2
}
function globalcolor($color)
{
echo '<style>body{color:' .$color . '}</style>' . "\n";
// * point 3
}
}
add_action( 'wphead', array( 'myclass', 'globalcolor' ) );
add_action( 'init', array( 'myclass', 'init' ) );
class-myclass
{
公共函数init()
{
全局$shortcode_标记;
添加_短代码(MYSHORTCODE,数组('myclass','shortcode');
//*第1点
返回;
}
公共函数短代码()
{
//*第2点
}
函数globalcolor($color)
{
回显'body{color:'.$color.}'。“\n”;
//*第3点
}
}
添加操作('wphead',数组('myclass','globalcolor');
添加动作('init',数组('myclass','init');
另外,现在我正在阅读有关自定义字段的信息。在此处输入代码
执行操作()
由WordPress调用,您需要
操作init
来得太早了。您现在甚至可以为后端调用该类,用于AJAX请求等。使用hooktemplate\u redirect
,该类仅在前端调用
无法按尝试的方式发送颜色值。有关工作示例,请参见示例代码
示例代码:
class My_Plugin {
/**
* Container for your color value.
* @var string
*/
static $color;
public static function init()
{
// Set the color value as a class member.
self::$color = '#345';
// Class methods are addressed with an array of the object or the
// class name and the function name.
add_action( 'wp_head', array ( __CLASS__, 'print_color' ) );
}
public static function print_color()
{
// In action you have to print/echo to get an output.
print '<style>body{color:' . self::$color . '}</style>';
}
}
add_action( 'template_redirect', array ( 'My_Plugin', 'init' ) );
class My\u插件{
/**
*颜色值的容器。
*@var字符串
*/
静态颜色;
公共静态函数init()
{
//将颜色值设置为类成员。
self::$color='#345';
//类方法通过对象数组或
//类名和函数名。
添加动作('wp_head',数组('wp_CLASS','print_color');
}
公共静态函数print_color()
{
//在实际操作中,您必须打印/回显以获得输出。
打印“body{color:”。self::$color.'}”;
}
}
添加动作('template_redirect',数组('My_Plugin','init');
我强烈建议您在WordPress上提出更多问题。:) 谢谢,但我一点也不清楚,而且我已经更新了我的完整计划。@greenbandit这是一个……全新的问题。。)短代码在
wp\u head
之后解析。改为使用一个。@toscho关于快捷码的好观点,主要思想是通过快捷码添加自定义颜色和背景,如[background color=“#0066ff”bg=“url”]有什么想法吗?@greenbandit没有输出缓冲是不可能的。你不想走那条路,真的。使用自定义字段。短代码不是为你想要的东西而设计的。@toscho再次感谢,你能告诉我使用自定义字段的正确方向吗?再次感谢您抽出时间。