I am working on a 2 part form to allow users to create posts on WordPress.
The first part takes in the post title, content, and link, then creates the post from that data using AJAX. When the post is created using wp_insert_post() the post ID is returned.
function makeblog_page2form (){
global $make_post_id;
if( check_admin_referer('page2form_submit','page2form_subform') ){
// more code
$make_post_id = wp_insert_post( $post );
exit;
} else{
wp_redirect( home_url( '/submission-error/' ) );
exit;
}
}
The second part takes an image and uploads it the WordPress gallery and then attaches the image to the post to be it’s featured image. For the image submission I am using Frontend Uploader, and using the included add_action('fu_after_upload', callback_function) with this plugin.
I am running into issues when I try to use the $make_post_id global within the add_action() to attach the image, nothing is returned.
add_action( 'fu_after_upload', function( $attachment_ids ) {
global $make_post_id;
var_dump($make_post_id); // no return value
} );
Both of these are within the same file.php.
If this isn’t clear enough let me know. Thanks.
Try passing this variable to the anonymous function scope:Global variables does not preserve values between separate request, so you have either return post ID form first call and pass it alongside uploaded files or store and access it in
$_SESSIONarray.