• I’d like to add a link in my post table. It is very similar to the edit link (under the post title), I just need to link an anchor to the post editor page. Name of the link: “Prezzi”

    I have found the code in internet but at the moment I only set the post type. The target address is https://test.sacconicase.com/wp-admin/post.php?post=9120&action=edit#prezzo_settimana , the post ID of course vary, in the post table each link gets the anchor for the corrispondent post ID

    the code:

    add_filter( 'post_row_actions', 'modify_list_row_actions', 10, 2 );
    
    function modify_list_row_actions( $actions, $post ) {
        // Check for your post type.
        if ( $post->post == "post" ) {
    
            // Build your links URL.
            $url = admin_url( '/wp-admin/post.php=' . $post->ID );
    
            // Maybe put in some extra arguments based on the post status.
            $edit_link = add_query_arg( array( 'action' => 'edit' ), $url );
    
            // The default $actions passed has the Edit, Quick-edit and Trash links.
            $trash = $actions['trash'];
    
            /*
             * You can reset the default $actions with your own array, or simply merge them
             * here I want to rewrite my Edit link, remove the Quick-link, and introduce a
             * new link 'Copy'
             */
            $actions = array(
                'edit' => sprintf( '<a href="%1$s">%2$s</a>',
                esc_url( $edit_link ),
                esc_html( __( 'Edit', 'contact-form-7' ) ) )
            );
    
            // You can check if the current user has some custom rights.
            if ( current_user_can( 'edit_my_cpt', $post->ID ) ) {
    
              // Include a nonce in this link
              $copy_link = wp_nonce_url( add_query_arg( array( 'action' => 'copy' ), $url ), 'edit_my_cpt_nonce' );
    
              // Add the new Copy quick link.
              $actions = array_merge( $actions, array(
                  'copy' => sprintf( '<a href="%1$s">%2$s</a>',
                    esc_url( $copy_link ),
                    'Duplicate'
                )
             ) );
    
                // Re-insert thrash link preserved from the default $actions.
                $actions['trash']=$trash;
            }
        }
    
        return $actions;
    }

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello @sacconi, your page link does not work, but I will try as much as possible to help.

    I have tried to modify the code:

    add_filter( 'post_row_actions', 'modify_list_row_actions', 10, 2 );
    
    function modify_list_row_actions( $actions, $post ) {
        // Check for your post type (e.g., 'post').
        if ( $post->post_type == 'post' ) {
    
            // Build your Prezzi link URL with the anchor.
            $url = admin_url( '/post.php?post=' . $post->ID . '&action=edit#prezzo_settimana' );
    
            // Add the 'Prezzi' link.
            $actions['prezzi'] = sprintf(
                '<a href="%s">%s</a>',
                esc_url( $url ),
                esc_html( 'Prezzi' )
            );
        }
    
        return $actions;
    }
    

    If you are using a custom post type, make sure to replace ‘post’ with the correct post type. Let me know if it works!

    Thread Starter sacconi

    (@sacconi)

    This is working but only if, after I display the post table, I filter the posts by id or name, so when I have just one row and only that specific post, when I click on the link “prezzi” I reach the anchor. But When I’m in the post table with all the post displayed, clicking the link just carry me to the post edit page, the anchor is not reached

    It’s likely because the anchor link is specific to the post’s individual edit page, and it doesn’t work as expected when viewing the post table with all posts. I believe you can modify this by adding JavaScript.

    Let me give it a try,

    with PHP,

    add_filter( ‘post_row_actions’, ‘modify_list_row_actions’, 10, 2 );

    function modify_list_row_actions( $actions, $post ) {
    // Check for your post type (e.g., ‘post’).
    if ( $post->post_type == ‘post’ ) {

    add_filter( 'post_row_actions', 'modify_list_row_actions', 10, 2 );
    
    function modify_list_row_actions( $actions, $post ) {
        // Check for your post type (e.g., 'post').
        if ( $post->post_type == 'post' ) {
    
            // Build the URL for the "Prezzi" link with a JavaScript function to scroll to the anchor.
            $url = 'javascript:void(0);';
            $anchor_link = '<a href="'.$url.'" class="prezzi-link" data-post-id="'.$post->ID.'">'.esc_html( 'Prezzi' ).'</a>';
    
            // Add the "Prezzi" link.
            $actions['prezzi'] = $anchor_link;
        }
    
        return $actions;
    }
    

    Now you can add js, to this. Kindly let me know if the code is working

    jQuery(document).ready(function($) {
    // Listen for clicks on the “Prezzi” link with the class ‘prezzi-link’.
    $(‘.prezzi-link’).click(function() {
    // Get the post ID from the ‘data-post-id’ attribute.
    var postID = $(this).data(‘post-id’);

    jQuery(document).ready(function($) {
        // Listen for clicks on the "Prezzi" link with the class 'prezzi-link'.
        $('.prezzi-link').click(function() {
            // Get the post ID from the 'data-post-id' attribute.
            var postID = $(this).data('post-id');
    
            // Build the anchor link specific to the post and scroll to it.
            var anchor = '#prezzo_settimana';
            var scrollTo = $('#post-' + postID).find(anchor);
    
            if (scrollTo.length > 0) {
                $('html, body').animate({
                    scrollTop: scrollTo.offset().top
                }, 1000); // You can adjust the animation speed (in milliseconds).
            }
        });
    });
    

    In the jQuery code, jQuery is used to scroll to the anchor #prezzo_settimana on the post’s table row. this is when the link is clicked.

    Thread Starter sacconi

    (@sacconi)

    I have to put js in a separated file and give it a name? And then where I put this file name inside php?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Modify list row action’ is closed to new replies.