I'm creating functionality that will clone a published page and create a new draft page from the content. I placed an action link alongside the "Edit | Quick Edit | Trash | View" items that appear beneath each page title on the page listing page within the admin dash. My link has the post_id and the custom action name. The link points back to the edit.php page and a function is run if the action matches clone-post_{post_id}. I get the following error screen each time I click the link. However, the clone functionality does work. When I go back to the page listings page the draft page exists with the appropriate content.
add_filter('post_row_actions', 'update_page_listing_menu', 10, 2);
add_filter('page_row_actions', 'update_page_listing_menu', 10, 2);
function update_page_listing_menu($actions, WP_Post $post)
{
$site_url = site_url();
$base_url = $site_url .'/wp-admin/edit.php?post_type=page&post=' . $post->ID . '&action=clone-post_'. $post->ID;
$actions['clone-post'] = '<a href="'. $base_url . '">Copy to Draft</a>';
return $actions;
}
// If copy to draft is true in the query vars then copy the post
add_action('wp_loaded','check_for_clone_action');
function check_for_clone_action() {
if ( strpos($_REQUEST['action'],'clone-post_' ) !== false )
create_draft_post_from_published_post($_REQUEST['post']);
}
function create_draft_post_from_published_post($id)
{
$current_post = get_post( $id );
$new_post_id = wp_insert_post( array(
'post_title' => $current_post->post_title,
'post_content' => $current_post->post_content,
'meta_input' => array(
'published_post_id' => $id
),
'post_type' => 'page'
), true );
}
FYI: I will add nonce verification once I get this working.
Below is the page that I see when clicking the link: