2

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

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...

  • Thank you for your answer, the error is gone with the field :) imgur.com/a/n1z45 Is there a way to also show the field in Quick Edit? – Downloadtaky Apr 10 '18 at 22:02
  • 1
    I've just changed my answer as I've just read the documentation and it looks like there's a fair bit more work involved in doing this and it's not as I first thought... – Mat Apr 10 '18 at 22:38
1

Here we are, finally found a solution, I leave it here so maybe it can help (maybe also future me XD ).

/**

* * Aggiungi Disponibilità nel Quick Edit */

function etdispo_quickedit_custom_posts_columns( $posts_columns ) {
    $posts_columns['et2018-quantita_birra'] = __( 'Disponibilità', 'etdispo' );
    return $posts_columns;
}
add_filter( 'manage_post_posts_columns', 'etdispo_quickedit_custom_posts_columns' );

function etdispo_quickedit_custom_column_display( $column_name, $post_id ) {
    if ( 'et2018-quantita_birra' == $column_name ) {
        $etdispo_regi = get_post_meta( $post_id, 'et2018-quantita_birra', true );

        if ( $etdispo_regi ) {
            echo esc_html( $etdispo_regi );
        } else {
            esc_html_e( 'N/A', 'etdispo' );
        }
    }
}
add_action( 'manage_post_posts_custom_column', 'etdispo_quickedit_custom_column_display', 10, 2 );

function etdispo_quickedit_fields( $column_name, $post_type, $post_id ) {
    if ( 'et2018-quantita_birra' != $column_name )
        return;

    $etdispo_regi = get_post_meta( $post_id, 'et2018-quantita_birra', true );
    ?>
    <fieldset class="inline-edit-col-right">
        <div class="inline-edit-col">
            <label>
                <span class="title"><?php esc_html_e( 'Disponibilità', 'etdispo' ); ?></span>
                <span class="input-text-wrap">
                <input type="text" name="et2018-quantita_birra" class="etdispoedit" value="">
            </span>
            </label>
        </div>
    </fieldset>
    <?php
}
add_action( 'quick_edit_custom_box', 'etdispo_quickedit_fields', 10, 3 );
function etdispo_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['et2018-quantita_birra'] ) ) {
        update_post_meta( $post_id, 'et2018-quantita_birra', $_POST['et2018-quantita_birra'] );
    }
}
add_action( 'save_post', 'etdispo_quickedit_save_post', 10, 2 );

function etdispo_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 editDispo = $(this).data( 'edit-dispo' );
                inlineEditPost.revert();
                $( '.etdispoedit' ).val( editDispo ? editDispo : '' );
            });
        });
    </script>
    <?php
}
add_action( 'admin_print_footer_scripts-edit.php', 'etdispo_quickedit_javascript' );
/* Qui */
function etdispo_quickedit_set_data( $actions, $post ) {
    $found_value = get_post_meta( $post->ID, 'et2018-quantita_birra', true );

    if ( $found_value ) {
        if ( isset( $actions['inline hide-if-no-js'] ) ) {
            $new_attribute = sprintf( 'data-edit-dispo="%s"', esc_attr( $found_value ) );
            $actions['inline hide-if-no-js'] = str_replace( 'class=', "$new_attribute class=", $actions['inline hide-if-no-js'] );
        }
    }

    return $actions;
}
add_filter('post_row_actions', 'etdispo_quickedit_set_data', 10, 2);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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