5

WordPressのバックエンドにあるカスタム列の1つに編集、削除、表示などを追加する方法がわからないようです。アイデアは、タイトルにカーソルを合わせたときにタイトルに添付されているリンクを取得して、別の列に添付することです。

これは、以下のコードが出力するものです。

これは、このスクリーンショットのタイトルにカーソルを合わせたときのように、マウスを著者にカーソルを合わせたときに、[著者]列のリンクに表示したいものです。すべての編集リンク。

これは私がこれまでに持っているものです:

add_filter( 'manage_edit-testimonial-quotes_columns', 'view_columns' ) ;
function view_columns( $columns ) {
  $columns = array(
    'cb' => '',
    'date' => __( 'Date' ),
    'tq_author' => __( 'Author' ),
    'tq_quote' => __( 'Testimonial' ),
  );
  return $columns;
}
add_action('manage_testimonial-quotes_posts_custom_column', 'custom_view_columns', 10, 2);
function custom_view_columns($column, $post_id){
global $post;
  switch ($column){
  case 'tq_author':
    echo '<a href="post.php?post=' . $post->ID . '&action=edit">';
    $column_content = the_field('tq_author');
    echo $column_content;
    echo '</a>';
    break;
  case 'tq_quote':
    $column_content = the_field('tq_quote');
    echo $column_content;
    break;
  default:
    break;
  }
}
4

4 に答える 4

14

WP 4.3.0以降、これを行うための最良の方法は

add_filter( 'list_table_primary_column', [ $this, 'list_table_primary_column' ], 10, 2 );

public function list_table_primary_column( $default, $screen ) {
    if ( 'edit-yourpostype' === $screen ) {
        $default = 'yourcolumn';
    }
    return $default;
}
于 2017-02-21T00:42:23.340 に答える
4

私はそれに対処するためのフックがあることを本当に疑っています。だから、私はコアをチェックすることさえせず、汚れた解決策に直接行きます:

add_action( 'admin_head-edit.php', 'so_13418722_move_quick_edit_links' );

function so_13418722_move_quick_edit_links()
{
    global $current_screen;
    if( 'post' != $current_screen->post_type )
        return;

    if( current_user_can( 'delete_plugins' ) )
    {
        ?>
        <script type="text/javascript">
        function so_13418722_doMove()
        {
            jQuery('td.post-title.page-title.column-title div.row-actions').each(function() {
                var $list = jQuery(this);
                var $firstChecked = $list.parent().parent().find('td.author.column-author');

                if ( !$firstChecked.html() )
                    return;

                $list.appendTo($firstChecked);
            }); 
        }
        jQuery(document).ready(function ($){
            so_13418722_doMove();
        });
        </script>
        <?php
    }
}

結果
クイック移動-編集

  • post_typeを調整します:'post' != $current_screen->post_type
  • 列クラスを調整します。find('td.author.column-author')

バグと解決策
更新後、クイック編集メニューが元の位置に戻ることに注意してください。次のAJAXインターセプトはそれを処理します。詳細については、このWordPress開発者の回答を参照してください。

add_action( 'wp_ajax_inline-save', 'so_13418722_ajax_inline_save' , 0 );

/**
 Copy of the function wp_ajax_inline_save()
 http://core.trac.wordpress.org/browser/tags/3.4.2/wp-admin/includes/ajax-actions.php#L1315

 Only Modification marked at the end of the function with INTERCEPT
*/
function so_13418722_ajax_inline_save()
{
    global $wp_list_table;

    check_ajax_referer( 'inlineeditnonce', '_inline_edit' );

    if ( ! isset($_POST['post_ID']) || ! ( $post_ID = (int) $_POST['post_ID'] ) )
        wp_die();

    if ( 'page' == $_POST['post_type'] ) {
        if ( ! current_user_can( 'edit_page', $post_ID ) )
            wp_die( __( 'You are not allowed to edit this page.' ) );
    } else {
        if ( ! current_user_can( 'edit_post', $post_ID ) )
            wp_die( __( 'You are not allowed to edit this post.' ) );
    }

    set_current_screen( $_POST['screen'] );

    if ( $last = wp_check_post_lock( $post_ID ) ) {
        $last_user = get_userdata( $last );
        $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' );
        printf( $_POST['post_type'] == 'page' ? __( 'Saving is disabled: %s is currently editing this page.' ) : __( 'Saving is disabled: %s is currently editing this post.' ),    esc_html( $last_user_name ) );
        wp_die();
    }

    $data = &$_POST;

    $post = get_post( $post_ID, ARRAY_A );
    $post = add_magic_quotes($post); //since it is from db

    $data['content'] = $post['post_content'];
    $data['excerpt'] = $post['post_excerpt'];

    // rename
    $data['user_ID'] = $GLOBALS['user_ID'];

    if ( isset($data['post_parent']) )
        $data['parent_id'] = $data['post_parent'];

    // status
    if ( isset($data['keep_private']) && 'private' == $data['keep_private'] )
        $data['post_status'] = 'private';
    else
        $data['post_status'] = $data['_status'];

    if ( empty($data['comment_status']) )
        $data['comment_status'] = 'closed';
    if ( empty($data['ping_status']) )
        $data['ping_status'] = 'closed';

    // update the post
    edit_post();

    $wp_list_table = _get_list_table('WP_Posts_List_Table');

    $mode = $_POST['post_view'];
    $wp_list_table->display_rows( array( get_post( $_POST['post_ID'] ) ) );

    // INTERCEPT: Check if it is our post_type, if not, do nothing  
    if( 'post' == $_POST['post_type'] )
    {
    ?>
        <script type="text/javascript">so_13418722_doMove();</script>
    <?php       
    }
    // end INTERCEPT    

    wp_die();

}
于 2012-12-01T19:16:55.917 に答える
2

私も同じ必要がありました。

「未定義の変数」の通知/エラーが発生したことを除いて、Nicolaのソリューションは私のために機能しました。次に、パラメータには「[$this...]」の部分がない必要があることに気付きました。

それはドキュメントからのコピー/貼り付けだったと思います。

だからこれはうまくいった:

add_filter( 'list_table_primary_column', 'list_table_primary_column', 10, 2 );
function list_table_primary_column( $default, $screen ) {
    if ( 'edit-your_post_type' === $screen ) {
        // Set default columns to Minutes Spent.
        $default = 'your_column';
    }
    return $default;
}

your_columnが何であるかわからない場合は、その列のタイトルを調べてIDを取得してください。

于 2018-01-11T20:41:30.020 に答える
0

受け入れられた答えがそうであるように、JSに頼らずにアクション自体を動かすことはできません。ただし、phpと組み込みのWordpress関数を使用して、独自のバージョンのアクションポップアップを非常に簡単に作成できます。このバージョンは、ユーザーがJSをオフにしている場合でも機能します。

スイッチを使用してカスタム列にデータを入力すると仮定して、独自の関数に適応しない場合は、次のようなことを行います。

switch ( $column ) {
    case 'your_column_name':
        echo "Your custom content here"; 
        my_custom_column_actions($post_id);
        break;
}

次に、アクションポップアップを再作成する別の関数を用意します。

function my_custom_column_actions($post_id) {
        if($_GET['post_status']!='trash') :
            $bare_url = "/wp-admin/post.php?post=$post_id&amp;action=trash";
            $nonce_url = wp_nonce_url( $bare_url, 'trash-post_'.$post_id );
            echo "  <div class='row-actions'>
                        <span class='edit'>
                            <a href='/wp-admin/post.php?post=$post_id&amp;action=edit'>Edit</a> | 
                        </span>
                        <span class='trash'>
                            <a href='$nonce_url' class='submitdelete'>Trash</a>
                        </span>
                        <span class='edit'>
                            <a href='".get_the_permalink($post_id)."'>View</a> | 
                        </span>
                    </div>";
        else: 
            $bare_url = "/wp-admin/post.php?post=$post_id&amp;action=untrash";
            $nonce_url = wp_nonce_url( $bare_url, 'untrash-post_'.$post_id );
            $delete_url = "/wp-admin/post.php?post=$post_id&amp;action=delete";
            $nonce_delete_url = wp_nonce_url( $delete_url, 'delete-post_'.$post_id );
            echo "  <div class='row-actions'>
                        <span class='untrash'>
                            <a href='$nonce_url' class='untrash'>Restore</a> | 
                        </span>
                        <span class='delete'>
                            <a href='$nonce_delete_url' class='submitdelete'>Delete Permanently</a>
                        </span>
                    </div>";
        endif;
    }

sを使用するすべてのビジネスnonce_urlは、ゴミ箱に移動、復元、または削除するために重要です。これらのアクションはそれなしでは機能しません。

作成者や公開日など、通常表示される列にこれを含める場合は、カスタム列を宣言するときに標準列を含める必要はなく、代わりに同じデータを取得するカスタム列を含める必要があります(さらに、上記の関数の呼び出し)。

タイトルを含めたいが、その下にアクションを表示させたくない場合も同じです。そうでない場合は、2回表示されます。列を含めず、代わりに、タイトルを取得するが新しい関数を取得しないtitle独自のカスタム列を含めます。

関数がエコーするコンテンツを編集するだけで、独自のアクションを非常に簡単に追加することもできます。

于 2016-05-04T07:45:33.103 に答える