Examples

Jay Hoffmann edited this page Jan 3, 2017 · 3 revisions

Post

Add Gallery category to every post with [gallery] shortcake

function find_gallery_posts() {
	register_batch_process( array(
		'name' => 'Convert Gallery Posts',
		'type' => 'post',
		'args' => array(
			'post_type' => 'post',
		),
		'callback' => 'add_gallery_category',
	) );
}
add_action( 'locomotive_init', 'find_gallery_posts' );

function add_gallery_category( $post ) {

	if ( strpos( $post->post_content, '[gallery' ) !== false ) {
		wp_set_object_terms( $post->ID, 'gallery', 'category', true );
	}

}

Change from one Custom Post Type to another

function change_custom_post_type_process() {
	register_batch_process( array(
		'name' => 'Change Custom Post Type',
		'type' => 'post',
		'args' => array(
			'post_type' => 'original-post-type'
		),
		'callback' => 'process_post_type_change',
	) );
}
add_action( 'locomotive_init', 'change_custom_post_type_process' );

function process_post_type_change( $post ) {

	set_post_type( $post->ID, 'new-post-type' );

}

User

Reset all user passwords

function register_user_password_reset() {
	register_batch_process( array(
		'name' => 'Reset all passwords',
		'type' => 'user',
		'callback' => 'reset_user_password',
		'args' => array(
			'offset' => 0
		)
	) );
}
add_action( 'locomotive_init', 'register_user_password_reset' );

function reset_user_password( $user ) {

	// Ensure that current user is not included.
	if( $user->ID !== get_current_user_id() ) {

		// Turn off reset password admin emails.
		add_filter( 'send_password_change_email', '__return_false' );
		$newpass = wp_generate_password();
		wp_update_user( array( 'ID' => $user->ID, 'user_pass' => $newpass ) );

	}

}

Comments

Delete all spam comments

function process_spam_comments() {
	register_batch_process( array(
		'name' =>Delete All Spam Comments',
		'type' => 'comment',
		'args' => array(
			'status' => 'spam',
		),
		'callback' => 'delete_comment_if_spam',
	) );
}
add_action( 'locomotive_init', 'process_spam_comments' );

function delete_comment_if_spam( $comment ) {

	wp_delete_comment( $comment->comment_ID, true );

}

Move all unapproved comments to approved

function process_unapproved_comments() {
	register_batch_process( array(
		'name' => 'Move Approved Comments',
		'type' => 'comment',
		'args' => array(
			'status' => 'hold',
		),
		'callback' => 'move_unapproved_comment',
	) );
}
add_action( 'locomotive_init', 'process_unapproved_comments' );

function move_unapproved_comment( $comment ) {

	wp_set_comment_status( $comment->comment_ID, 'approve' );

}

Terms

Prepend text to all categories

function get_all_categories() {
	register_batch_process( array(
		'name' => 'Move Approved Comments',
		'type' => 'term',
		'args' => array(
			'taxonomy' => 'category',
		),
		'callback' => 'prepend_text_to_category',
	) );
}
add_action( 'locomotive_init', 'get_all_categories' );

function prepend_text_to_category( $category ) {

	$new_name = 'This is: ' . esc_attr( $category->name );
	
	wp_update_term( $category->term_id, 'category', array(
			'name' => $new_name,
	) );

}
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.