post

Copiar/mover paginas entre sites (multisites) WordPress

Publicado em: 05/09/2022

Categorias: BlogPHPWordpress

É possível mover paginas entre sites, da sua rede de multisites do WordPress com alguns trechos de código PHP.

Adicionar o trecho de código PHP abaixo em seu arquivo functions.php ou através de um plugin de código como WPCode.

/**
 * WordPress Multisite: Copy or Move Pages between Sites
 *
 * @author Misha Rudrastyh
 * @link https://rudrastyh.com/wordpress-multisite/move-posts-between-blogs.html
 */
// add bulk actions
add_filter( 'bulk_actions-edit-page', 'rudr_my_bulk_multisite_actions' );
function rudr_my_bulk_multisite_actions( $bulk_array ) {

	$sites = get_sites( array(
		// 'site__in' => array( 1,2,3 )
		'site__not_in' => get_current_blog_id(), // excluding current blog
		'number' => 50,
	) );
	
	if( $sites ) {
		foreach( $sites as $site ) {
			$bulk_array[ 'move_to_' . $site->blog_id ] = 'Move to "' . $site->blogname . '"';
		}
	}

	return $bulk_array;

}

// move or Copy posts 
add_filter( 'handle_bulk_actions-edit-page', 'rudr_bulk_action_multisite_handler', 10, 3 );
function rudr_bulk_action_multisite_handler( $redirect, $doaction, $object_ids ) {

	// we need query args to display correct admin notices
	$redirect = remove_query_arg( array( 'misha_posts_moved', 'misha_blogid' ), $redirect );

 	// our actions begin with "move_to_", so let's check if it is a target action
	if( strpos( $doaction, 'move_to_' ) === 0 ) {
		$blog_id = str_replace( 'move_to_', '', $doaction ); // get blog ID from action name

		foreach ( $object_ids as $post_id ) {

			// get the original post object as an array
			$post = get_post( $post_id, ARRAY_A );
			// if you need to apply terms (more info below the code)
			$post_terms = wp_get_object_terms( $post_id, 'category', array( 'fields' => 'slugs' ) );
			// get all the post meta
			$data = get_post_custom( $post_id );
			// empty ID field, to tell WordPress to create a new post, not update an existing one
			$post[ 'ID' ] = '';

			switch_to_blog( $blog_id );
			
			// insert the post
			$inserted_post_id = wp_insert_post( $post ); // insert the post
			// update post terms
			wp_set_object_terms( $inserted_post_id, $post_terms, 'category', false );
			// add post meta
			foreach ( $data as $key => $values) {
				// if you do not want weird redirects
				if( '_wp_old_slug' === $key ) {
					continue;
				}
				foreach( $values as $value ) {
					add_post_meta( $inserted_post_id, $key, $value );
				}
			}

			restore_current_blog();

			// if you want just to copy pages, comment this line
			wp_delete_post( $post_id );

		}

		$redirect = add_query_arg( array(
			'misha_posts_moved' => count( $object_ids ),
			'misha_blogid' => $blog_id
		), $redirect );

	}

	return $redirect;

}

// show an appropriate notice 
add_action( 'admin_notices', 'rudr_bulk_multisite_notices' );
function rudr_bulk_multisite_notices() {

	if( ! empty( $_REQUEST[ 'misha_posts_moved' ] ) ) {

		// because I want to add blog names to notices
		$blog = get_blog_details( $_REQUEST[ 'misha_blogid' ] );

		// depending on ho much posts were changed, make the message different
		echo '<div class="updated notice is-dismissible"><p>';
		printf(
			_n( 
				'%d post has been moved to "%s".', 
				'%d posts have been moved to "%s".', 
				$_REQUEST[ 'misha_posts_moved' ]
			),
			$_REQUEST[ 'misha_posts_moved' ], 
			$blog->blogname 
		);
		echo '</p></div>';

	}

}

Créditos: https://rudrastyh.com/wordpress-multisite/move-posts-between-blogs.html


Link de compartilhamento

Compartilhe esse conteudo nas redes sociais ou por mensagem usando o link curto abaixo. Basta clicar em cima do link para copiar.

bruno.art.br/pb/1954

ID de Referência: 1954

Sugira uma publicação

Envie uma mensagem para mim, e sugira alguma publicação de algum tema que tenha dificuldades de resolver.

Clique aqui e entre em contato


Comentários