Untitled

 avatar
unknown
plain_text
02/19/2025 12:12 PM
1.9 KB
3
No Index
function custom_internal_embed_handler($html, $url, $attr, $post_id) {
    // Get the site URL to check if the link is internal
    $site_url = get_site_url();

    // Check if the URL belongs to this site
    if (strpos($url, $site_url) !== false) {
        $linked_post_id = url_to_postid($url); // Get post ID from URL

        if ($linked_post_id) {
            $title = esc_html(get_the_title($linked_post_id)); // Get title
            $image = get_the_post_thumbnail_url($linked_post_id, 'medium'); // Get featured image

            if (!$image) {
                $image = 'https://eurocom.bg/wp-content/uploads/2024/07/no-image.png'; // Fallback image
            }

            // Generate the custom embed HTML
            return '
            <div class="custom-internal-embed">
                <a href="' . esc_url($url) . '" class="custom-internal-embed-link" target="_blank" rel="noopener noreferrer">
                    <picture class="custom-internal-embed-image">
                        <img src="' . esc_url($image) . '" alt="' . esc_attr($title) . '">
                    </picture>
                    <span class="custom-internal-embed-title">' . $title . '</span>
                </a>
            </div>';
        }
    }

    // Return the original embed for external links
    return $html;
}

// Apply filter to override WordPress' default embed handling for internal links
add_filter('embed_oembed_html', 'custom_internal_embed_handler', 10, 4);
Editor is loading...
Leave a Comment