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

Let's play spot the difference. One meta box works, the other doesn't.

Hi folks,

I've built a meta box, it's all there on the screen, input text and hit update or publish and nothing happens.

No database entry, nothing.

Pulled in a meta box from another theme, put it on the same page and it works fine so I'm stumped. I've spent the last I don't know how long going letter by letter through the code to find a fault and I can't.

Another pair of eyes would be hugely appreciated, see if anyone spots anything.

Zac Gordon and Kevin Korte if you have a minute.

Thanks.

Top code is the working one from another theme, second code is not working but is identical???

The code -

add_action('add_meta_boxes', 'create_committee_meta');

    function create_committee_meta(){
        add_meta_box('committee-meta-box', 'Select Committee Information', 'build_committee_meta', 'team', 'side', 'high');
        }

    function build_committee_meta($post){
        $committee_position = get_post_meta($post->ID, '_committee_position', true);
        $committee_name = get_post_meta($post->ID, '_committee_name', true);
        $committee_company = get_post_meta($post->ID, '_committee_company', true);

        ?>

        <form>
            <label for="committee_position" style="width:40%; display:inline-block;">Position: </label>
            <input type="tel" name="committee_position" id="committee_position" value="<?php echo esc_attr($committee_position); ?>" />
            <label for="committee_name" style="width:40%; display:inline-block;">Name: </label>
            <input type="text" name="committee_name" id="committee_name" value="<?php echo esc_attr($committee_name); ?>" />
            <label for="committee_company" style="width:40%; display:inline-block;">Company: </label>
            <input type="text" name="committee_company" id="committee_company" value="<?php echo esc_attr($committee_company); ?>" />
        </form>

        <?php
    }

add_action('save_post', 'save_committee_meta');

    function save_committee_meta($post_id){

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

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

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

    }
add_action('add_meta_boxes', 'create_team_meta');

function create_team_meta(){
    add_meta_box('team-info-meta-box', 'Team Member Information', 'build_team_meta', 'team', 'normal', 'high');
}

function build_team_meta($post){
    $member_name = get_post_meta($post->ID, '_member_name', true);
    $member_age = get_post_meta($post->ID, '_member_age', true);
    $member_years = get_post_meta($post->ID, '_member_years', true);
    $member_platform = get_post_meta($post->ID, '_member_platform', true);
    $member_first_game = get_post_meta($post->ID, '_member_first_game', true);
    $member_favourite_game = get_post_meta($post->ID, '_member_favourite_game', true);
    $member_genres = get_post_meta($post->ID, '_member_genres', true);
    $member_suggestion = get_post_meta($post->ID, '_member_suggestion', true);

?>

<div id="team-member-meta">
    <div id="team-meta-form">
    <p>Please note, however you type something here is how it will appear on the page so watch those commas and capitals!</p>
    <p>If you'd like to see additional questions for the bio, type in the question on the last box and it might be added to this list.</p>  
    <form>
        <div class="team-col-one">
            <label for="member_name" class="member-label-one">Your Name </label>
            <input type="text" name="member_name" id="member-name" class="member-input" value="<?php echo esc_attr($member_name); ?>" />
            <label for="member_age" class="member-label-one">Your Age </label>
            <input type="text" name="member_age" id="member-age" class="member-input" value="<?php echo esc_attr($member_age); ?>" />
            <label for="member_years" class="member-label-one">Years Gaming </label>
            <input type="text" name="member_years" id="member-years" class="member-input" value="<?php echo esc_attr($member_years); ?>" />
            <label for="member_platform" class="member-label-one">Your Preferred Platforms </label>
            <input type="text" name="member_platform" id="member-platform" class="member-input" value="<?php echo esc_attr($member_platform); ?>" />
        </div>

        <div class="team-col-two">  
            <label for="member_genres" class="member-label-two">What genres do you play? </label>
            <input type="text" name="member_genres" id="member-genres" class="member-input" value="<?php echo esc_attr($member_genres); ?>" />
            <label for="member_first_game" class="member-label-two">What was your first game(s) you played? </label>
            <input type="text" name="member_first_game" id="member-first-game" class="member-input" value="<?php echo esc_attr($member_first_game); ?>" />
            <label for="member_favourite_game" class="member-label-two">What's your favourite game(s) played? </label>
            <input type="text" name="member_favourite_game" id="member-favourite-game" class="member-input" value="<?php echo esc_attr($member_favourite_game); ?>" />
            <label for="member_suggestion" class="member-label-two">Any other questions you'd like to see here? </label>
            <input type="text" name="member_suggestion" id="member-suggestion" class="member-input" value="<?php echo esc_attr($member_suggestion); ?>" />
        </div>  
    </form>
    </div>
</div>

<script>
var $ =jQuery.noConflict();
$(document).ready(function($) {
    var formHeight = $('#team-meta-form').outerHeight();

    $('#team-member-meta').css({'height':formHeight + 'px'});
}); 
</script>
<?php }

add_action('save_post', 'save_team_meta');

function save_team_meta($post_id){

    if(isset($_POST['member-name'])){
        update_post_meta($post_id, '_member_name', 
            strip_tags($_POST['member-name']));
    }

    if(isset($_POST['member-age'])){
        update_post_meta($post_id, '_member_age', 
            strip_tags($_POST['member-age']));
    }

    if(isset($_POST['member-years'])){
        update_post_meta($post_id, '_member_years', 
            strip_tags($_POST['member-years']));
    }

    if(isset($_POST['member-platform'])){
        update_post_meta($post_id, '_member_platform', 
            strip_tags($_POST['member-platform']));
    }

    if(isset($_POST['member-genres'])){
        update_post_meta($post_id, '_member_genres', 
            strip_tags($_POST['member-genres']));
    }

    if(isset($_POST['member-first-game'])){
        update_post_meta($post_id, '_member_first_game', 
            strip_tags($_POST['member-first-game']));
    }

    if(isset($_POST['member-favourite-game'])){
        update_post_meta($post_id, '_member_favourite_game', 
            strip_tags($_POST['member-favourite-game']));
    }

    if(isset($_POST['member-suggestion'])){
        update_post_meta($post_id, '_member_suggestion', 
            strip_tags($_POST['member-suggestion']));
    }
}

1 Answer

Kevin Korte
Kevin Korte
28,149 Points

You're stretching me here, lol. Where are you at on this? Did you get her figured out, or no?

Matt Campbell
Matt Campbell
9,767 Points

Hi Kevin Korte.

Yes I did get it working. And check out how, by copying and pasting a line at a time into the working meta box. It's so ridiculous.

The only difference is I'm using the word team instead of member and using underscores instead of hyphens for the ids.

I thought having underscore prefixes was causing an issue but looking at this and what I have now, no. I thought having two forms caused the problem but looking at this and what I have now, no!

I spent a whole afternoon and some weird phantom problem. That I totally don't understand. lol.

Either way, it works now. Very odd.