I am trying to create a metabox input field in a custom post type and it does not work even if I followed the instructions the best I could. I am not a PHP dev so I guess it could be just a small thing that is missing or it is wrong.
By not working I mean, I does not show at all in the WP UI.
Here is the code:
<?php
function add_post_type($name, $args = array()) {
add_action('init', function() use($name, $args) {
$upper = ucwords($name);
$name = strtolower(str_replace(' ','_',$name));
$args = array_merge(
array(
'public'=> true,
'label' => "All $upper" . 's',
'labels' => array('add_new_item' => "Add New $upper"),
'support' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields'),
'taxonomies' => array('post_tag','category')
),
$args
);
register_post_type('$name', $args);
});
}
//now we create and register a taxonomy
function add_taxonomy($name, $post_type, $args = array()) {
$name = strtolower($name);
add_action('init', function() use($name, $post_type, $args) {
$args = array_merge(
array(
'label' => ucwords($name),
),
$args
);
register_taxonomy($name, $post_type, $args);
});
}
/************************************************************
Now we add the names of the custom post type and taxonomies
*************************************************************/
add_post_type('snippet', array(
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields'),
'taxonomies' => array('post_tag')
));
add_taxonomy('language', 'snippet');
/************************************************************
Creating Metaboxes
*************************************************************/
add_action('add_meta_boxes', function() {
add_meta_box(
'er_snippet_info',
'Snippet Info',
'er_snippet_info_cb',
'snippet',
'normal',
'high'
);
});
function er_snippet_info_cb() {
global $post;
$url = get_post_custom($post->ID);
?>
<label for="er_associated_url">Associated URL: </label>
<input type="text" name="er_associated_url" id="er_associated_url" value="<?php echo $url; ?>" />
<?php
}
add_action('save_post', function () {
global $post;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
//security check
if ($_POST && !wp_verify_nonce($_POST['er_nonce'], _FILE_)) {
if ( isset ($_POST['er_associated_url']) ) {
update_post_meta($post->ID, 'er_associated_url', $_POST['er_associated_url']);
}
}
});
?>
The other issue is that the Tag is not named even if the function is there.