Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A function that clones an existing reusable block a given number of times.
<?php
/**
* Clone a given reusable block a given number of times.
*
* @param int $block_id The ID of an existing reusable block to clone.
* @param int $n_clones The number of clones to create.
*/
function clone_reusable_block( $block_id, $n_clones ) {
$post = get_post( $block_id );
$post->to_array();
unset( $post->ID );
$base_title = $post->post_title;
// Words for search testing later — these are appended to the clone title.
$words = [
'donkey',
'camel',
'microphone',
'water',
'apple',
'cat',
'sky',
'trees',
'house',
'car',
'aeroplane',
'microplane',
'ski',
'ski-doo',
'macbook',
'pop shield',
'guitar',
'venetion',
'apricot',
'banana',
'lamp',
'phone',
'glass',
'coffee',
];
for ( $i = 0; $i < $n_clones; $i ++ ) {
$post->post_title = $base_title . ' ' . $words[ array_rand( $words ) ] . ' ' . uniqid();
wp_insert_post( $post );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment