Skip to content

Instantly share code, notes, and snippets.

@BretCameron
Last active November 6, 2019 23:13
Show Gist options
  • Select an option

  • Save BretCameron/3d7487207d8eae1a2c7fc32d75ece448 to your computer and use it in GitHub Desktop.

Select an option

Save BretCameron/3d7487207d8eae1a2c7fc32d75ece448 to your computer and use it in GitHub Desktop.
Add a custom field type called "genre" to WordPress.
<?php // Add Custom Field Type: Genre
function genre_meta_box() {
add_meta_box(
'global-notice',
__( 'Genre', 'sitepoint' ),
'genre_meta_box_callback',
'movies',
'side',
'low'
);
}
function genre_meta_box_callback() {
global $post;
$custom = get_post_custom($post->ID);
$genre = $custom["genre"][0];
?>
<input style="width:100%" name="genre" value="<?php
echo $genre; ?>" />
<?php
}
function save_genre(){
global $post;
update_post_meta($post->ID, "genre",
$_POST["genre"]);
}
add_action( 'add_meta_boxes', 'genre_meta_box' );
add_action( 'save_post', 'save_genre' );
@phoydar

phoydar commented Nov 6, 2019

Copy link
Copy Markdown

Why does this have printer_category? Was this a copy and paste from somewhere else?

@BretCameron

Copy link
Copy Markdown
Author

Why does this have printer_category? Was this a copy and paste from somewhere else?

@phoydar I adapted this gist from one of my real-world projects, so left "printer_category" in there by accident!

I've replaced it with "genre"!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment