ID;
$post_title = get_the_title( $post_id );
$post_url = get_permalink( $post_id );
$post_excerpt = $this->get_post_excerpt( $post );
$site_name = get_bloginfo( 'name' );
$site_url = home_url();
// Get the image URL (featured image or first image in content)
$image_url = $this->get_post_image_url( $post_id );
// Basic Open Graph tags
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
// Description
if ( $post_excerpt ) {
echo '' . "\n";
}
// Image
if ( $image_url ) {
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
}
// Article specific tags
echo '' . "\n";
echo '' . "\n";
echo '' . "\n";
// Add post tags as article:tag
$tags = get_the_tags( $post_id );
if ( $tags && ! is_wp_error( $tags ) ) {
foreach ( $tags as $tag ) {
echo '' . "\n";
}
}
// Twitter Card tags for better Twitter sharing
echo '' . "\n";
echo '' . "\n";
if ( $post_excerpt ) {
echo '' . "\n";
}
if ( $image_url ) {
echo '' . "\n";
}
// Additional meta tags for better SEO
echo '' . "\n";
}
/**
* Get post excerpt or generate one from content.
*
* @param WP_Post $post The post object.
* @return string The excerpt.
*/
private function get_post_excerpt( $post ) {
// Use post excerpt if available
if ( ! empty( $post->post_excerpt ) ) {
return wp_trim_words( $post->post_excerpt, 30, '...' );
}
// Generate excerpt from content
$content = strip_shortcodes( $post->post_content );
$content = wp_strip_all_tags( $content );
$content = wp_trim_words( $content, 30, '...' );
return $content;
}
/**
* Get the best image URL for the post.
*
* @param int $post_id The post ID.
* @return string|false The image URL or false if no image found.
*/
private function get_post_image_url( $post_id ) {
// First, try to get the featured image
if ( has_post_thumbnail( $post_id ) ) {
$thumbnail_id = get_post_thumbnail_id( $post_id );
$image_url = wp_get_attachment_image_url( $thumbnail_id, 'large' );
if ( $image_url ) {
return $image_url;
}
}
// If no featured image, look for the first image in the post content
$post = get_post( $post_id );
if ( ! $post ) {
return false;
}
$content = $post->post_content;
// Look for images in the content
preg_match_all( '/
]+src="([^"]+)"[^>]*>/i', $content, $matches );
if ( ! empty( $matches[1] ) ) {
$first_image_url = $matches[1][0];
// Convert relative URLs to absolute URLs
if ( strpos( $first_image_url, 'http' ) !== 0 ) {
$first_image_url = home_url( $first_image_url );
}
return $first_image_url;
}
// Look for images in shortcodes (like galleries)
preg_match_all( '/\[gallery[^\]]*ids="([^"]+)"[^\]]*\]/i', $content, $gallery_matches );
if ( ! empty( $gallery_matches[1] ) ) {
$image_ids = explode( ',', $gallery_matches[1][0] );
$first_image_id = trim( $image_ids[0] );
if ( $first_image_id ) {
$image_url = wp_get_attachment_image_url( $first_image_id, 'large' );
if ( $image_url ) {
return $image_url;
}
}
}
// Fallback: try to get the site logo or a default image
$custom_logo_id = get_theme_mod( 'custom_logo' );
if ( $custom_logo_id ) {
$logo_url = wp_get_attachment_image_url( $custom_logo_id, 'full' );
if ( $logo_url ) {
return $logo_url;
}
}
// Last resort: return false (no image found)
return false;
}
}