需求起因

基于 A/B 测试发展的需要,我们需要在原来的基础上复制一个一样的文章版本,在其基础上进行变体内容、图片、布局等元素的修改,以确保 A/B 测试的目标元素为唯一变量,避免测试结果被其他非目标元素差异混淆。

而 WordPress 提供的只是单击复制内容功能 (见下图),其它一些配置如分类和标签等需要手动设置,这很容易产生一些人为的失误和遗漏,所以在后台添加一个克隆的功能将大大提高效率的同时最大化减少人为造成的失误。


如何在 WordPress 后台添加单击复制文章和页面功能

1) 找到你主题下的 functions.php 文件

2) 复制下面的代码段,粘贴到文件末尾,保存即可

/*
 * Function for post duplication. Dups appear as drafts.
 *
*/
function aimeesign_duplicate_post_as_draft(){
	global $wpdb;
	if (! ( isset( $_GET['post']) || isset( $_POST['post'])  || ( isset($_REQUEST['action']) && 'aimeesign_duplicate_post_as_draft' == $_REQUEST['action'] ) ) ) {
		wp_die('No post to duplicate has been supplied!');
	}
    // Nonce verification
    if ( !isset( $_GET['duplicate_nonce'] ) || !wp_verify_nonce( $_GET['duplicate_nonce'], basename( __FILE__ ) ) )
		return;
    // Get the original post id
    $post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
    // And all the original post data then
    $post = get_post( $post_id );
    // If post data exists, create the post duplicate
    if (isset( $post ) && $post != null) {
		$args = array(
			'comment_status' => $post->comment_status,
			'ping_status'    => $post->ping_status,
			'post_author'    => $post->post_author,
			'post_content'   => $post->post_content,
			'post_excerpt'   => $post->post_excerpt,
			'post_name'      => $post->post_name,
			'post_parent'    => $post->post_parent,
			'post_password'  => $post->post_password,
			'post_status'    => 'draft',
			'post_title'     => $post->post_title,
			'post_type'      => $post->post_type,
			'to_ping'        => $post->to_ping,
			'menu_order'     => $post->menu_order
		);
        // Insert the post by wp_insert_post() function
	$new_post_id = wp_insert_post( $args );
        // Get all current post terms ad set them to the new post draft
	$taxonomies = get_object_taxonomies($post->post_type); 
		foreach ($taxonomies as $taxonomy) {
			$post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
			wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
		}
        // Duplicate all post meta just in two SQL queries
	$post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
		if (count($post_meta_infos)!=0) {
			$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
			foreach ($post_meta_infos as $meta_info) {
				$meta_key = $meta_info->meta_key;
				if( $meta_key == '_wp_old_slug' ) continue;
				$meta_value = addslashes($meta_info->meta_value);
				$sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
			}
			$sql_query.= implode(" UNION ALL ", $sql_query_sel);
			$wpdb->query($sql_query);
		}
        // Finally, redirect to the edit post screen for the new draft
	wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
		exit;
	} else {
		wp_die('Post creation failed, could not find original post: ' . $post_id);
	}
}
add_action( 'admin_action_aimeesign_duplicate_post_as_draft', 'aimeesign_duplicate_post_as_draft' );

// Add the duplicate link to action list for post_row_actions
function aimeesign_duplicate_post_link( $actions, $post ) {
	if (current_user_can('edit_posts')) {
		$actions['duplicate'] = '<a href="' . wp_nonce_url('admin.php?action=aimeesign_duplicate_post_as_draft&post=' . $post->ID, basename(__FILE__), 'duplicate_nonce' ) . '" title="Duplicate this item" rel="permalink">Duplicate</a>';
	}
	return $actions;
}
add_filter( 'post_row_actions', 'aimeesign_duplicate_post_link', 10, 2 );

此段代码复制的内容包括:

  • Title
  • All the content
  • Permalink (发布后会自动在链接后面添加 -2)
  • Author
  • Categories
  • Tags
  • Featured image
  • Excerpt

如果还有需要复制 Page 页面,则需在最后面再添加一句下方的代码段。

add_filter('page_row_actions', 'aimeesign_duplicate_post_link', 10, 2);

3) 登录 WordPress 后台并强制刷新页面,你将会看到文章下方出现 Duplicate 字段, 单击即可复制一篇一样的文章并保存为草稿状态。