Created
June 25, 2025 12:30
-
-
Save plugin-republic/0fa8e9d0ed8b2c4f75f27fe4ad863825 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Programmatically create swatches fields using images that have already been uploaded to your media library | |
| */ | |
| function demo_filter_item_start_list( $item, $group, $group_id, $post_id ) { | |
| $field_id = $item['field_id']; | |
| if( $field_id != 978 ) { // Change this to the ID of your field | |
| return $item; | |
| } | |
| // These image URLs must have been uploaded to your WP media library | |
| $urls = array( | |
| 'https://yourdomain.com/wp-content/uploads/2025/06/0151.webp', | |
| 'https://yourdomain.com/wp-content/uploads/2025/06/airforce_logo.webp', | |
| 'https://yourdomain.com/wp-content/uploads/2025/06/Gunfighter_Silhouette.webp', | |
| 'https://yourdomain.com/wp-content/uploads/2025/06/navy_corpsman.webp', | |
| 'https://yourdomain.com/wp-content/uploads/2025/06/vmm-164.webp' | |
| ); | |
| if( empty( $item['field_options'] ) ) { | |
| // Create new set of options | |
| $options = array(); | |
| } else { | |
| // Add URLs to existing options | |
| $options = $item['field_options']; | |
| } | |
| foreach( $urls as $url ) { | |
| $image = attachment_url_to_postid( $url ); | |
| $value = wc_get_filename_from_url( $url ); // Create option label from file name | |
| $options[] = array( | |
| 'image' => $image, | |
| 'value' => $value, | |
| 'price' => '' | |
| ); | |
| } | |
| $item['field_options'] = $options; | |
| return $item; | |
| } | |
| add_filter( 'pewc_filter_item_start_list', 'demo_filter_item_start_list', 10, 4 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment