Skip to content

Instantly share code, notes, and snippets.

@ShinichiNishikawa
Last active December 15, 2015 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShinichiNishikawa/5233530 to your computer and use it in GitHub Desktop.
Save ShinichiNishikawa/5233530 to your computer and use it in GitHub Desktop.
WordPressの管理画面の投稿タイトルの下に、ポストメタの入力欄を追加する
// タイトル下にタイトル2を追加
add_action( 'edit_form_after_title', 'nskw_postMetaAfterTitle' );
function nskw_postMetaAfterTitle() {
global $post;
$metaKey = 'title2';
if ( empty ( $post ) || 'post' !== get_post_type( $GLOBALS['post'] ) ) {
return;
}
if ( ! $content = get_post_meta( $post->ID, $metaKey, TRUE ) ) {
$content = '';
}
printf(
'<p><label for="%1$s_id">タイトル下段<input size="100" type="text" name="%1$s" id="%1$s_id" value="%2$s" /></label></p>',
$metaKey,
esc_attr( $content )
);
}
// タイトル2を保存
add_action( 'save_post', 'nskw_savePostMeta' );
function nskw_savePostMeta( $post_id )
{
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
$metaKey = 'title2';
if ( isset ( $_POST[ $metaKey ] ) ) {
return update_post_meta( $post_id, $metaKey, $_POST[ $metaKey ] );
}
delete_post_meta( $post_id, $metaKey );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment