'admin-screenshots-button', 'title' => 'SCREENSHOT THIS PAGE', 'href' => '#' ); $wp_admin_bar->add_node($args); } } add_action('admin_bar_menu', 'admin_screenshots_toolbar_button', 999); /* --- THE FUNCTION THAT CREATES A CANVAS AND SAVES IT AS AN IMAGE -------------------------------------------------------------- */ if (!function_exists('admin_screenshots_save_canvas')) { function admin_screenshots_save_canvas() { if ( isset( $_POST['image'] ) ) { $upload_dir = wp_upload_dir(); // Array $upload_dir_path = $upload_dir['path']; // /serverstuff/app/public/wp-content/uploads $upload_dir_base = $upload_dir['basedir']; // /serverstuff/app/public/wp-content/uploads $upload_path = $upload_dir['basedir'] . '/admin-screenshots'; // /serverstuff/app/public/wp-content/uploads/admin-screenshots $upload_dir_url = $upload_dir['baseurl'] . '/admin-screenshots'; // http://mysite.local/wp-content/uploads if (!file_exists($upload_path)) { wp_mkdir_p($upload_path); } $image_data = sanitize_text_field($_POST['image']); $timestamp = time(); $img = $timestamp . '.png'; $file = $upload_path . '/' . $img; $fullpath = $upload_dir['url'] . '/admin-screenshots/' . $img; $success = file_put_contents( $file, base64_decode( str_replace( 'data:image/png;base64,', '', $image_data ) ) ); if ( $success ) { $wp_filetype = wp_check_filetype( $img, null ); $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => 'Screenshot '.$timestamp.' (generated by Admin Screenshots)', 'post_content' => '', 'post_status' => 'inherit' ); $attachment_id = wp_insert_attachment( $attachment, $file ); require_once( ABSPATH . 'wp-admin/includes/image.php' ); $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file ); wp_update_attachment_metadata( $attachment_id, $attachment_data ); echo $upload_dir_url . ',' . $timestamp; // Sending this to the JS function in screenshotThis() } else { _e('Failed to save image.', 'admin-screenshots'); } } wp_die(); } } add_action( 'wp_ajax_save_canvas', 'admin_screenshots_save_canvas' ); add_action( 'wp_ajax_nopriv_save_canvas', 'admin_screenshots_save_canvas' );