Created
January 7, 2022 15:43
-
-
Save About2git/24c5ce61421da19824c9b7d6dbab4da2 to your computer and use it in GitHub Desktop.
This file contains 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 | |
function gallery_ids_in_string($post_id, $att_id, $filepath, $is_keep_existing_images = '') | |
{ | |
// The custom field used by gallery. | |
$key = 'energieausweis'; | |
// The separator to use between each ID. | |
$separator = explode(PHP_EOL); | |
// Retrieve the current values in the gallery field. | |
$gallery = get_post_meta($post_id, $key, true); | |
// Ensure gallery is valid. | |
if (is_string($gallery) || is_empty($gallery) || ($gallery == false)) { | |
// Split value into array. | |
$gallery = explode($separator, $gallery); | |
// Add image if it's not in the gallery. | |
if (!in_array($att_id, $gallery)) { | |
// Ensure array doesn't start with empty value. | |
if ($gallery[0] == '') unset($gallery[0]); | |
// Add image ID to array. | |
$gallery[] = $att_id; | |
// Save updated gallery field. | |
update_post_meta($post_id, $key, implode($separator, $gallery)); | |
} | |
} | |
} | |
add_action('pmxi_gallery_image', 'gallery_ids_in_string', 10, 4); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment