plugin_path . '/assets/images/placeholder.png'; $filename = $upload_dir['basedir'] . '/visual-portfolio/placeholder.png'; // try to move to /visual-portfolio/ directory. if ( ! file_exists( $upload_dir['basedir'] . '/visual-portfolio' ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged @mkdir( $upload_dir['basedir'] . '/visual-portfolio', 0755, true ); } if ( ! file_exists( $upload_dir['basedir'] . '/visual-portfolio' ) ) { $filename = $upload_dir['basedir'] . '/visual-portfolio-placeholder.png'; } if ( ! file_exists( $filename ) ) { copy( $source, $filename ); } if ( ! file_exists( $filename ) ) { return; } $filetype = wp_check_filetype( basename( $filename ), null ); $attachment = array( 'guid' => $upload_dir['url'] . '/' . basename( $filename ), 'post_mime_type' => $filetype['type'], 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), 'post_content' => '', 'post_status' => 'inherit', ); $attach_id = wp_insert_attachment( $attachment, $filename ); // Update settings. $general_settings['no_image'] = $attach_id; update_option( 'vp_general', $general_settings ); // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. require_once ABSPATH . 'wp-admin/includes/image.php'; // Generate the metadata for the attachment, and update the database record. $attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); wp_update_attachment_metadata( $attach_id, $attach_data ); } } new Visual_Portfolio_Image_Placeholder();