Internal Shortcode | [Post_Thumb]
EkoJr edited this page Jan 13, 2017
·
3 revisions
Adds the Image URL of the Post's Thumb/Featured Image, but can also grab an image from the Post's Content. However, using WP's featured image can also utilize the size parameters.
[post_thumb size="thumbnail" extract="none"]
(string) (optional) Determines which image URL size should be added. Adding a custom size will only return the closest downsize that is available. Default: "thumbnail"
- thumbnail
- medium
- large
- full
- "xx, yy"
(string) (Optional) Determines whether to add any images from the Post's Content. Default: "none"
- none - Displays the Featured Image. Doesn't attempt to grab images from post content.
- on - If no Featured Image is found, then will attempt to use an image URL from post content.
- force - Feature Image is ignored, and will add the first
<img>
URL in post content.
(string) Image URL.
[post_thumb]
Returns
http://example.com/wp-content/uploads/2017/01/image.jpg
[post_thumb extract="none" size="full"]
Returns
http://example.com/wp-content/uploads/2017/01/image.jpg
[post_thumb extract="force"]
Returns
http://example.com/image.jpg
File: advanced-post-list/includes/class/apl_shortcodes.php
/**
* Post Thumb Shortcode.
*
* Desc: Adds a Post Thumb/Featured Image URL associated with the post, but
* can also grab an image within WP_Post->post_content (extract=on/force).
* Image Sizes include (default) thumbnail, medium, large, full,
* and custom "XX, XX" (closest image size available).
*
* Note: Post_Content Images are not resizable.
*
* 1. If 'size' string is numeric, then convert 'size' to an array(xx, yy).
* 2. Grab Featured Image from Post/Page w/ 'size'.
* 3. If 'extract' is 'on' OR 'force', add to return the src URL in img tag
* from $post->post_content.
* 4. Return string.
*
* @since 0.1.0
* @version 0.3.0 - Added 'extract' attribute.
* @version 0.4.0 - Changed to Class function, and uses WP's built-in
* functions for setting default attributes & do_shortcode().
* Added Custom Size support.
*
* @param array $atts {
*
* Shortcode Attributes.
*
* @type string $size Sets the image size used by WP's function.
* (thumbnail, medium, large, full, and
* custom "XX, XX").
* @type string $extract Extract from post_content (none, on, & force).
* }
* @return string Post Image URL.
*/
public function post_thumb($atts)
{
//INIT
$atts_value = shortcode_atts( array(
'size' => 'full',
'extract' => 'none'
), $atts, 'post_thumb');
$return_str ='';
//STEP 1
if (is_numeric(substr($atts_value['size'], 0, 1)) && substr($atts_value['size'], 0, 1) != '0')
{
$atts_value['size'] = explode(',', $atts_value['size']);
foreach ($atts_value['size'] as $key => $value)
{
$atts_value['size'][$key] = intval($value);
}
}
//STEP 2
if ( strtolower($atts_value['extract']) != "force" && current_theme_supports('post-thumbnails'))
{
$featured_image = wp_get_attachment_image_src(get_post_thumbnail_id($this->_post->ID), $atts_value['size']);
if ($featured_image)
{
$return_str .= $featured_image[0];
}
}
//EXTRACT/FALLBACK IMAGE (No Featured Image)
//STEP 3
if ( strtolower($atts_value['extract']) != 'none' && empty($return_str) )
{
//Parse and grab src="{}"
preg_match_all('/src="([^"]+)"/', $this->_post->post_content, $matches);
if (!empty($matches[1]))
{
//TODO ADD Offset? OR save for post_attachments
$return_str .= $matches[1][0];
}
}
//STEP 4
return $return_str;
}