我們有些時(shí)候在WordPress后臺(tái)編輯文章的時(shí)候希望頁(yè)面和文章要復(fù)制后重新編輯,如果我們?cè)偈止?fù)制有點(diǎn)效率低。這里我們可以用一些插件實(shí)現(xiàn),比如Post Duplicator,這里我們直接不用插件用代碼實(shí)現(xiàn)也可以。
// 復(fù)制文章 function zm_clone_post_as_draft() { global $wpdb; if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'zm_clone_post_as_draft' == $_REQUEST['action'] ) ) ) { wp_die('No post to clone has been supplied!'); } // Nonce 驗(yàn)證 if ( !isset( $_GET['clone_nonce'] ) || !wp_verify_nonce( $_GET['clone_nonce'], basename( __FILE__ ) ) ) return; // 獲取原文章ID $post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) ); // 所有文章數(shù)據(jù) $post = get_post( $post_id ); // 如果不希望當(dāng)前用戶作為新文章作者,將下兩行替換為$new_post_author = $post->post_author; $current_user = wp_get_current_user(); $new_post_author = $current_user->ID; // 如果發(fā)布數(shù)據(jù)存在,則創(chuàng)建發(fā)布克隆 if (isset( $post ) && $post != null) { // 創(chuàng)建新文章數(shù)組 $args = array( 'comment_status' => $post->comment_status, 'ping_status' => $post->ping_status, 'post_author' => $new_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 ); // 通過(guò)wp_insert_post() 函數(shù)添加文章 $new_post_id = wp_insert_post( $args ); // 將新的文章設(shè)置為草稿 $taxonomies = get_object_taxonomies($post->post_type); // 返回文章類型的分類數(shù)組,例如:array("category", "post_tag"); 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); } // 查詢復(fù)制所有文章信息 post meta $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); } wp_redirect( admin_url( 'edit.php?post_type='.$post->post_type ) ); exit; } else { wp_die('Post creation failed, could not find original post: ' . $post_id); } } add_action( 'admin_action_zm_clone_post_as_draft', 'zm_clone_post_as_draft' ); // 復(fù)制文章并添加到列表 function zm_clone_post_link( $actions, $post ) { if (current_user_can('edit_posts')) { $actions['clone'] = '<a href="' . wp_nonce_url('admin.php?action=zm_clone_post_as_draft&post=' . $post->ID, basename(__FILE__), 'clone_nonce' ) . '" title="Clone this '.$post->post_type.'" rel="permalink">復(fù)制</a>'; } return $actions; } add_filter( 'post_row_actions', 'zm_clone_post_link', 10, 2 ); add_filter('page_row_actions', 'zm_clone_post_link', 10, 2);
這里我們丟到 functions.php 文件然后可以在文章列表可以看到復(fù)制按鈕,復(fù)制后就變成草稿。我們就可以繼續(xù)編輯文章。
評(píng)論