删除该过滤器/函数,并在PHP模板/页面文件中应用标记。如果需要帮助,请在输出标题的位置张贴。
下面是我如何使用类设置它的:
if ( ! class_exists( \'ThemeCustomizations\' ) ) {
class ThemeCustomizations {
static $inBody = false;
public static function set_in_body_true() {
static::$inBody = true;
}
public static function set_in_body_false() {
static::$inBody = false;
}
public static function filter_bloginfo( $name, $show = null ) {
if ( \'name\' == $show && static::$inBody ) {
$name = "<span class=\'info-style\'>Info</span>" . "<span class=\'psi-style\'>Psi</span>" . "<span class=\'md-style\'>.md</span>";
return "$name";
} else {
return $name;
}
}
}
}
add_action( \'wp_head\', array ( \'ThemeCustomizations\', \'set_in_body_true\' ), PHP_INT_MAX );
add_action( \'wp_footer\', array ( \'ThemeCustomizations\', \'set_in_body_false\' ), 0 );
add_action( \'bloginfo\', array ( \'ThemeCustomizations\', \'filter_bloginfo\' ), 10, 2 );
静态,并使用函数的静态范围变量:
function prefix__is_in_body( $isTrue = null ) {
// static initializer is false
static $inBody = false;
// only overwrite if boolean supplied
if ( is_bool( $isTrue ) ) {
$inBody = $isTrue;
}
// return regardless of getter/setter
return $inBody;
}
add_action( \'wp_head\', function(){ prefix__is_in_body(true); }, PHP_INT_MAX );
add_action( \'wp_footer\', function(){ prefix__is_in_body(false); }, 0 );
add_action( \'bloginfo\', function($name, $show = null){
if ( \'name\' == $show && prefix__is_in_body() ) {
$name = "<span class=\'info-style\'>Info</span>" . "<span class=\'psi-style\'>Psi</span>" . "<span class=\'md-style\'>.md</span>";
return "$name";
} else {
return $name;
}
}, 10, 2 );
魔法
Singleton
&;
Factory
模式+PHP魔术方法。
if ( ! class_exists( \'Magic\' ) ) {
class Magic {
private static $__ = array ();
public $_ = array ();
function __construct( $args = null ) {
if ( is_array( $args ) ) {
foreach ( $args as $k => $v ) {
$this->{$k} = $v;
}
}
return $this;
}
public static function instance( $id = \'\', $args = null ) {
if ( ! isset( self::$__[ $id ] ) ) {
self::$__[ $id ] = new Magic($args);
}
return self::$__[ $id ];
}
public function __get( $k ) {
return isset( $this->_[ $k ] ) ? $this->_[ $k ] : null;
}
public function __set( $k, $v ) {
return $this->_[ $k ] = $v;
}
public function __call( $k, $a ) {
if ( isset($this->_[ $k ]) && is_callable( $this->_[ $k ] ) ) {
return call_user_func_array( $this->_[ $k ], $a );
}
}
}
}
现在使用
Magic
课堂上你可以这样做;
$m = Magic::instance( \'\', array (
\'isBody\' => false,
\'action_wp_head\' => function() { Magic::instance()->isBody = true; },
\'action_wp_footer\' => function() { Magic::instance()->isBody = false; },
\'filter_wp_bloginfo\' => function( $output, $show ) {
return ( \'name\' == $show && Magic::instance()->isBody ) ? \'<span class="info-style">Info</span><span class="psi-style">Psi</span><span class="md-style">.md</span>\' : $output;
},
));
add_action( \'wp_head\', array ( $m, \'action_wp_head\' ), PHP_INT_MAX );
add_action( \'wp_footer\', array ( $m, \'action_wp_footer\' ), 0 );
add_filter( \'bloginfo\', array ( $m, \'filter_wp_bloginfo\' ), 10, 2 );