top of page

Duplicate Category in Wordpress

  • Writer: سُلَيْمَان بْن دَاوُوْد
    سُلَيْمَان بْن دَاوُوْد
  • Feb 20, 2025
  • 1 min read

The following code helps to duplicate the default wordpress category by help of code.

// Add Duplicate option for categories in WordPress
function add_duplicate_category_link( $actions, $term ) {
    if ( 'category' === $term->taxonomy ) {
        $url = add_query_arg( 
            array(
                'action' => 'duplicate_category',
                'term_id' => $term->term_id
            ),
            admin_url( 'edit-tags.php' )
        );
        $actions['duplicate'] = '<a href="' . esc_url( $url ) . '">' . __( 'Duplicate' ) . '</a>';
    }
    return $actions;
}
add_filter( 'category_row_actions', 'add_duplicate_category_link', 10, 2 );
// Handle the duplicate category logic
function duplicate_category() {
    if ( isset( $_GET['action'] ) && $_GET['action'] === 'duplicate_category' && isset( $_GET['term_id'] ) ) {
        $term_id = intval( $_GET['term_id'] );
        // Get the original category
        $term = get_term( $term_id, 'category' );
        if ( $term && ! is_wp_error( $term ) ) {
            // Create a new category with the same name and description
            $new_term = wp_insert_term(
                $term->name . ' (Copy)', // Name of the new category
                'category',
                array(
                    'description' => $term->description,
                    'slug'        => $term->slug . '-copy', // Optional: Ensure unique slug
                )
            );
            // Redirect to the edit categories page after duplicating
            if ( ! is_wp_error( $new_term ) ) {
                wp_redirect( admin_url( 'edit-tags.php?taxonomy=category' ) );
                exit;
            }
        }
    }
}
add_action( 'admin_init', 'duplicate_category' );

 
 
 

Recent Posts

See All
C#.NET and Its Associated Codes

1- Creating an Desktop Application in C#.NET C# ADO.NET & MySQL MAIN C# and MySQL ADO.NET with Crystals Reports Ready Code 2- Handling...

 
 
 

Comments


© 2020 by syednazrulhassan

bottom of page