我正在构建一个插件。我必须在标题中输出某些内容。主题的php文件取决于我是否要编写。然而,我还没有达到这一点。我只是为了将函数推入页眉而学习的。php我必须使用wp_head
. 不幸的是,当我这样做的时候,我得到了WordPress处理的许多额外的东西塞进了wp_head
作用
<meta name=\'robots\' content=\'noindex,follow\' />
<script type="text/javascript">
window._wpemojiSettings = {"baseUrl":"https:\\/\\/s.w.org\\/images\\/core\\/emoji\\/72x72\\/","ext":".png","source":{"concatemoji":"http:\\/\\/zachis.it\\/client\\/wgp-master\\/wp-includes\\/js\\/wp-emoji-release.min.js?ver=4.4.2"}};
!function(a,b,c){function d(a){var c,d=b.createElement("canvas"),e=d.getContext&&d.getContext("2d"),f=String.fromCharCode;return e&&e.fillText?(e.textBaseline="top",e.font="600 32px Arial","flag"===a?(e.fillText(f(55356,56806,55356,56826),0,0),d.toDataURL().length>3e3):"diversity"===a?(e.fillText(f(55356,57221),0,0),c=e.getImageData(16,16,1,1).data.toString(),e.fillText(f(55356,57221,55356,57343),0,0),c!==e.getImageData(16,16,1,1).data.toString()):("simple"===a?e.fillText(f(55357,56835),0,0):e.fillText(f(55356,57135),0,0),0!==e.getImageData(16,16,1,1).data[0])):!1}function e(a){var c=b.createElement("script");c.src=a,c.type="text/javascript",b.getElementsByTagName("head")[0].appendChild(c)}var f,g;c.supports={simple:d("simple"),flag:d("flag"),unicode8:d("unicode8"),diversity:d("diversity")},c.DOMReady=!1,c.readyCallback=function(){c.DOMReady=!0},c.supports.simple&&c.supports.flag&&c.supports.unicode8&&c.supports.diversity||(g=function(){c.readyCallback()},b.addEventListener?(b.addEventListener("DOMContentLoaded",g,!1),a.addEventListener("load",g,!1)):(a.attachEvent("onload",g),b.attachEvent("onreadystatechange",function(){"complete"===b.readyState&&c.readyCallback()})),f=c.source||{},f.concatemoji?e(f.concatemoji):f.wpemoji&&f.twemoji&&(e(f.twemoji),e(f.wpemoji)))}(window,document,window._wpemojiSettings);
</script>
<style type="text/css">
img.wp-smiley,
img.emoji {
display: inline !important;
border: none !important;
box-shadow: none !important;
height: 1em !important;
width: 1em !important;
margin: 0 .07em !important;
vertical-align: -0.1em !important;
background: none !important;
padding: 0 !important;
}
</style>
<link rel=\'stylesheet\' id=\'open-sans-css\' href=\'https://fonts.googleapis.com/css?family=Open+Sans%3A300italic%2C400italic%2C600italic%2C300%2C400%2C600&subset=latin%2Clatin-ext&ver=4.4.2\' type=\'text/css\' media=\'all\' />
<link rel=\'stylesheet\' id=\'dashicons-css\' href=\'http://zachis.it/client/wgp-master/wp-includes/css/dashicons.min.css?ver=4.4.2\' type=\'text/css\' media=\'all\' />
<link rel=\'stylesheet\' id=\'admin-bar-css\' href=\'http://zachis.it/client/wgp-master/wp-includes/css/admin-bar.min.css?ver=4.4.2\' type=\'text/css\' media=\'all\' />
<link rel=\'https://api.w.org/\' href=\'http://zachis.it/client/wgp-master/wp-json/\' />
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://zachis.it/client/wgp-master/xmlrpc.php?rsd" />
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://zachis.it/client/wgp-master/wp-includes/wlwmanifest.xml" />
<meta name="generator" content="WordPress 4.4.2" />
test<style type="text/css" media="print">#wpadminbar { display:none; }</style>
<style type="text/css" media="screen">
html { margin-top: 32px !important; }
* html body { margin-top: 32px !important; }
@media screen and ( max-width: 782px ) {
html { margin-top: 46px !important; }
* html body { margin-top: 46px !important; }
}
</style>
是否有其他方法将插件中的变量发布到标题中。php不使用
wp_head
?
SO网友:arafat
@布莱恩·威利斯有一个很好的答案,可以让你从wp_head
. 在大多数情况下,这可能是一种更好的方法,但如果你真的不需要所有额外的东西wp_head()
, 你可以决定不带它去。
但是,如果删除wp_head
完全从标题或模板中,您将无法使用如下挂钩wp_enqueue_scripts
, 这使得在模板中加载自己的CSS和JS文件变得容易。这就是为什么如果你wp_head
在你的模板中,除去你需要的以外的所有东西,你的模板将输出一个干净的头部,同时允许你从中受益wp_head()
.
这样,您就不必识别和删除WordPress或其他主题或插件加载到模板上的所有特定内容。
Remove all functions from wp_head
hook, and maybe from wp_footer
hook except the ones you need.
function unhook_wp_head_footer() {
if ( /* some condition */ ) {
global $wp_filter; // it contains all hooks
foreach ( $wp_filter[\'wp_head\']->callbacks as $priority => $wp_head_hooks ) {
if ( is_array( $wp_head_hooks ) ){
foreach ( $wp_head_hooks as $idx => $wp_head_hook ) {
if ( $wp_head_hook[\'function\'] !== \'wp_enqueue_scripts\' // keep functionality of wp_enqueue_scripts
&& $wp_head_hook[\'function\'] !== \'wp_print_head_scripts\' // to allow wp_enqueue_scripts load js inside the head element
&& $wp_head_hook[\'function\'] !== \'wp_print_styles\' ) { // to allow wp_enqueue_scripts load css inside the head element
remove_action( \'wp_head\', $wp_head_hook[\'function\'], $priority );
}
}
}
}
foreach ( $wp_filter[\'wp_footer\']->callbacks as $priority => $wp_footer_hooks ) {
if ( is_array( $wp_footer_hooks ) ) {
foreach ( $wp_footer_hooks as $idx => $wp_footer_hook ) {
if ( $wp_footer_hook[\'function\'] !== \'wp_print_footer_scripts\' ) { // to allow wp_enqueue_scripts load scripts in the footer
remove_action( \'wp_footer\', $wp_footer_hook[\'function\'], $priority );
}
}
}
}
}
}
add_action( \'wp\', \'unhook_wp_head_footer\' );
In case of keeping wp_enqueue_scripts
, dequeue all CSS and JS that you don\'t need.
// dequeue all assets if not from your plugin
public function dequeue_assets() {
if ( /* some condition */ ) {
global $wp_styles, $wp_scripts;
$prefix = \'plugin-handle\'; // your plugin name used as prefix in $handle of wp_enqueue_script or wp_enqueue_style function calls
foreach ( $wp_styles->queue as $handle ) {
if ( strpos( $handle, $prefix ) !== 0 ) {
wp_deregister_style( $handle );
wp_dequeue_style( $handle );
}
}
foreach ( $wp_scripts->queue as $handle ) {
if ( strpos( $handle, $prefix ) !== 0 ) {
wp_deregister_script( $handle );
wp_dequeue_script( $handle );
}
}
}
}
add_action( \'wp_enqueue_scripts\', \'dequeue_assets\', 9999 );