Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Matt Campbell
Matt Campbell
9,767 Points

Meta box input value not been saved to database but entry is

Built a tonne of meta boxes across this site I'm building. Built a meta box for venue district, address and phone number.

It used to work but for some reason now, when you click update, a blank box is returned BUT a blank entry is being created in the database so the save function is working. The meta box is shared in both a CPT and WooCommerce product.

Same meta box works fine in WooCommerce. It's getting the data correctly for inputs previous to the problem beginning. I have no idea where to start, gone through all the usual debugging but can't find an issue anywhere.

It's only for the CPT, all other meta boxes work wherever else you put them. But no meta boxes work properly in this particular CPT.

Can you help Zac Gordon or Kevin Korte? It's an odd one that's very frustrating.

4 Answers

Matt Campbell
Matt Campbell
9,767 Points

The code:

    add_action('add_meta_boxes', 'create_venue_meta');

    function create_venue_meta(){
        add_meta_box('venue-meta-box', 'Venue Information', 'build_venue_meta', 'venues', 'side');
        add_meta_box('venue-meta-box', 'Venue Information', 'build_venue_meta', 'product', 'side', 'high');
        }

    function build_venue_meta($post){
        $venue_district = get_post_meta($post->ID, '_venue_district', true);
        $venue_address = get_post_meta($post->ID, '_venue_address', true);
        $venue_phone = get_post_meta($post->ID, '_venue_phone', true);

        ?>

        <form>
            <label for="venue_district" style="width:40%; display:inline-block;">Venue District: </label>
            <input type="text" name="venue_district" id="venue_district" value="<?php echo esc_attr($venue_district); ?>" />
            <label for="venue_address" style="width:40%; display:inline-block;">Venue Address: </label>
            <input type="text" name="venue_address" id="venue_address" value="<?php echo esc_attr($venue_address); ?>" />
            <label for="venue_phone" style="width:40%; display:inline-block;">Venue Phone: </label>
            <input type="tel" name="venue_phone" id="venue_phone" value="<?php echo esc_attr($venue_phone); ?>" />
        </form>

        <?php
    }

add_action('save_post', 'save_venue_meta');

    function save_venue_meta($post_id){

        if(isset($_POST['venue_district'])){
            update_post_meta($post_id, '_venue_district',
                strip_tags($_POST['venue_district']));
        }

        if(isset($_POST['venue_address'])){
            update_post_meta($post_id, '_venue_address',
                strip_tags($_POST['venue_address']));
        }

        if(isset($_POST['venue_phone'])){
            update_post_meta($post_id, '_venue_phone',
                strip_tags($_POST['venue_phone']));
        }

    }
Matt Campbell
Matt Campbell
9,767 Points

Development - if I add the value to the empty key in the database, guess what....it's echo'd out. So, somewhere, the input text isn't being passed to the database.

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Hmm, not seeing a problem at first glance. Is it not saving the post at all, or just the meta information?

Matt Campbell
Matt Campbell
9,767 Points

Hi Zac Gordon. I've managed to solve the problem but still don't understand it.

What it was doing was adding the key to the database and a value but there was no information in the value.

This relates to my other thread about two meta boxes in the sidebar.

I was adding each meta box with it's own action and function etc. I discovered that, once placing both meta boxes in the same action and function it resolved the issue.

What I don't get though is that I have another meta box in the main body of the page and that works just fine, even with the sidebar meta box.

Any ideas?