Last active
December 19, 2015 06:09
-
-
Save attitude/5909203 to your computer and use it in GitHub Desktop.
Register the Post Thumbnail Support Snippet/Plugin for WordPress with one click
This file contains 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: Snipet: Enable `the_post_thumbnail` | |
Plugin URI: http://www.attitude.sk | |
Description: Displays `Post thumbnail` in post | |
Version: 0.1.0 | |
Author: Mrg. art. Martin Adamko | |
Author URI: http://www.attitude.sk | |
License: The MIT License (MIT) | |
Copyright (c) 2013 Mgr. art. Martin Adamko | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
/** | |
* Micro-plugin/snippet to add post thumbnail support | |
* | |
* @author Mgr. art. Martin Adamko <margin@attitude.sk> | |
* @version 0.1.0 | |
* | |
*/ | |
add_action('init', 'registerThePostThumbnail'); | |
add_action('the_post', 'prepareThePostThumbnail'); | |
/** | |
* Registers post tumbnail support | |
* | |
* @param void | |
* @returns void | |
* | |
*/ | |
function registerThePostThumbnail() | |
{ | |
add_theme_support('post-thumbnails'); | |
} | |
/** | |
* Setup `post_thumbnail` attribute during the Loop for WP_Post object passed | |
* | |
* @param WP_Post $_post Passed post object from the loop | |
* @returns WP_Post Modified post object with `post_thumbnail` attribute | |
* | |
*/ | |
function prepareThePostThumbnail(WP_Post $_post = NULL) | |
{ | |
// You can disable setting up the post thumbnail autmatically by filtering | |
// this to false | |
if (add_filter('prepareThePostThumbnail/emable', true)===false) { | |
return $_post; | |
} | |
if (empty($_post)) { | |
global $post; | |
if (isset($post)) { | |
$_post =& $post; | |
} else { | |
return $_post; | |
} | |
} | |
if (empty($_post) || is_admin()) { | |
return $_post; | |
} | |
$post_thumbnail_id = get_post_thumbnail_id($_post->ID); | |
if (!$post_thumbnail_id) { | |
return $_post; | |
} | |
if (! $post_thumbnail = wp_get_attachment_image_src($post_thumbnail_id, 'post-thumbnail')) { | |
// When post has no thumb return original image | |
return $_post; | |
} | |
$post_thumbnail_attribute_name = apply_filters('prepareThePostThumbnail/post_thumbnail_attribute_name', 'post_thumbnail'); | |
$_post->{$post_thumbnail_attribute_name} = $post_thumbnail; | |
return $_post; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment