Add Duplicate link to pages, posts and custom posts.

Home » Snippets » Add Duplicate link to pages, posts and custom posts.
0

Created with:

Visibility: 

public

Creator: Ahrale

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
function web4udev_add_duplicate_post_link( $actions, $post ) {
    if ( current_user_can( 'edit_posts' ) ) {
        $duplicate_link = sprintf(
            '<a href="%s" title="%s">%s</a>',
            wp_nonce_url( admin_url( 'admin.php?action=web4udev_duplicate_post&post=' . $post->ID ), 'web4udev_duplicate_post_' . $post->ID ),
            esc_attr__( 'Duplicate this item', 'web4udev' ),
            esc_html__( 'Duplicate', 'web4udev' )
        );

        // Create a new array to hold the modified actions
        $new_actions = array();

        // Loop through the existing actions and insert the "Duplicate" link after the "View" link
        foreach ( $actions as $key => $value ) {
            $new_actions[ $key ] = $value;

            if ( $key === 'view' ) {
                $new_actions['duplicate'] = $duplicate_link;
            }
        }

        // Replace the original actions with the modified actions
        $actions = $new_actions;
    }

    return $actions;
}
add_filter( 'post_row_actions', 'web4udev_add_duplicate_post_link', 10, 2 );
add_filter( 'page_row_actions', 'web4udev_add_duplicate_post_link', 10, 2 );
add_filter( 'post_type_row_actions', 'web4udev_add_duplicate_post_link', 10, 2 );
				

Register an account to save your snippets or go Pro to get more features.