我正在使用wordpress 5.2.3
,我正在尝试为插件mailster 2.4.4
(用于wordpress的新闻通讯插件)编写自己的自定义标签。
我应该在mailster_add_tag
上添加一个动作functions.php
的{{3}}。
但是,我试图创建自己的插件,因为将来会增加更多的功能/复杂性:
我的newsletter.php
:
<?php
/**
Plugin Name: Newsletter Extension
description: Mailster Newsletter Extension
Version: 1.0
Author: Batman
License: GPLv2 or later
Text Domain: newsletter
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Newsletter {
/**
* Constructor.
*/
public function __construct() {
// constants.
define( '_FILE', __FILE__ );
define( '_DIR', trailingslashit( dirname( __FILE__ ) ) );
define( '_VERSION', '0.0.1' );
register_activation_hook( basename( _DIR ) . '/' . basename( _FILE ), array( $this, 'activate' ) );
add_action( 'plugins_loaded', array( $this, 'includes' ) );
}
/**
* Called on plugin activation
*/
public function activate() {
$this->includes();
$this->addCustomTags();
flush_rewrite_rules();
}
/**
* Includes.
*/
public function includes() {
include_once( _DIR . 'includes/DailyTemplate.php' );
}
/**
* Includes.
*/
public function addCustomTags() {
DailyTemplate::addMyTag();
}
}
new Newsletter();
在我的includes/DailyTemplate.php
内:
<?php
class DailyTemplate {
public function __construct() { }
public function addMyTag() {
if ( function_exists( 'mailster_add_tag' ) ) {
mailster_add_tag( 'coupon', function( $option, $fallback, $campaignID = null, $subscriberID = null ) {
// make sure the subscriber ID is set
if ( ! is_null( $subscriberID ) ) {
return get_subscribers_coupon( $subscriberID );
}
// return the fallback "NOCOUPONCODE4U"
return $fallback;
} );
}
}
function get_subscribers_coupon( $subscriber_id ) {
$seed = AUTH_SALT;
$length = 10;
$code = substr( strtoupper( base_convert( md5( $seed . $subscriber_id ), 16, 36 ) ), 0, $length );
return $code;
}
}
new DailyTemplate();
运行上面的代码时,我没有得到任何错误。
但是,在我看来,该插件未加载是因为-如文档所述-plugin documentation says中没有标签。 (请参见第图片)
任何建议我在加载标签时做错了什么?我使用了错误的钩子吗?
感谢您的答复!
答案 0 :(得分:1)
此处abcdefghijklmnopqrstuvwxyz
azyxwvutsrqponmlkjihgfedcb
所调用的方法是静态的,无需创建对象即可访问。因此,您可以将其声明为静态方法。自PHP 7开始,不建议静态调用非静态方法。声明为
DailyTemplate::addMyTag();
并检查其是否有效
答案 1 :(得分:1)
这里您要调用静态方法:
public function addCustomTags() {
DailyTemplate::addMyTag();
}
因此,最好将此方法声明为静态方法,因为不建议使用静态调用非静态方法,就像这样:
public static function addMyTag() { //.....
在“ addMyTag()”方法中,您可以在类内部调用成员方法,因此您应通过以下方式调用方法:
return self::get_subscribers_coupon( $subscriberID );
最后,您还应该将“ get_subscribers_coupon()”方法声明为静态方法,因为我们将其称为静态方法:
public static function get_subscribers_coupon( $subscriber_id ) { //....
希望获得帮助。