投稿に keyword description メタタグの追加

WordPressでは、

<meta name="generator" content="WordPress 3.8.1" />

といったメタ要素が自動的に埋め込まれます。

このコードを、functions.phpに追加することで、

generatorの出力で使われているフィルタを使って、

シングルページ表示した場合に、カスタムフィールドに、descriptionまたは、keywordというフィールドがある場合に、その値を、

generatorのメタタグの代わりに、descriptionとkeywordを埋めこみます。

カスタムフィールドに該当するフィールドがない時は、generatorを表示します

<?php
function nobita_generator( $gen, $type ) {
	global $post;
	$result= '';
	
	if ( is_singular( ) ) {
	
		$description= get_post_meta( $post->ID, 'description', true );
		
		if ( ! empty( $description ) ) {
		
			$result .= '<meta name="description" content="'. esc_attr( $description ). '" />';
		}
		
		$keyword= get_post_meta( $post->ID, 'keyword', true );
		
		if ( ! empty( $keyword ) ) {
	
			$result .= '<meta name="keywords" content="'. esc_attr( $keyword ). '" />';
		}
		
		if ( !empty( $result ) ) {
		
			return $result;
		}	
	}
	return $gen;
}
add_filter( 'get_the_generator_xhtml', 'nobita_generator', 10, 2 );
?>

以下のようなソースが出力されます。

<meta name="description" content="hello world" /><meta name="keywords" content="hello, world" />

[emulsion_relate_posts]