我对构建插件很陌生,为了测试的目的,我想构建一个简单的插件来更改网站的标题。我需要什么挂钩或过滤器?
如何创建更改网站标题颜色的插件?
2 个回复
最合适的回答,由SO网友:Johansson 整理而成
具体的实现方法可能会因您使用的主题而异,但这里有一个简单的插件wp_head
动作挂钩,并在标题中添加一些样式:
<?php
/**
* @package PACKAGE_NAME
*/
/*
Plugin Name: Plugin name
Plugin URI: https://plugin-website
Description: Plugin Description
Version: 1.0.0
Author: Plugin Author
Author URI: https://author-website
License: Plugin License
Text Domain: text-domain
*/
add_action( \'wp_head\', \'wpse321903_add_styles\' );
function wpse321903_add_styles(){ ?>
<style>
.page-title {
color: white;
}
</style><?php
}
SO网友:Michae Pavlos Michael
或者,如果您已经将样式表排队(应该是这样),可以使用wp\\u add\\u inline\\u style()。按照Jack的解释设置插件标题,并使用以下代码:-
// if your plugin has its own stylesheet, include this line
// replacing \'my-plugin-css.css\' for your stylesheet filename
wp_enqueue_style( \'my-plugin-css\', \'my-plugin-css.css\' );
// add the inline style
// if you want to hijack the themes style, leave out the line above
// and replace \'my-plugin-css\' with your theme\'s wp_enqueue_style handle.
$custom_css = "
.page-title {
color: #f00;
}";
wp_add_inline_style( \'my-plugin-css\', $custom_css );
有关更多详细信息,请参见此处:https://codex.wordpress.org/Function_Reference/wp_add_inline_style