如何不用插件复制WordPress的帖子、页面和自定义帖子

作为一个管理员、编辑甚至开发者,能够在你的网站上一键复制WordPress帖子、页面和其他帖子类型真的很有用。此外,你不应该每次都需要重新输入标签、类别、元字段和其他相关选项来发布。

在这篇博文中,我们将在你的网站上添加一个小的代码段,这样你将能够通过一次点击来复制任何帖子类型。这个方法适用于所有WordPress主题。

在每个帖子上添加一个链接按钮来复制它

要复制WordPress的帖子,首先让我们在每个帖子标题下添加一个链接按钮,这样当它被点击时,帖子就会被复制。
进入你的主题的function.php文件,在它的末尾添加以下代码。

add_filter( 'post_row_actions', 'jupiterx_add_duplicate_post_button', 10, 2 );
 
function jupiterx_add_duplicate_post_button( $actions, $post ) {
    $args = [
        'jupiterx-to-duplicate' => $post->ID,
        'jupiterx-duplicate-posts' => wp_create_nonce( 'jupiterx-duplicate-nonce' ),
    ];
 
    if ( 'post' !== $post->post_type ) {
        $args['post_type'] = $post->post_type;
    }
 
    $url  = add_query_arg( $args, admin_url( 'edit.php' ) );
 
    $actions['duplicate_post'] = "<a href='{$url}'>Duplicate</a>";
    return $actions;
}

通过添加这段代码,如果你导航到编辑帖子界面,你会在每个帖子下看到以下按钮。

Screenshot 1

如果你现在点击这个按钮,页面将简单地重新加载。
在下一节中,我们将为这个按钮添加功能,以便它能复制相关的帖子。

为按钮添加功能

点击按钮后,我们会得到帖子的信息,包括标签、类别、元字段和属于该帖子的其他一切。然后我们将使用这些信息创建一个新的帖子,完成这一过程。
在你的主题中的function.php文件末尾复制并添加以下代码,也就是你之前添加代码段的地方。

add_action( 'admin_init', 'jupiterx_create_duplicate_button_functionality' );
 
function jupiterx_create_duplicate_button_functionality() {
    if ( ! isset( $_GET['jupiterx-duplicate-posts'] ) ) {
        return;
    }
 
    check_ajax_referer( 'jupiterx-duplicate-nonce', 'jupiterx-duplicate-posts', true );
 
    $post_id = absint( $_GET['jupiterx-to-duplicate'] );
 
    $post = get_post( $post_id );
 
    if ( ! $post ) {
        wp_die( 'We could not find any post to duplicate it.' );
    }
 
    // Create post array exactly like post that was clicked.
    $args = [
        'comment_status' => $post->comment_status,
        'ping_status'    => $post->ping_status,
        'post_author'    => wp_get_current_user()->ID,
        '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 . ' Duplicated',
        'post_type'      => $post->post_type,
        'to_ping'        => $post->to_ping,
        'menu_order'     => $post->menu_order
    ];
 
    $duplicated_post = wp_insert_post( $args );
 
    // Copy taxonomies.
    $taxonomies = get_object_taxonomies( get_post_type( $post ) );
    if( $taxonomies ) {
        foreach ( $taxonomies as $taxonomy ) {
            $post_terms = wp_get_object_terms( $post_id, $taxonomy, [ 'fields' => 'slugs' ] );
            wp_set_object_terms( $duplicated_post, $post_terms, $taxonomy, false );
        }
    }
 
    // Copy meta fields.
    $post_meta = get_post_custom( $post_id );
    if( $post_meta ) {
        foreach ( $post_meta as $meta_key => $meta_values ) {
            update_post_meta( $duplicated_post, $meta_key,  $meta_values[0] );
        }
    }
 
    $args = [];
 
    if ( 'post' !== $post->post_type ) {
        $args['post_type'] = $post->post_type;
    }
 
    $url  = add_query_arg( $args, admin_url( 'edit.php' ) );
 
    wp_safe_redirect( $url );
    exit;
}

现在,如果你按下复制链接,相关的帖子将被复制,并在其标题上添加 “Duplicated “一词,这样你就会知道哪一个是原始帖子。

在下面的图片中,你可以看到帖子已经被复制了。

Screenshot 2

现在我们已经成功地为帖子添加了复制帖子的功能。但是,其他的帖子类型,如 “页面”‘、”产品 “等,又该如何处理?

让我们在第3步解决这个问题。

为任何帖子类型添加复制功能


要把这个功能添加到任何一种帖子类型中,你需要知道帖子类型的slug。例如,WooCommerce产品的帖子类型是 “product”。

在你了解了帖子类型的slug后,你需要将这行代码添加到你已经添加到function.php文件的第一个代码段中。

为了给帖子添加这个功能,我们添加了以下代码。

add_filter( 'post_row_actions', 'jupiterx_add_duplicate_post_button', 10, 2 );

为了将其添加到页面,我们只需要添加以下一行,因为我们已经知道页面的slug是 “page”。

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

这样页面也拥有了复制的功能

分享你的喜爱

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注