Last active
May 3, 2021 23:10
-
-
Save mishterk/9d91431afc66e8338fcd976bce80e135 to your computer and use it in GitHub Desktop.
A function that clones an existing reusable block a given number of times.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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