Move custom column admin links from below “post ti

2019-04-09 07:08发布

It seems I can't figure out how to add the edit, remove, view, etc... to one of my custom columns in the backend of wordpress. The idea is to get the links that are attached to title when one hovers over the title, to be attached to a different column.

This is what the below code outputs.

This is what I want the link in the authors column have when mouse is hovered over the authors, just like when you hover over the title in this screenshot; all the edit links.

This is what I have so far:

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

标签: wordpress
4条回答
Emotional °昔
2楼-- · 2019-04-09 07:30

You can't move the actions per se without resorting to JS, as the accepted answer does. However, you can very easily build your own version of the actions popup with php and inbuilt Wordpress functions. This version will work even if the user has JS turned off.

Presuming you use a switch to populate your custom columns, do something likes this, if not adapt to your own function:

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

Then have a separate function that recreates the actions popup.

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

All the business with nonce_urls is important for trashing, restoring or deleting; these actions won't work without it.

If you want to include this on a column that would normally appear, e.g. author or date published, you'll need to not include the standard column when you're declaring your custom columns and instead include a custom column that gets the same data (plus a call to the function above).

The same goes for if you want to include the title but not have the actions appear under it - otherwise they'll appear twice. Don't include the title column and instead include your own custom column that grabs the title but not the new function.

You can also very easily add your own actions by just editing the content that the function echoes.

查看更多
走好不送
3楼-- · 2019-04-09 07:32

I had the same need.

Nicola's solution worked for me except I was getting an "Undefined variable" notice/error. Then I realized that the parameter has to be without the "[$this ... ]" part.

I guess that was a copy/paste from the documentation.

So this worked:

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

If you don't know what your_column is, just inspect the title of that column and get the ID.

查看更多
戒情不戒烟
4楼-- · 2019-04-09 07:39

I really doubt that there's a hook to deal with that. So, I'll not even check the core and go straight to the dirty solution:

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

Result:
move quick-edit

Notes:

  • adjust your post_type: 'post' != $current_screen->post_type
  • adjust your column classes: find('td.author.column-author')

Bug and solution:
You'll note that, after updating, the quick-edit menu goes back to its original position. The following AJAX interception deals with it. Refer to this WordPress Developers answer for more details.

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

}
查看更多
对你真心纯属浪费
5楼-- · 2019-04-09 07:41

The best way of doing this since WP 4.3.0 is using

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;
}
查看更多
登录 后发表回答