自定义快速编辑列中未定义变量post_id

时间:2018-04-10 作者:Downloadtaky

我正在尝试添加一个自定义栏来发布快速编辑,一切都很正常。自定义元已保存并传递,但如果单击“快速编辑”,预览将为空。这就是它给我的错误:

Notice: 未定义变量:post_id 在/home/etimueit/public\\u html/wp content/themes/caru/functions中。php在线626

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

这是我的密码。

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 个回复
最合适的回答,由SO网友:Downloadtaky 整理而成

我们终于找到了一个解决方案,我把它放在这里,也许它能帮上忙(也许未来的我XD)。

/**
**Aggiungi Disponibilitánel快速编辑*/

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);

SO网友:Mat

很抱歉,在查看WordPress Codex将自定义可编辑数据添加到快速编辑中后,我不得不更改我的答案。因此,您必须删除对的引用$post_id 太(从add_action 函数中的参数和)。

看起来像quick_edit_custom_box 仅接受2个参数:$column_name$post_type. 然后,为了获取和显示值,必须使用更多的PHP和Javascript。

如果您进一步阅读quick edit custom box 并进一步阅读\'Setting Existing Values\', 您将看到,要做到这一点,还需要做更多的工作。

我建议你更详细地阅读这些文件,因为这并不像我最初想的那么直截了当。。。

结束