With the tutorial series of codeacademy.cc(https://www.youtube.com/watch?v=33bilBgZrb4) I tried to create the following plugin. However, it doesn’t save the metadata. I don’t know what is happening here and I can’t find out why it is not saving it. I put different “die” functions into the if clauses for a kind of debug. It seems that $_POST['the-key']
is not set. When I try to use an else
clause to add_meta_data
, it also changes nothing. Does anyone have a clue how I can fix this code?
Also I tried different hooks:
pre_post_update
save_post
post_updated
(adding a line because stackoverflow has a problem with list+code)
<?php
/*
* Plugin Name: Zenva Video Widget
* Plugin URI: http://www.zenva.com
* Description: Learn how to create shortcodes and to retrieve data from the web.
* Version: 1.0
* Author: IQ Development
* Author URI: http://www.zenva.com
* License: GPL2
*/
add_action('add_meta_boxes', 'zvavw_add_metabox');
add_action('save_post', 'zvavw_save_metabox');
function zvavw_add_metabox() {
add_meta_box('zvavw_youtube', 'YouTube Video Link', 'zvavw_youtube_handler', 'post');
}
function zvavw_youtube_handler() {
$value = get_post_custom($post->ID);
$youtube_link = esc_attr($value['zvavw_youtube'][0]);
echo '<label for="zvavw_youtube">YouTube Video Link</label><input type="text" id="zvavw_youtube" value="' . $youtube_link . '" />';
}
function zvavw_save_metabox($post_id) {
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if(!current_user_can('edit_post')) {
return;
}
if(isset($_POST['zvavw_youtube'])) {
update_post_meta($post_id, 'zvavw_youtube', esc_url($_POST['zvavw_youtube']));
}
}
?>
I don’t remember if I forgot ID or name, but I forgot one of the both on the input box, and because of that it didn’t get any value. So if anyone ever bumps into this problem, check for ID / name on your input tag.