Undefined variable post_id in custom quick edit coloumn

I am trying to add a custom coloumn to post quick edit and everything is almost working. Custom meta is saved and passed but if I click on quick edit the preview is blank.
This is the error it gives me:

Notice: Undefined variable: post_id in /home/etimueit/public_html/wp-content/themes/caru/functions.php on line 626

I can see the data here (last coloumn)
No variable here (but still saved in DB)

Here is my code.

function disponibilitaet_quickedit_custom_posts_columns( $posts_columns ) {
    $posts_columns['disponibilitaet_edit_time'] = __( 'Modifica Disponibilità', 'disponibilitaet' );
    return $posts_columns;
}
add_filter( 'manage_post_posts_columns', 'disponibilitaet_quickedit_custom_posts_columns' );`

function disponibilitaet_quickedit_custom_column_display( $column_name, $post_id ) {
    if ( 'disponibilitaet_edit_time' == $column_name ) {
        $dispo_registrata = get_post_meta( $post_id, 'disponibilitaet_edit_time', true );

        if ( $dispo_registrata ) {
            echo esc_html( $dispo_registrata );
        } else {
            esc_html_e( 'N/A', 'disponibilitaet' );
        }
    }
}
add_action( 'manage_post_posts_custom_column', 'disponibilitaet_quickedit_custom_column_display', 10, 2 );

function disponibilitaet_quickedit_fields( $column_name, $post_type ) {
    if ( 'disponibilitaet_edit_time' != $column_name )
        return;`
    //This is line 626

    $dispo_registrata = get_post_meta( $post_id, 'disponibilitaet_edit_time', true );
    ?>
    <fieldset class="inline-edit-col-right">
        <div class="inline-edit-col">
            <label>
                <span class="title"><?php esc_html_e( 'Disponibilit&agrave;', 'disponibilitaet' ); ?></span>
                <span class="input-text-wrap">
                <input type="text" name="disponibilitaet_edit_time" class="disponibilitaetedittime" value="<?php echo $dispo_registrata;?>">
            </span>
            </label>
        </div>
    </fieldset>
    <?php
}
add_action( 'quick_edit_custom_box', 'disponibilitaet_quickedit_fields', 10, 2 );

function disponibilitaet_quickedit_save_post( $post_id, $post ) {
    // if called by autosave, then bail here
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;

    // if this "post" post type?
    if ( $post->post_type != 'post' )
        return;

    // does this user have permissions?
    if ( ! current_user_can( 'edit_post', $post_id ) )
        return;

    // update!
    if ( isset( $_POST['disponibilitaet_edit_time'] ) ) {
        update_post_meta( $post_id, 'disponibilitaet_edit_time', $_POST['disponibilitaet_edit_time'] );
    }
}
add_action( 'save_post', 'disponibilitaet_quickedit_save_post', 10, 2 );

function disponibilitaet_quickedit_javascript() {
    $current_screen = get_current_screen();
    if ( $current_screen->id != 'edit-post' || $current_screen->post_type != 'post' )
        return;

    // Ensure jQuery library loads
    wp_enqueue_script( 'jquery' );
    ?>
    <script type="text/javascript">
        jQuery( function( $ ) {
            $( '#the-list' ).on( 'click', 'a.editinline', function( e ) {
                e.preventDefault();
                var editTime = $(this).data( 'edit-time' );
                inlineEditPost.revert();
                $( '.disponibilitaetedittime' ).val( editTime ? editTime : '' );
            });
        });
    </script>
    <?php
}
add_action( 'admin_print_footer_scripts-edit.php', 'disponibilitaet_quickedit_javascript' );

2 Answers
2

Sorry, I’ve had to change my answer after checking the WordPress Codex on adding custom editable data to the quick edit. So you’ll have to remove the references to $post_id too (from the add_action arguments and from within your function).

It looks like the quick_edit_custom_box only takes 2 arguments: $column_name and $post_type. Then for getting and displaying the value, this has to be done using some more PHP and Javascript.

If you read further in to the WordPress Codex on the quick edit custom box and read further down to the section on ‘Setting Existing Values’, you’ll see there’s a fair bit more work required in order to do this.

I’d suggest reading the docs in a bit more detail as it’s not quite as straight-forward as I first thought…

Leave a Comment