Code duplicate post WordPress, nhân đôi bài viết

5/5 - (1 bình chọn)
Bạn có thể viết code theo ý mình để duplicate WordPress page hoặc post. Nếu bạn thích lập trình, cách này sẽ dành cho bạn. Kể cả nếu không phải, chúng tôi sẽ chỉ toàn bộ cách làm cho bạn. Bên dưới, bạn sẽ thấy code snippet để kích hoạt post duplication trong WordPress:
/*

* Function for post duplication. Dups appear as drafts. User is redirected to the edit screen

*/

function rd_duplicate_post_as_draft(){

global $wpdb;

if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'rd_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 you don't want current user to be the new post author,

* then change next couple of lines to this: $new_post_author = $post->post_author;

*/

$current_user = wp_get_current_user();

$new_post_author = $current_user->ID;

/*

* if post data exists, create the post duplicate

*/

if (isset( $post ) && $post != null) {

/*

* new post data array

*/

$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

);

/*

* 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); // returns array of taxonomy names for post type, ex 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);

}

/*

* 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_rd_duplicate_post_as_draft', 'rd_duplicate_post_as_draft' );

/*

* Add the duplicate link to action list for post_row_actions

*/

function rd_duplicate_post_link( $actions, $post ) {

if (current_user_can('edit_posts')) {

$actions['duplicate'] = '<a href="' . wp_nonce_url('admin.php?action=rd_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', 'rd_duplicate_post_link', 10, 2 );
add_filter('page_row_actions', 'rd_duplicate_post_link', 10, 2);
5/5 - (1 bình chọn)


  1. c88 login

    1. C88 Fun – Boundless Entertainment Awaits!
    C88 Fun is more than a gaming platform; it’s an expedition waiting to be embraced. Featuring an intuitive interface and a diverse array of games, C88 Fun caters to all tastes. From timeless classics to cutting-edge releases, C88 Fun ensures every player discovers their gaming haven.

    2. JILI & Evo 100% Welcome Bonus – A Grand Welcome Awaits Newcomers!
    Embark on your gaming journey with a grand welcome from C88. New members are greeted with a 100% Welcome Bonus from JILI & Evo, doubling the excitement right from the beginning. This bonus acts as a catalyst for players to explore the diverse array of games available on the platform.

    3. C88 First Deposit Get 2X Bonus – Double the Excitement!
    At C88, generosity towards players is a fundamental principle. With the “First Deposit Get 2X Bonus” offer, players can revel in double the fun on their initial deposit. This promotion enhances the gaming experience, providing more avenues to win big across various games.

    4. 20 Spin Times = Get Big Bonus (8,888P) – Spin Your Way to Greatness!
    Spin your way to substantial bonuses with the “20 Spin Times” promotion. Accumulate spins and stand a chance to win an impressive bonus of 8,888P. This promotion adds an extra layer of excitement to the gameplay, combining luck and strategy for maximum enjoyment.

    5. Daily Check-in = Turnover 5X?! – Daily Rewards Await!
    Consistency is paramount at C88. By simply logging in daily, players not only bask in the thrill of gaming but also stand a chance to multiply their turnovers by 5X. Daily check-ins bring additional perks, making every day a rewarding experience for dedicated players.

    6. 7 Day Deposit 300 = Get 1,500P – Unlock Deposit Rewards!
    For those eager to seize opportunities, the “7 Day Deposit” promotion is a game-changer. Deposit 300 and receive a generous reward of 1,500P. This promotion encourages players to explore the platform further and maximize their gaming potential.

    7. Invite 100 Users = Get 10,000 PESO – Share the Excitement!
    C88 believes in the strength of community. Invite friends and fellow gamers to join the excitement, and for every 100 users, receive an incredible reward of 10,000 PESO. Sharing the joy of gaming has never been more rewarding.

    8. C88 New Member Get 100% First Deposit Bonus – Exclusive Benefits!
    New members are in for a treat with an exclusive 100% First Deposit Bonus. C88 ensures that everyone kicks off their gaming journey with a boost, setting the stage for an exhilarating experience filled with opportunities to win.

    9. All Pass Get C88 Extra Big Bonus 1000 PESO – Unlock Unlimited Rewards!
    For avid players exploring every nook and cranny of C88, the “All Pass Get C88 Extra Big Bonus” offers an additional 1000 PESO. This promotion rewards those who embrace the full spectrum of games and features available on the platform.

    Ready to immerse yourself in the excitement? Visit C88 now and unlock a world of gaming like never before. Don’t miss out on the excitement, bonuses, and wins that await you at C88. Join the community today, and let the games begin! #c88 #c88login #c88bet #c88bonus #c88win

Trả lời

Email của bạn sẽ không được hiển thị công khai.