Duplicate Category in Wordpress
- سُلَيْمَان بْن دَاوُوْد

- 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 WordPressfunction 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 logicfunction 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' );
Comments