setImageProperty( 'exif:UserComment', $marker ); $img->writeImage( $path ); $img->clear(); $img->destroy(); } catch ( Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf // Silently ignore and continue to IPTC. } } // 2) Also write IPTC (works with JPEGs). $iptc = array( '2#116' => $marker, // Caption/Abstract. '2#110' => get_bloginfo( 'name' ), // Credit. '2#115' => home_url(), // Source. '2#085' => get_bloginfo( 'name' ), // By-line. '2#090' => home_url(), // City (reused as URL anchor). ); $data = ''; foreach ( $iptc as $tag => $str ) { $tag_num = (int) substr( $tag, 2 ); $str = (string) $str; $rec = chr( 0x1C ); $rec .= chr( 2 ); $rec .= chr( $tag_num ); $rec .= chr( strlen( $str ) ); $rec .= $str; $data .= $rec; } // Suppress warnings from iptcembed/file_put_contents but only write if successful. $content = @iptcembed( $data, $path, 0 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged if ( false !== $content ) { require_once ABSPATH . 'wp-admin/includes/file.php'; WP_Filesystem(); global $wp_filesystem; if ( $wp_filesystem ) { $wp_filesystem->put_contents( $path, $content, FS_CHMOD_FILE ); } } } }