wordpress后台标题下添加自定义字段

add_action( 'edit_form_after_title', 'my_url_form_field' );
add_action( 'save_post', 'my_url_form_field_save' );

function my_url_form_field( $post ) {
if ( $post->post_type != 'post' ) return;
$format = get_post_format( $post->ID );
if ( !empty( $format ) && ( $format != 'link' ) ) return;
?>
<div id="urlwrap">
<p>
<label for="post-link-url"><strong>URL:</strong></label>
<input type="text" class="large-text" name="_my_custom_url" size="30" value="<?php echo get_post_meta($post->ID, '_my_custom_url', true); ?>" id="post-link-url" autocomplete="off" />
</p>
</div>
<?php
}

function my_url_form_field_save( $postid ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE );
if ( isset( $_POST[ '_my_custom_url' ] ) ) {
if ( ( strpos( $_POST[ '_my_custom_url' ], 'http://' ) !== 0 ) && ( strpos( $_POST[ '_my_custom_url' ], 'https://' ) !== 0 ) )
$_POST[ '_my_custom_url' ] = 'http://' . $_POST[ '_my_custom_url' ];
$url = filter_var( $_POST[ '_my_custom_url' ], FILTER_VALIDATE_URL ) ? $_POST[ '_my_custom_url' ] : '';
update_post_meta( $postid, '_my_custom_url', $url );
}
}