Error verifying Nonce
Question
I’m trying to create an action that clones a published page to a new draft page. Everything is working except the nonce verification. When I click on the link (a custom link that I placed alongside the edit | Quick Edit | Trash | View links that are located beneath each page listed on the page listings page) I get the message “The link you followed has expired. Please try again.”
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;
$complete_url = wp_nonce_url($base_url,'clone-post_'. $post->ID,'cp_nonce');
$actions['clone-post'] = '<a href="'. $complete_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)
{
$nonce_verified = wp_verify_nonce($_REQUEST['cp_nonce'],'clone-post_'. $id);
if ($nonce_verified) {
$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 );
}
}
0
nonce, plugin-development
4 years
2019-11-06T08:33:31-05:00
2019-11-06T08:33:31-05:00 0 Answers
63 views
0
Leave an answer