AI Verified

Name

Get Featured Image Metadata for Current Post

About

This creates a shortcode that returns the requested metadata item for the current posts' featured image. The item values that can be returned are alt, title, caption, description, or url.

Language

PHP

Rating

Voted: 0 by 0 user(s)

How to Setup Snippet

// Examples of Use: // [get_featured_image_metadata get="alt" /] --> returns the alt text for the featured image // --- // [get_featured_image_metadata get="title"] // Formatted {metadata} output. // [/get_featured_image_metadata] --> returns "Formatted image_title output."

Codevault

Jemully-Media

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.1

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

04/05/2023

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Found 0 vulnerabilities

Get Featured Image Metadata for Current Post

 
                    
1//========================================================================//
2function get_featured_image_metadata_shortcode( $atts, $content = "" ) {
3 // --------------------------------------------------------------------------------
4 // Examples of Use:
5 // [get_featured_image_metadata get="alt" /] --> returns the alt text for the featured image
6 // [get_featured_image_metadata get="title"]
7 // Format {metadata} output.
8 // [/get_featured_image_metadata] --> returns "Format image_title output."
9 // --------------------------------------------------------------------------------
10 
11 $output = "";
12 $val = "";
13 
14 // Get current post's featured image
15 $featured_image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
16 
17 // Get featured image meta data
18 $post_alt = get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true );
19 $post_title = get_the_title();
20 $post_caption = get_the_post_thumbnail_caption();
21 $post_description = get_post( get_post_thumbnail_id() )->post_content;
22 $post_file_url = $featured_image[0];
23 
24 // This grabs any content that was supplied between the shortcode tags
25 $content = trim($content); // This is the content found between shortcode tags (see example above)
26 
27 // Get which metadata is requested: alt, title, caption, desc, url
28 $get = (isset($atts["get"]) ? trim(strtolower($atts["get"])) : "");
29 
30 switch($get) {
31 case "alt": $val = $post_alt; break;
32 case "title": $val = $post_title; break;
33 case "cap":
34 case "caption": $val = $post_caption; break;
35 case "description":
36 case "desc": $val = $post_description; break;
37 case "url": $val = $post_file_url; break;
38 }
39 
40 $content = str_replace("{metadata}", $val, $content);
41 
42 $output = ($content === "" ? $val : $content);
43 
44 return $output; // Return the output to the website
45 
46 // --------------------------------------------------------------------------------
47}
48add_shortcode( 'get_featured_image_metadata', 'get_featured_image_metadata_shortcode' );
49//========================================================================//

0

Related Snippets

Please see some snippets below related to this snippet..

General

Unverified

0

Jumping Header

Added: 3 months ago

Last Updated: 3 months ago

Use this code to prevent the jumping header. See this guide https://quadlayers.com/fix-jumping-header-in-divi/#:~:text=When%20you%20apply%20changes%20to,creates%20the%20jumping%20header%20illusion....

General

AI Verified

1

Make upload filenames lowercase

Added: 1 year ago

Last Updated: 1 year ago

Ensures that the filenames of all uploaded files are converted to lowercase, for consistency.

General

AI Verified

0

Search results will show only blog posts, not pages

Added: 6 months ago

Last Updated: 4 months ago

This PHP code snippet is a custom search filter for WordPress designed to modify the default search behavior in the front end, ensuring that it only provides results for blog posts.

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

General

AI Verified

0

Get Featured Image Metadata for Current Post

Added: 1 year ago

Last Updated: 1 year ago

This creates a shortcode that returns the requested metadata item for the current posts' featured image. The item values that can be returned are alt, title, caption, description, or url.