Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

So I'm having issues regarding a Dynamic Todo List Metabox. It should be so simple, everything else is working except for saving. I've been looking at it for 5 hours now, and could do with some fresh eyes.

    function webm_todo_box(){
    global $post;
    ?>
    <div id="meta_inner">
    <?php

    //get the saved meta as an arry
    $TodoList = get_post_meta($post->ID,'TodoList');

    $c = 0;
    if ( is_array($TodoList) ) {
        foreach( $TodoList as $Todo ) {
            if ( isset( $TodoList['todotitle'] ) ) {
                printf( '<p>Todo Item Title: <input type="text" name="TodoList[%1$s][todotitle]" value="%2$s" /><span class="remove">%4$s</span></p>', $c, $TodoList['todotitle'], __( 'Remove Track' ) );
                $c = $c +1;
            }
        }
    }

    ?>
<span id="here"></span>
<span class="add"><?php _e('Add Todo Item'); ?></span>
<script>
    var $ =jQuery.noConflict();
    $(document).ready(function() {
        var count = <?php echo $c; ?>;
        $(".add").click(function() {
            count = count + 1;

            $('#here').append('<p>Todo Item Title: <input type="text" name="TodoList['+count+'][todotitle]" value="" /><span class="remove">Remove Track</span></p>' );
            return false;
        });
        $(".remove").live('click', function() {
            $(this).parent().remove();
        });
    });
    </script>
</div><?php

}

add_action( 'save_post', 'save_TodoList', 10, 2 );
/* When the post is saved, saves our custom data */
function save_TodoList( $post_id ) {
    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return;

    // OK, we're authenticated: we need to find and save the data

    $TodoList = $_POST['todotitle'];


    update_post_meta($post_id,'todotitle', $TodoList);
}
share|improve this question
    
Can you please recheck the code you have pasted? save_TodoList function is missing –  Maruti Mohanty Sep 24 '13 at 6:38
    
Sure, oh my apologies :) –  Scott Baker Sep 24 '13 at 6:39
    
Code updated :) –  Scott Baker Sep 24 '13 at 6:41

2 Answers 2

I too have come across this strange issue, I am not sure why $post_id doesn't contain the post id. Please try the below snippet and this would work

Use the global $post and use $post->ID in update_post_meta

add_action( 'save_post', 'save_TodoList' );
/* When the post is saved, saves our custom data */
function save_TodoList() {
    global $post;

    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return;

    // OK, we're authenticated: we need to find and save the data

    $TodoList = $_POST['todotitle'];

    update_post_meta($post->ID,'todotitle', $TodoList); 
}
share|improve this answer
    
Still doesn't seem to be working. :( –  Scott Baker Sep 24 '13 at 6:49
    
Are you sure of getting the value of $TodoList? –  Maruti Mohanty Sep 24 '13 at 6:56
    
I'm not sure, is there a way I can test? –  Scott Baker Sep 24 '13 at 7:01
    
Yes you can, but before that can you test my code with taking off the following from the snippet if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; –  Maruti Mohanty Sep 24 '13 at 7:04
    
I've had that line in with every test I've made. I've also tested without, same thing. –  Scott Baker Sep 24 '13 at 7:05

In your input field the name attribute is "TodoList…" I would say "$_POST['todotitle']" is just empty

add_action( 'save_post', 'save_TodoList', 10, 2 );
/* When the post is saved, saves our custom data */
function save_TodoList( $post_id ) {
    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return;

    // OK, we're authenticated: we need to find and save the data

    $TodoList = $_POST['todotitle'];

    update_post_meta($post_id,'todotitle', $TodoList);

    # DEBUG START
    echo '<pre>' . print_r( $_POST, TRUE ) . '</pre>';
    die('---end---');
    # DEBUG END
}

Debug first what you are doing.

  1. Check if the function is actually called
  2. Check if you pass the 'DOING_AUTOSAVE' clause
  3. Check if your $_POST var is set
  4. Check if the var contain the data you expect
  5. Save the data
share|improve this answer
    
Didn't know you could print out the full array, handy Debugging feature. It's told me that the [todotitle] => 1234, which is what I've set it to, it's just not updating the boxes back on the post. –  Scott Baker Sep 24 '13 at 12:32
    
It's also printing out to the single template, so there's an issue with the update_post_meta then? –  Scott Baker Sep 24 '13 at 12:33
    
Sorry for the treble comment, starting to resolve this now, it's an issue with my foreach loop, rather than instantiating todotitle it's setting todotitle to be the last entry from the last box. –  Scott Baker Sep 24 '13 at 12:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.