]*)>/'; $replacement = ''; $content = preg_replace_callback($pattern, 'replace_image_with_download_button', $content); return $content; } function replace_image_with_download_button($matches) { $image_tag = $matches[0]; $image_attributes = $matches[1]; $image_src = get_image_src_from_attributes($image_attributes); $download_url = generate_download_url($image_src); // Update the download button link to force the browser to download the file $replacement = str_replace('[DOWNLOAD_URL]', $download_url, $image_tag . ''); return $replacement; } function get_image_src_from_attributes($image_attributes) { preg_match('/src=["\'](.*?)["\']/', $image_attributes, $matches); return isset($matches[1]) ? $matches[1] : ''; } add_filter('the_content', 'add_download_button_to_images'); ?>