I have to create og image url from featured image because along with featured image i have to send text from post title. i have created custom field in editor named as tnation_og_image and i have to set image url in it along with text. Sharing the code but it is not working “`function generate_custom_og_image() {
global $post;
// Check if the post has a featured image
if (has_post_thumbnail($post->ID)) {
$thumbnail_id = get_post_thumbnail_id($post->ID);
$thumbnail = wp_get_attachment_image_src($thumbnail_id, 'large'); // You can change 'large' to other image sizes if needed
// Create a new image from the featured image
$image_path = $thumbnail[0];
echo $image = imagecreatefromjpeg($image_path); // Change function based on image type (jpeg, png, etc.)
// Set text color and font size
$text_color = imagecolorallocate($image, 255, 255, 255); // White color
$font_size = 20; // Font size for post title and author name
$font_path = 'https://xyz.com/wp-content/themes/the-2023/inc/fonts/ostrich-regular.ttf'; // Specify the font file name
// Add post title on the image
$post_title = "Your text here";
//$post_title = get_the_title($post->ID);
imagettftext($image, $font_size, 0, 50, 50, $text_color, $font_path, $post_title); // Adjust coordinates as needed
// Add author name on the image
//$author_name = get_the_author_meta('display_name', $post->post_author);
//imagettftext($image, $font_size, 0, 50, 100, $text_color, $font_path, 'By ' . $author_name); // Adjust coordinates as needed
$savePath = 'https://xyz/wp-content/uploads/2023/08/converted.jpg';
// Save the modified image
imagejpeg($image, $savePath); // Change function based on image type (jpeg, png, etc.)
// Free up memory
imagedestroy($image);
// Check if the file was created successfully
if (file_exists($savePath)) {
// Get the image content
echo $imageContent = file_get_contents($savePath);
// Update post meta with the image content
update_post_meta($post->ID, 'tnation_og_image', $imageContent);
// Delete the temporary image file
unlink($savePath);
}
die();
}
}
// Hook the function to wp_head action
add_action(‘save_post’, ‘generate_custom_og_image’);“`