web-dev-qa-db-ja.com

Wp_headを使わずにheader.phpにコンテンツを入れる

私はプラグインを作っています。私は書くかどうかに応じてテーマのheader.phpファイルに特定のものを出力する必要があります。しかし、私はまだその時点ではありません。私はちょうど関数をheader.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&#038;subset=latin%2Clatin-ext&#038;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>

wp_headを使用せずにプラグインからheader.phpに変数をポストする別の方法はありますか?

2
Zach Smith

Wp_headを削除すると、wordpress.orgのプラグインのほぼ90%を使用できなくなるため、悪影響があります。また、あなたが見ているそのコードのいくつかはあなたがログインしているときにだけ現れる(あなたのテーマが特にそれを追加していない限り)。たとえば、下にあるダッシュ、open sans、およびadmin-barスタイルシートとインラインスタイルはすべてそこにあるので、フロントエンドのadminバーを使用できます。

ここにいくつかの選択肢があります。

1.カスタムヘッダ - これはあなたのページの中にwp_headが欲しくないけれども他のテンプレートで他のワードプレスプラグインを使えるようにしたいのなら安全な代替策です。

  1. テーマフォルダにheader-custom.phpを含まないファイルwp_headを作成します。
  2. あなたのカスタム page template<?php get_header( 'custom' ); ?> を使ってそのヘッダを呼び出します
  3. プラグインを使ってまだこれにフックできないようにするには、wp_headの代わりにカスタムアクションを使用できます。基本的にあなたがしなければならないのはあなたがwp_headをするのと同じ方法で<?php do_action('custom_head'); ?>を加えてからあなたのプラグインフックにそれだけです。

function my_plugin_custom_head_action() {
    // do stuff here
}
add_action('custom_head', 'my_plugin_custom_head_action');

カスタムフック/アクションを使うこのプロセスをとても簡単にする plugin もあります。


2. wp_headから不要なものを削除する

// Remove emojis
function disable_emojis_wp_head() {
    remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
    remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
    remove_action( 'wp_print_styles', 'print_emoji_styles' );
    remove_action( 'admin_print_styles', 'print_emoji_styles' );    
    remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
    remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );  
    remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
    add_filter( 'tiny_mce_plugins', 'disable_emojis_wp_tinymce' );
}
add_action( 'init', 'disable_emojis_wp_head' );

function disable_emojis_wp_tinymce( $plugins ) {
    if ( is_array( $plugins ) ) {
        return array_diff( $plugins, array( 'wpemoji' ) );
    } else {
        return array();
    }
}

// Remove other crap in your example
add_action( 'get_header', function() {
    remove_action('wp_head', 'rsd_link'); // Really Simple Discovery service endpoint, EditURI link
    remove_action('wp_head', 'wp_generator'); // XHTML generator that is generated on the wp_head hook, WP version
    remove_action('wp_head', 'feed_links', 2); // Display the links to the general feeds: Post and Comment Feed
    remove_action('wp_head', 'index_rel_link'); // index link
    remove_action('wp_head', 'wlwmanifest_link'); // Display the link to the Windows Live Writer manifest file.
    remove_action('wp_head', 'feed_links_extra', 3); // Display the links to the extra feeds such as category feeds
    remove_action('wp_head', 'start_post_rel_link', 10, 0); // start link
    remove_action('wp_head', 'parent_post_rel_link', 10, 0); // prev link
    remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0); // relational links 4 the posts adjacent 2 the currentpost
    remove_action('template_redirect', 'wp_shortlink_header', 11);  
    remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
}, 99);

// Remove adminbar inline css on frontend
function removeinline_adminbar_css_frontend() {
    if ( has_filter( 'wp_head', '_admin_bar_bump_cb' ) ){
        remove_filter( 'wp_head', '_admin_bar_bump_cb' );
    }
}
add_filter( 'wp_head', 'removeinline_adminbar_css_frontend', 1 );

そして、あなたが残りのapiリンクを削除したいのなら、私はこれがうまくいくと信じています:

remove_action( 'template_redirect', 'rest_output_link_header', 11, 0 );

Rest apiを削除する追加の解決策については、この WPSEの投稿を参照してください

3
Bryan Willis