gift_slide_bk_all
Mon Feb 12 2024 17:25:03 GMT+0000 (Coordinated Universal Time)
Saved by @FOrestNAtion
<?php
/*gift_slide_bk_all*/
function gift_slide_bkg_all($gift_slide) {
$ua = strtolower($_SERVER["HTTP_USER_AGENT"]);
$isMob = is_numeric(strpos($ua, "mobile"));
if ($isMob != 'mobile') {
// Determine which background field to check based on the $gift_slide value
if ($gift_slide == 'one') { $slideBackground = 'slide1background'; }
if ($gift_slide == 'two') { $slideBackground = 'slide2background'; }
if ($gift_slide == 'three') { $slideBackground = 'slide3background'; }
if ($gift_slide == 'four') { $slideBackground = 'slide4background'; }
$background = get_field($slideBackground, get_the_ID());
if (!empty($background)) {
// Apply Cloudinary transformations to the background URL
if (strpos($background, "cloudinary.com") !== false) {
// Extract the URL parts to insert transformations
$parts = explode('/upload/', $background);
if (count($parts) == 2) {
// Ensure the URL ends with .jpg and apply transformations including blur
$transformedUrl = $parts[0] . '/upload/f_auto,q_auto:eco,e_blur:5000/' . preg_replace('/\.\w+$/', '.jpg', $parts[1]);
return $transformedUrl;
}
}
return $background; // Return the original URL if slideBackfield url field is empty
} else {
if ($gift_slide == 'one') { $gift_slide_number = 'slide1_type'; }
if ($gift_slide == 'two') { $gift_slide_number = 'slide2_type'; }
if ($gift_slide == 'three') { $gift_slide_number = 'slide3_type'; }
if ($gift_slide == 'four') { $gift_slide_number = 'slide4_type'; }
$slidetype = get_field($gift_slide_number, get_the_ID());
if ($slidetype == 'photo editor') {
if ($gift_slide == 'one') { $gift_field = 'gift_image'; }
if ($gift_slide == 'two') { $gift_field = 'gift_image_2'; }
if ($gift_slide == 'three') { $gift_field = 'gift_image_3'; }
if ($gift_slide == 'four') { $gift_field = 'gift_image_4'; }
$img_id = get_field($gift_field, get_the_ID());
if (!empty($img_id)) {
$media_url = wp_get_attachment_image_url($img_id, 'full');
if (!empty($media_url)) {
return $media_url;
}
}
} else {
if ($gift_slide == 'one') { $gift_field = 'gif1'; }
if ($gift_slide == 'two') { $gift_field = 'gif2'; }
if ($gift_slide == 'three') { $gift_field = 'gif3'; }
if ($gift_slide == 'four') { $gift_field = 'gif4'; }
$media_id = get_field($gift_field, get_the_ID());
if (!empty($media_id) && is_numeric($media_id)) {
// Try fetching from the _cloudinary meta field
$cloudinary_data = get_post_meta($media_id, '_cloudinary', true);
if (isset($cloudinary_data['_cloudinary_url'])) {
$media_url = $cloudinary_data['_cloudinary_url'];
// Check if the media URL is from Cloudinary
if (strpos($media_url, "cloudinary.com") !== false) {
// Insert transformations for all media types
$parts = explode('/upload/', $media_url);
$media_url = $parts[0]. '/upload/f_auto,q_auto,so_2,e_blur:5000/'.$parts[1];
}
} else {
$media_url = wp_get_attachment_url($media_id);
}
$filetype = wp_check_filetype($media_url);
if ($filetype['ext'] == 'mp4') {
if (strpos($media_url, "cloudinary.com") !== false) {
$parts = explode('/upload/', $media_url);
$transformed_media_url = $parts[0]. '/upload/f_auto,q_auto,so_2,e_blur:5000/'.$parts[1];
$transformed_media_url = str_replace(".mp4", ".jpg", $transformed_media_url);
return $transformed_media_url;
}
} else {
return $media_url;
}
}
if (!empty($media_id) && !is_numeric($media_id)) {
$media_url = $media_id;
$filetype = wp_check_filetype($media_id);
if ($filetype['ext'] == 'mp4') {
$media_name = basename(get_attached_file($media_id));
$media_name_jpg = str_replace(".mp4", ".jpg", "$media_name");
return "https://res.cloudinary.com/drt2tlz1j/video/upload/f_auto,q_auto,so_2,e_blur:5000/fn/$media_name_jpg";
} else {
return $media_url;
}
}
if (empty($media_id)) {
return 'https://res.cloudinary.com/drt2tlz1j/images/f_auto,q_auto:eco,e_blur:5000/v1700932277/fn/fallback-GS/fallback-GS.jpg?_i=AA';
}
}
}
}
}
// register shortcode
add_shortcode('gift_slide_bkg_all', 'gift_slide_bkg_all');



Comments