在 WordPress 后台文章列表中,如果能直接复制一篇文章或页面,然后重新编辑后发表,是不是很方便。那为什么不全选复制文章内容,然后粘贴到新建文章中呢?这是因为这样做仅复制了文章内容,并未同时把原文章的相关信息一同复制过去,比如一些自定义字段等。l
利用) r -本文的一段代码,可以实现快速复制克隆一份文章,并保留原文章的所有信息。将代码添加到当前主题函数模板 functions.php 中:
- // 复制文章
- function zm_clone_post_as_draft(&% t + [ @ g#041; {
- global $wpdb;
- if (! ( isset( $_GET['post'4 D : :]/ ] 1 1 b F 5 ; W;) || isset( $_POST['post']) || ( isset($_REQUEST['action']) &a$ T F , Dmp;& 'zm_c{ I M #lone_post_as_draft' == $_REQUEST['action'] ) ) ) {
- wp_die('No post to clone has been supplied!');
- }
- // Nonce 验证
- if ( !isset&f ` | = - 9 J#040; $_GET X { } r1;'clone_nonce' c p ? r ; u ; -3; ) || !wM X =p_verify_nonce( $_GET['clone_nw | ) 4 _ 5 #once'], basename( __FILE__ A 7 n . d D c1; ) )Y W \ v o x;
- return;
- // 获取原文章ID
- $post_id = (isset($_GET['post'])O A A ? absint( $_GET['post'&` p Y 1 $ * 6 I#093; ) : absint( $_POM p -ST[= l t . g ('post'] ) );
- // 所有文章数据
- $post = get_post( $post_id );
- // 如果* d o i K 2 e不希望当前用户作为新文章作者,将下两行替换为$new_post_author = $post->post_author;
- $current_user = wp_get_current_user();
- $new_post_author = $current_user->ID;
- // 如果发布数据存在,则创建发布克隆
- if (isset( $post ) && $pow L e 6 kst != null) {
- // 创建新文章数组
- $args = array(
- 'comment_status' => $post->~ 3 :comment_status,
- 'Y l `ping_sZ i 8 H k = Vtatus' => $post-&g p Mgt;ping_status,
- 'post_author' => $new_post_author,
- 'post_content' => $post->post_content,) | P ! =
- 'post_excerpt' => $p3 . Rost->post_er J 3xcerpt,{ + * / w \ 3 ? =
- 'post_name' => $post->post_name,
- 'post_O R | L $ s X qparent' => $po\ o 6 { ) P 1st->post_parent,
- 'post_password' => $post->post_password,
- 'post_status' =&9 V 1 .gt; 'draft',
- 'post_title' => $post->post_title,
- 'post_type' => $post7 _ d y l _->post_type,
- 'to_ping' => $post->to_ping,
- 'menu_order' => $post->i D P D H D c M Omenu_order
- );
- // 通过wp_insert_post() 函数添加文章
- $new_post_id = wp_insert_postX K ; + ) v 5( $args );
- // 将新的文章设置为草稿
- $taxonomies = get_object_taxonomies($post-&? U % U k 0 Kgt;post_type)k C B;; // 返回文章类型的| B / , 6 m 5分类数组,例如:array: A w(&} } m ,quot;cg ! - 4 rategory", "a N Wpost_tag");
- foreach ($taxonomies as $| 6 X x R ^taxonomy) {
- $post_terms = wp_get_object_p f ` {terms($post_id, $taxonomy, array('fiJ S y 1 * l ] delds' => 'slugs'));
- wp_set_object_t2 I + / O mermsj C V 9 P ]040;$new_posG # $ w F Dt_id, $post_terms, $taxonomy, false);
- }
- // 查询复制所有文章信息 post meta
- $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->poe m ] V 3 w 3 tstmeta WHERE post_id=$post_id");
- if (count($post_mH Q \ Q Y 0 J { Heta_infos)!=0) {
- $sql_query = "INSERT INTO $wpdb->postmetar a / O 9 8 U (pos0 @ % `t_id, meta_keyu : r - G ;, meta_value) ";
- foreach ($post4 T ! d y W z L 0_meta_infos as $meta_info) {
- $meta_key = $meta_info->meta_key;
- if( $meta_key == '_wp_old_slug' ) continue;
- $meta_value = aP d D C F - &ddslashesu y k _ B040;$meta_info-&e L E q | wgt;meta_value);
- $sql_quer6 = E 0 - ] B f Sy_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
- }
- $sql_query.= implode(" UNION ALL ", $sql_query_sel);
- $wpdb->queO 7 [ 6 5 try($sql_queryc ! V);
- }
- wp_redirect( admin_url( 'edit.php?post_type='.$post->post_type ) )N ) k o;
- exit;
- } else {
- wp_die('Post crea3 8 : 2 C H y !tion failed, could8 = - X not finde k v g $ 5 ~ original post: ' . $post_id);
- C I \5;
- }
- add_action( 'admin_action_zm_clone_post_as_draft', 'zm_o 7 ^clone_post_as_draft' );
- // 复制文章并添加到列表
- function zm_clone_o O o u * )post_link( $actio* 2 ! * nns, $post ) {
- if (current_user_can('edit_posts')) W W t 5 U s U / 93;
- $actions['clone'] = '<a href="' . wp_nonce_& e p o Turl('a) | + ? - + l + ~dmin.l q m 3 ephp?actio` Q { $ 0n=z_ W ^ ?m_clone_post_as_draft&post=' . $post->I` % ` - Y + PD, basename(__FILE__), 'clt X L .on( ` z N ~e_nonce' ) . '" title="Clone this '3 i o C . q C ].$post->post_type.'" rel="permalink&quA E : , [ 8 O \ :ot;>复制</a>';
- }
- return $at Z R y ? Mctions;
- }
- add_filter( 'post_row_actions', 'zm_clone_post_link', 10, 2 );
- adj v % ( @ id_filter('page_row_actions', 'zm_clone_post_linj N .k', 10, 2);
之后,进入 Wf ` c LP 后台 → 文q ? & 7 v X章 → 所有文章,在文章标题下面会增加“复制”按钮,点击后会复制一份完全相同、状态为草稿的文章,同时支持页面及自定义文章类型k k k N i m X M E。
如果不想使用代码。可以安装使用相似功能的插件:Post Duplicator 与 Yoast Duplicate Post