我正在尝试创建自己的类,以便在所有WP站点中进行一些最常见的更改。如在不需要时删除规范标记或更改元描述。
我没有创建插件,因为我不熟悉它。
我已经写了下面的课程,但我还没能让它完全发挥作用。只要调用了\\u setup函数,就会在源代码上输出任何其他内容。类似地,所有add\\u操作(“wp\\u head”)都会被忽略。
我怀疑这是因为我回想起这门课太晚了,但我不知道是否是这样,我如何才能测试它。
class Stuff_Frontend {
private static $_instance = null;
public static function init()
{
add_action( \'wp_head\', array(self::instance(), \'_setup\'));
}
public static function instance()
{
// create a new object if it doesn\'t exist.
is_null(self::$_instance) && self::$_instance = new self;
return self::$_instance;
}
/**
* Setup the class
* Adds and removes a lot of filters.
*/
function _setup() {
echo "I\'m here;";
add_action( \'wp_head\', array(self::instance(), \'head\' ), 1 );
// Remove actions that we will handle through our wpseo_head call, and probably change the output of
remove_action( \'wp_head\', \'rel_canonical\' );
remove_action( \'wp_head\', \'index_rel_link\' );
remove_action( \'wp_head\', \'start_post_rel_link\' );
remove_action( \'wp_head\', \'adjacent_posts_rel_link_wp_head\' );
remove_action( \'wp_head\', \'rsd_link\' );
remove_action( \'wp_head\', \'wlwmanifest_link\' );
add_action( \'mystuff\', array(self::instance(), \'change_metadesc\' ), 10 );
}
/**
* Main wrapper function attached to wp_head. This combines all the output on the frontend.
*/
public function head() {
echo "this is a test for my class head";
/**
* Action: \'mystuff\'
*/
do_action( \'mystuff\' );
return;
}
/**
* Change the meta description element.
*
* @param bool $echo Whether or not to echo the description.
*
* @return string
*/
public function change_metadesc() {
echo "this is an attempt to change the description";
}
}
在函数中调用上述类。php ini使用以下语句:
include( locate_template( \'MyStuff.php\' ) );
Stuff_Frontend::init();
任何提示都将不胜感激。谢谢