Skip Navigation

[Resolved] Add featured image url, alt text and description shortcodes

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 8 replies, has 6 voices.

Last updated by morktron 5 years, 10 months ago.

Assigned support staff: Caridad.

Author
Posts
#38290

Tim

Hi there,

I noticed that there was little support for displaying a featured image url, caption, alt text or description. There are heaps of plugins which use featured images, there are also theme's which automatically inset them into posts. So in my limited abilities I added some code to provide this functionality to the "wp-viewsembeddedincwpv-shortcodes.php" file in the WordPress plugins folder.

It is working perfectly for my purposes, but I have a few concerns:
1) The changes will be overwritten on the next Views update.
2) That I'm directly calling the content, so I'm not sure if I'm bypassing some important View's functions.

I have created this post in the hopes this will inspire the Views programmers to implement something like this officially, to the benefit of all their customers and so I don't have to hack these changes back into Views every time they release an update.

NOTES:
1) This has not been tested thoroughly!
2) Backup your site first (or at least the "wpv-shortcodes.php" file)
3) If not done correctly (by someone with at least some PHP experience), you could easily produce various errors on multiple pages if not prevent all of them from displaying.
4) The WP-Types team probably doesn't approve this modification

Anyway, I'm sure it would be easy for one of your programmers to tidy up, if not, I think it'll be helpful for anyone else needing these features.
New shortcodes:
[wpv-post-featured-image-url]
[wpv-post-featured-image-alt-text]
[wpv-post-featured-image-caption]
[wpv-post-featured-image-description]

Added the following code to "wp-contentpluginswp-viewsembeddedincwpv-shortcodes.php":

Note: I added the following code up the top (its obvious where) - this adds the new shortcodes to Views, it also adds them to the "Insert Views Shortcode" window (see screenshot attached).

/* Code Starts Here - Copy From Here */

$wpv_shortcodes['wpv-post-featured-image-url'] = array('wpv-post-featured-image-url', __('Featured image URL', 'wpv-views'), 'wpv_shortcode_wpv_post_featured_image_url');
$wpv_shortcodes['wpv-post-featured-image-alt-text'] = array('wpv-post-featured-image-alt-text', __('Featured image alt text', 'wpv-views'), 'wpv_shortcode_wpv_post_featured_image_alt_text');
$wpv_shortcodes['wpv-post-featured-image-caption'] = array('wpv-post-featured-image-caption', __('Featured image caption', 'wpv-views'), 'wpv_shortcode_wpv_post_featured_image_caption');
$wpv_shortcodes['wpv-post-featured-image-description'] = array('wpv-post-featured-image-description', __('Featured image description', 'wpv-views'), 'wpv_shortcode_wpv_post_featured_image_description');

/* Code Ends Here - Copy To Here */

Note: I then added the following code further down:

/* Code Starts Here - Copy From Here */

/**
* Views-Shortcode: wpv-post-featured-image-url
*
* Description: Display the url to the featured image of the current post
*
* Parameters:
* This takes no parameters.
*
*/

function wpv_shortcode_wpv_post_featured_image_url($atts) {
$post_id_atts = new WPV_wpcf_switch_post_from_attr_id($atts);

extract(
shortcode_atts( array(), $atts )
);

$out = wp_get_attachment_url( get_post_thumbnail_id() );

return $out;
}

/**
* Views-Shortcode: wpv-post-featured-image-alt-text
*
* Description: Display the alt text of the featured image of the current post
*
* Parameters:
* This takes no parameters.
*
*/

function wpv_shortcode_wpv_post_featured_image_alt_text($atts) {
$post_id_atts = new WPV_wpcf_switch_post_from_attr_id($atts);

extract(
shortcode_atts( array(), $atts )
);

$out = get_post_meta( get_post_thumbnail_id() , '_wp_attachment_image_alt', true);

return $out;
}

/**
* Views-Shortcode: wpv-post-featured-image-caption
*
* Description: Display the caption of the featured image of the current post
*
* Parameters:
* This takes no parameters.
*
*/

function wpv_shortcode_wpv_post_featured_image_caption($atts) {
$post_id_atts = new WPV_wpcf_switch_post_from_attr_id($atts);

extract(
shortcode_atts( array(), $atts )
);

$thumbnail_image = get_posts(array('p' => get_post_thumbnail_id(), 'post_type' => 'attachment'));

$out = '';
if ($thumbnail_image && isset($thumbnail_image[0])) {
$out = $thumbnail_image[0]->post_excerpt;
}

return $out;
}

/**
* Views-Shortcode: wpv-post-featured-image-description
*
* Description: Display the description of the featured image of the current post
*
* Parameters:
* This takes no parameters.
*
*/

function wpv_shortcode_wpv_post_featured_image_description($atts) {
$post_id_atts = new WPV_wpcf_switch_post_from_attr_id($atts);

extract(
shortcode_atts( array(), $atts )
);

$thumbnail_image = get_posts(array('p' => get_post_thumbnail_id(), 'post_type' => 'attachment'));

$out = '';
if ($thumbnail_image && isset($thumbnail_image[0])) {
$out = $thumbnail_image[0]->post_content;
}

return $out;
}

/* Code Ends Here - Copy To Here */

So, can we get something similar in a future release please?

-Timbo

#38337

Dear Timbo,

Thank you for your suggestion. I will pass this on to the developers to evaluate.

Regards,
Caridad

#38377

bruce
Supporter

Languages: English (English )

Timezone: Australia/Perth (GMT+08:00)

Hi Timbo,

Thanks for your input. The shortcode you propose could cause problems with the shortcode processing - http://codex.wordpress.org/Shortcode_API#Hyphens

Best regards,
Bruce

#38442

Tim

Hi Bruce,

Looks like you have a valid point, but I merely reflected how the Views shortcodes were setup. For example:

This is an official Views shortcode: [wpv-post-featured-image]
This is a shortcode I suggested: [wpv-post-featured-image-url]

Seems like the problem would be with how the developers of Views have setup the official shortcodes.

I'm not by any means proposing my code as a full and robust (or even an elegant) solution. But my suggestion is quick and simple to implement and is consistent with the current Views shortcodes. I also said "I'm sure it would be easy for one of your programmers to tidy up", and I'm sure they can tidy up the hyphens if they would like to.

-Timbo

#38460

Tim

Hello again,

I've updated my shortcodes to the following:
[wpv-post-featured-title]
[wpv-post-featured-url]
[wpv-post-featured-caption]
[wpv-post-featured-alt-text]
[wpv-post-featured-description]

As the shortcode [wpv-post-featured] doesn't exist I presume this is correct?

Obviously a better way of doing this would be to use [wpv-post-featured-image] with parameters, perhaps like this:
[wpv-post-featured-image format="title"]
[wpv-post-featured-image format="url"]
[wpv-post-featured-image format="caption"]
[wpv-post-featured-image format="alt-text"]
[wpv-post-featured-image format="description"]

I would love to see something like this being implemented in the future!

-Timbo

#38461

bruce
Supporter

Languages: English (English )

Timezone: Australia/Perth (GMT+08:00)

Hi Timbo,

I've add this to our todo list for the release after next. We'll be adding the format attribute the the wpv-post-featured-image shortcode.

Best regards,
Bruce

#68120

Bruce when do you recon you'll have these changes implemented? Very excited!

#99745

Any word on this inclusion?

#244800

Yes, this would be really usefull!. I know we can do it all by using a few Views custom fields.

It seems silly though - to reinvent the wheel and remove WP core features, only to have to manually re-add them for each project that needs a featured image in Views.

So that being said...

Any news on this addition to the core?