WordPress private \\u doing\\u it\\u Error()函数的替代方法是什么?
WordPress永远无法摆脱_doing_it_wrong()
函数,因此使用它非常安全。但如果出于某种原因,您不想使用它,因为它被标记为私有,那么您可以创建一个插件,该插件具有一个名为doing_it_wrong()
即复制和粘贴自_doing_it_wrong()
.
另一种方法是不复制代码,而是使用处理不推荐使用的代码的类。下面的一些示例代码基本上与_doing_it_wrong()
.
class deprecated {
protected $method;
protected $message;
protected $version;
public function method( $method ) {
$this->method = $method;
return $this;
}
public function message( $message ) {
$this->message = $message;
return $this;
}
public function version( $version ) {
$this->version = sprintf(
__( \'This message was added in version %1$s.\' ),
$version
);
return $this;
}
public function trigger_error() {
do_action( \'doing_it_wrong_run\', $this->method, $this->message, $this->version );
if ( WP_DEBUG && apply_filters( \'doing_it_wrong_trigger_error\', true ) ) {
trigger_error( sprintf(
__( \'%1$s was called <strong>incorrectly</strong>. %2$s %3$s\' ),
isset( $this->method ) ? $this->method : \'\',
isset( $this->message ) ? $this->message : \'\',
isset( $this->version ) ? $this->version : \'\'
) );
}
}
}
用法
class wpse_238672 {
public function some_deprecated_method() {
( new deprecated() )
->method( __METHOD__ )
->message( __(
\'Deprecated Method: Use non_deprecated_method() instead.\', \'wpse-238672\'
) )
->version( \'2.3.4\' )
->trigger_error();
$this->non_deprecated_method();
}
public function non_deprecated_method() {
}
}