Last active
December 23, 2024 20:50
-
-
Save thomasbeutel/48a30df2b4d241292b5168c623b06974 to your computer and use it in GitHub Desktop.
WordPress plugin to add og:description and og:image for the Made by Minimal theme
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Plugin Name: OG Metadata (Gallery Description + Featured Image) | |
| * Description: This plugin automatically adds both og:description (using the "gallery_description" meta field first) and og:image (using the featured image if available) to all WordPress pages and posts. You can check the OG data at https://www.opengraph.xyz/ | |
| * Version: 1.4 | |
| * Author: Thomas Beutel | |
| * License: GPL2 | |
| */ | |
| /** | |
| * DISCLAIMER: This script is provided "as is" without any warranties, | |
| * expressed or implied, including but not limited to the implied | |
| * warranties of merchantability or fitness for a particular purpose. | |
| * Use at your own risk. The author assumes no responsibility for | |
| * errors, omissions, or damages resulting from its use. | |
| */ | |
| /** | |
| * add_og_metadata | |
| * | |
| * Adds Open Graph meta tags for description and featured image (if available) for the Made by Minimal theme: https://madebyminimal.com/made | |
| * 1) Uses the "gallery_description" post meta field first for og:description. | |
| * 2) Falls back to excerpt or post content if no gallery_description is found. | |
| * 3) Uses the featured image (thumbnail) or _og_image_id for og:image if set. | |
| * | |
| * @return void | |
| */ | |
| function add_og_metadata() { | |
| if ( is_singular() ) { | |
| global $post; | |
| // Prepare the og:description | |
| $post_description = ''; | |
| $gallery_description = get_post_meta( $post->ID, 'gallery_description', true ); | |
| if ( ! empty( $gallery_description ) ) { | |
| $post_description = wp_trim_words( strip_tags( $gallery_description ), 30 ); | |
| } elseif ( has_excerpt( $post->ID ) ) { | |
| $post_description = strip_tags( get_the_excerpt( $post->ID ) ); | |
| } else { | |
| $post_content = strip_tags( apply_filters( 'the_content', $post->post_content ) ); | |
| $post_description = wp_trim_words( $post_content, 30 ); | |
| } | |
| // Output the og:description tag | |
| echo "\n".'<meta property="og:description" content="' . esc_attr( $post_description ) . '" />' . "\n"; | |
| // Prepare and output the og:image | |
| $featured_image_url = get_the_post_thumbnail_url( $post->ID, 'full' ); | |
| if ( empty( $featured_image_url ) ) { | |
| // Use og_image as fallback if featured image is not available | |
| $og_image_id = get_post_meta( $post->ID, '_og_image_id', true ); | |
| if ( $og_image_id ) { | |
| $featured_image_url = wp_get_attachment_url( $og_image_id ); | |
| } | |
| } | |
| if ( ! empty( $featured_image_url ) ) { | |
| echo '<meta property="og:image" content="' . esc_url( $featured_image_url ) . '" />' . "\n"; | |
| } | |
| } | |
| } | |
| // Hook into wp_head to include the meta tags in the <head> section. | |
| add_action( 'wp_head', 'add_og_metadata' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment