Hi,
I want to RENEW / REPURCHASE the WP MEDIA folder but i had one request specifically that i am looking for.
When you go to an image in media folder, with your plugin a new field appears that says Image Gallery Link to: and the link you put there is the link that the image will link to in the WP FOLDER gallery. However I want that link to also work when you insert that media file into a post or page. The link attached to it, should always be the link it links to, not only in the folder.
YOUR AI ASSISTANT on your site actually gave me snippet code to create this. I was so excited but unfortunately it didnt work this is the code it gave me. But something like this within your plugin would be amazing. Please let me know.
add_filter('the_content', 'custom_image_link');
function custom_image_link($content) {
// Use regex to find all images in the content
$pattern = '/]+src=["\']([^"\']+)["\'][^>]*>/i';
// Callback function to replace the image tag with a linked image
$content = preg_replace_callback($pattern, function($matches) {
$image_url = $matches[1]; // Get the image URL
$image_id = attachment_url_to_postid($image_url); // Get the image ID
// Get the custom field value for the image
$image_link = get_post_meta($image_id, 'Image_gallery_link_to', true);
// If a link exists, wrap the image in an anchor tag
if ($image_link) {
return '';
}
// Return the original image if no link exists
return $matches[0];
}, $content);
return $content;
}
I want to RENEW / REPURCHASE the WP MEDIA folder but i had one request specifically that i am looking for.
When you go to an image in media folder, with your plugin a new field appears that says Image Gallery Link to: and the link you put there is the link that the image will link to in the WP FOLDER gallery. However I want that link to also work when you insert that media file into a post or page. The link attached to it, should always be the link it links to, not only in the folder.
YOUR AI ASSISTANT on your site actually gave me snippet code to create this. I was so excited but unfortunately it didnt work this is the code it gave me. But something like this within your plugin would be amazing. Please let me know.
add_filter('the_content', 'custom_image_link');
function custom_image_link($content) {
// Use regex to find all images in the content
$pattern = '/]+src=["\']([^"\']+)["\'][^>]*>/i';
// Callback function to replace the image tag with a linked image
$content = preg_replace_callback($pattern, function($matches) {
$image_url = $matches[1]; // Get the image URL
$image_id = attachment_url_to_postid($image_url); // Get the image ID
// Get the custom field value for the image
$image_link = get_post_meta($image_id, 'Image_gallery_link_to', true);
// If a link exists, wrap the image in an anchor tag
if ($image_link) {
return '';
}
// Return the original image if no link exists
return $matches[0];
}, $content);
return $content;
}
- Page :
- 1
There are no replies made for this post yet.
