Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nikitasinelnikov/e9f2dd69862965e507d4fd913f5fff76 to your computer and use it in GitHub Desktop.
Save nikitasinelnikov/e9f2dd69862965e507d4fd913f5fff76 to your computer and use it in GitHub Desktop.
Change roles by regexp
$posts = get_posts( array(
'numberposts' => -1,
'post_type' => 'any',
'post_status' => 'publish'
) );
foreach ( $posts as $post ) {
$content = preg_replace_callback( "/(\[um_show_content roles=\')(.*?)(\'\])/im", "um_custom_change_show_content_roles", $post->post_content );
$post->post_content = $content;
wp_update_post(array(
'ID' => $post->ID,
'post_content' => $post->post_content
));
}
function um_custom_change_show_content_roles( $matches ) {
$roles = $matches[2];
$roles = explode( ',', $roles );
$roles = array_map( function( $item ) {
return 'um_' . $item;
}, $roles );
$roles = implode( ',', $roles );
return $matches[1]. $roles . $matches[3];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment