Question

I'm creating a video site in wordpress that will feauture both videoes and live streaming. The live streaming is occational and I'm trying to add a checkbox in the "new post" section where the user can check the box to display an alert that they are streaming live on the front page.

This is my code so far, but it does not save that the chebox is checked.

function register_post_assets(){
    add_meta_box('live-tv', __('Live TV'), 'add_featured_meta_box', 'post', 'advanced', 'high');
}

add_action('admin_init', 'register_post_assets', 1);

function add_featured_meta_box($post){
    $featured = get_post_meta($post->ID, 'live-tv', true);
    echo "<label for='live-tv'>".__('Is this a live broadcast post?', 'foobar')."</label>";
    echo "<input type='checkbox' name='live-tv' id='live-tv' value='1' ".checked(1,         $featured)."     />";
}

function save_featured_meta($post_id){
if (isset($_REQUEST['live-tv']))
    update_post_meta(esc_attr($post_id, '_featured-post', esc_attr($_REQUEST['featured-post']))); 
}
add_action('save_post', 'save_featured_meta');

I'm also wondering how i can check if the checkbox is checked to display content on the front page. Thank you so much for your time and any help will be appreciated!

Était-ce utile?

La solution

You have an error in your save_featured_meta function:

It has to be like this:

function save_featured_meta($post_id)
{
    if (isset($_REQUEST['live-tv']))
        update_post_meta(esc_attr($post_id), '_featured-post', esc_attr($_REQUEST['featured-post']));
}

You didn't close the esc_attr() at the right place.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top