WordPress adding custom field to the post screen

<?php
// add custom post meta media_url for posts
function themename_media_url_meta() {
	add_meta_box( 'themename_meta', 'Media embed URL', 'themename_media_url_meta_html', 'post', 'side', 'default' );
}
add_action( 'add_meta_boxes', 'themename_media_url_meta' );

function themename_media_url_meta_html( $post ) {
	$themename_media_url = get_post_meta( $post->ID, '_themename_media_url', true);
	?>
<input type="text" class="widefat" name="themename_media_url" value="<?php echo esc_attr( $themename_media_url ); ?>" />
<?php
}

function themename_save_project_meta( $post_ID ) {
	global $post;
	if( $post->post_type == "post" ) {
		if( isset( $_POST ) ) {
			update_post_meta( $post_ID, '_themename_media_url', strip_tags( $_POST['themename_media_url'] ) );
		}
	}
}
add_action( 'save_post', 'themename_save_project_meta' );
?>

Leave a Comment