[00] Subset Clone, For developers
Developer reference
Duplicating from your own code, the filters that decide what is copied and by whom, the REST route and the WP-CLI command.
This page covers duplicating from your own code, Subset Clone’s filters and actions, the REST route and the WP-CLI command. The list actions, both editors, the admin bar, REST and WP-CLI all use the same permission check and engine, so every hook here applies everywhere. Pro’s cross-site hooks are at the end.
Duplicating from your own code
<?php
use Subset\Duplicator\Plugin;
$plugin = Plugin::instance();
// The engine doesn't check permissions itself, so check first.
$error = $plugin->permissions()->check( $post_id ); // null, or a WP_Error with an HTTP status
if ( null === $error ) {
$copy_id = $plugin->duplicator()->duplicate(
$post_id,
array(
'title' => 'Landing page B', // optional; default "Title (Copy)" or the settings' format
'author' => 7, // optional; default the current user, or the setting
'attachments' => true, // optional; default the setting
)
);
}
duplicate() returns the copy’s ID or a WP_Error with an HTTP status in its data:
| Code | Status | When |
|---|---|---|
subset_dup_not_found | 404 | The post doesn’t exist. |
subset_dup_unsupported_type | 400 | The post type can’t be duplicated. |
subset_dup_auto_draft | 400 | The post hasn’t been saved yet. |
subset_dup_meta_failed | 500 | A meta row couldn’t be written. |
A copy is always a new draft with a fresh date and an empty slug. If any step fails or throws, Subset Clone deletes the copy and any attachments made for it, so you never get a half-made post.
What gets copied
Post types
<?php
// Default: every type with an admin UI, minus attachments, revisions, menu items,
// site editor types and WooCommerce's `product`.
add_filter( 'subset_dup_supported_post_types', function ( array $types ): array {
return array_values( array_diff( $types, array( 'my_type' ) ) );
} );
Removing a type is how a plugin with its own duplicator steps aside: Subset Clone’s actions disappear from that type’s screens. The Content types setting then switches each remaining type on or off for the site, and a switched-off type is refused everywhere.
Until the setting is saved for a type, WordPress’s own types (posts, pages, patterns) and the active theme’s types are on, and other plugins’ types are off. A type counts as the theme’s when a file in the theme’s folder, or its parent theme’s, registers it.
<?php
// Whether a type is on before the setting says otherwise.
add_filter( 'subset_dup_post_type_on_by_default', function ( bool $on, string $post_type ): bool {
return 'event' === $post_type || $on; // A companion plugin's type, on like the theme's.
}, 10, 2 );
Post data and title
<?php
// The arguments as passed, before defaults are filled in.
add_filter( 'subset_dup_args', function ( array $args, WP_Post $original ): array {
return $args;
}, 10, 2 );
// The fields passed to wp_insert_post(), unslashed.
add_filter( 'subset_dup_post_data', function ( array $data, WP_Post $original, array $args ): array {
$data['post_status'] = 'pending'; // send copies straight to review
return $data;
}, 10, 3 );
// The title, when the caller didn't pass one.
add_filter( 'subset_dup_copy_title', function ( string $title, WP_Post $original, array $args ): string {
return 'Copy of ' . $original->post_title;
}, 10, 3 );
Meta
Every meta row is copied as stored, byte for byte and in order, including protected keys, serialized objects and slashed JSON. Core’s add_post_meta and added_post_meta actions still fire for each row.
<?php
// Keys left out. `*` matches any run of characters.
add_filter( 'subset_dup_excluded_meta_keys', function ( array $keys, WP_Post $original, int $copy_id ): array {
$keys[] = '_my_plugin_cache_*';
return $keys;
}, 10, 3 );
// Change a value before it's copied, or return null to skip the row.
add_filter( 'subset_dup_meta_value', function ( $value, string $key, int $original_id, int $copy_id ) {
return 'views' === $key ? 0 : $value;
}, 10, 4 );
The default exclusions are WordPress’s bookkeeping (edit locks, old slugs and dates, trash and ping markers), the copy’s own origin link, and caches from Elementor, Rank Math, Yoast Duplicate Post and WPML that need rebuilding for the copy.
Terms
The original’s terms are assigned to the copy by ID, for every taxonomy of the post type. No terms are created. Polylang’s post_translations is left out, so a copy keeps its language but doesn’t join the original’s translation group.
<?php
add_filter( 'subset_dup_taxonomies', function ( array $taxonomies, WP_Post $original ): array {
return array_diff( $taxonomies, array( 'series' ) );
}, 10, 2 );
Attachments
The featured image comes across by reference with the meta. Child attachments are copied only when the Attached media setting is on. The copies are new attachment posts that point to the same files.
<?php
// Default: the setting. An `attachments` argument beats both.
add_filter( 'subset_dup_copy_attachments', function ( bool $enabled, WP_Post $original ): bool {
return 'gallery' === $original->post_type;
}, 10, 2 );
Who may duplicate
Permissions::check( $post_id, $user_id ) refuses unless all of these hold:
- the post exists and its type is supported and switched on
- the user can
edit_postit: their own posts, and other people’s only withedit_others_posts. A copy carries every custom field, hidden ones included, so being able to read the original isn’t enough - the user can create posts of its type
- the user has a role the settings allow (administrators and super admins always do)
Refusals use the code subset_dup_forbidden, with status 401 when logged out and 403 otherwise.
<?php
// The last word, after the built-in checks.
add_filter( 'subset_dup_can_duplicate', function ( bool $can, int $post_id, int $user_id ): bool {
return $can && ! get_post_meta( $post_id, '_locked', true );
}, 10, 3 );
Admin links use a per-post nonce, subset_dup_{id}. Subset\Duplicator\Admin\DuplicateRequest::url( $post_id ) builds a complete duplicate link.
Reacting to a copy
<?php
add_action( 'subset_dup_before_duplicate', function ( WP_Post $original, array $args ) {}, 10, 2 );
// After every step, and after WordPress's own wp_after_insert_post for the copy.
add_action( 'subset_dup_after_duplicate', function ( int $copy_id, int $original_id, array $args ) {
// reset a counter, or log the copy
}, 10, 3 );
Each copy stores its original’s ID in the protected meta key _subset_dup_original. Subset\Duplicator\Duplication\Origin::original_of( $copy_id ) returns the original as a WP_Post, or null when there’s no link or the original is gone.
Other filters
| Hook | Type | Purpose |
|---|---|---|
subset_dup_bulk_limit | filter | Posts per bulk Duplicate request. Default 50. |
subset_dup_loading | action | Fires before any component registers, for add-ons. |
subset_dup_admin_tabs | filter | Adds tabs to Subset → Duplicator (slug => label). |
subset_dup_menu_status | filter | Status badges shown for Subset Clone on the Subset overview. |
REST
POST /wp-json/subset-duplicator/v1/posts/{id}/duplicate
| Argument | Type | Notes |
|---|---|---|
title | string | Optional. |
attachments | boolean | Optional. Overrides the setting. |
status | string | Only draft. Copies are always drafts. |
On success it returns 201 with id, original, type, status, title and edit_link, plus a Location header for the copy’s core REST route. Refusals use the permission check’s codes and statuses.
const copy = await wp.apiFetch({
path: `/subset-duplicator/v1/posts/${postId}/duplicate`,
method: "POST",
data: { title: "Landing page B" },
});
Add-ons can accept more arguments with subset_dup_rest_route_args and turn them into engine arguments with subset_dup_rest_duplicate_args.
WP-CLI
wp subset duplicator duplicate duplicates posts with the same permission check and settings as the Duplicate buttons. WP-CLI commands lists its options, the settings commands and what Pro adds on multisite.
Copying to another site (Pro)
Subset Clone Pro adds cross-site copies on multisite. See Copy to another site for how it works from wp-admin.
From your own code:
<?php
use Subset\DuplicatorPro\Plugin;
$pro = Plugin::instance();
$error = $pro->cross_site_permissions()->check( $post_id, $target_site_id );
if ( null === $error ) {
$copy_id = $pro->cross_site()->duplicate( $post_id, $target_site_id, array( 'title' => 'Landing page B' ) );
}
The REST route takes target_site (a site ID). The request then runs the cross-site permission check and the response adds site. The WP-CLI command takes --to-site=<site>, as a site ID or URL, and adds a site column. On the target site, _subset_dup_original holds site_id:post_id.
| Hook | Type | Arguments |
|---|---|---|
subset_dup_pro_cross_site_args | filter | $args, WP_Post $original, int $target_site_id |
subset_dup_pro_can_copy_to_site | filter | bool $can, int $post_id, int $target_site_id, int $user_id |
subset_dup_pro_target_has_post_type | filter | bool $exists, string $post_type, int $target_site_id |
subset_dup_pro_cross_site_excluded_meta_keys | filter | array $keys, $snapshot |
subset_dup_pro_create_missing_terms | filter | bool $create, string $taxonomy, int $target_site_id |
subset_dup_pro_map_term | filter | ?int $term_id, array $source_term, string $taxonomy, int $target_site_id |
subset_dup_pro_remap_content | filter | string $remapped, string $content, array $ids, array $urls, $snapshot |
subset_dup_pro_target_site_query | filter | The get_sites() query that lists a super admin’s target sites. |
subset_dup_pro_before_cross_site_duplicate | action | WP_Post $original, int $target_site_id, array $args (source site) |
subset_dup_pro_after_cross_site_duplicate | action | int $copy_id, int $original_id, int $source_site_id, array $args (target site) |
subset_dup_pro_create_missing_terms defaults to the network-wide Categories and tags setting. subset_dup_pro_map_term gets the matched or created term ID, or null, and can return another term ID.