Pulls Resized Images From Customizer Settings in WordPress
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 | |
if ( ! class_exists( 'My_Customizer_Theme_Mods' ) ) { | |
class My_Customizer_Theme { | |
var $version = '0.1'; | |
function __construct() { | |
/** | |
* We have two basic sizes of images used from the images set in the Customizer | |
* One of them is full-width of the browser, so let's make it large, but not unlimited | |
* The other is a 125x125 icon, but, just in case it needs to be a bit larger on mobile or | |
* something, we'll set it a bit larger than that | |
*/ | |
add_image_size( 'parallax_background', 2560, 1920, false ); | |
add_image_size( 'home_box_icon', 300, 225, false ); | |
/** | |
* Let's gather a list of the Customizer areas that get output as 125x125 icons | |
*/ | |
$this->icon_mods = array( 'featured_box1_image', 'featured_box2_image', 'featured_box3_image' ); | |
foreach ( $this->icon_mods as $name ) { | |
add_filter( "theme_mod_{$name}", array( $this, 'get_resized_theme_mod_icon' ) ); | |
} | |
/** | |
* Let's gather a list of the Customizer areas that get output as full-width backgrounds | |
*/ | |
$this->bg_mods = array( 'subheader_bgimg', 'home_footer_bgimg' ); | |
foreach ( $this->bg_mods as $name ) { | |
add_filter( "theme_mod_{$name}", array( $this, 'get_resized_theme_mod_image' ) ); | |
} | |
} | |
/** | |
* Tries to convert an attachment URL into a post ID. | |
* This version of this function was copied almost directly from the | |
* code within WordPress Core; it's just a backup in case the | |
* function doesn't exist within Core for some reason | |
* | |
* @since 4.0.0 | |
* | |
* @global wpdb $wpdb WordPress database abstraction object. | |
* | |
* @param string $url The URL to resolve. | |
* @return int The found post ID, or 0 on failure. | |
*/ | |
function attachment_url_to_postid( $url ) { | |
/** | |
* Default to the one included with WP; if that doesn't | |
* exist for some reason, we'll use the one copped from WP 4.3+ | |
*/ | |
if ( function_exists( 'attachment_url_to_postid' ) ) { | |
return attachment_url_to_postid( $url ); | |
} | |
/** | |
* Proceed with the one we copied from Core if the function | |
* didn't exist in Core for some reason | |
*/ | |
global $wpdb; | |
$dir = wp_upload_dir(); | |
$path = $url; | |
$site_url = parse_url( $dir['url'] ); | |
$image_path = parse_url( $path ); | |
//force the protocols to match if needed | |
if ( isset( $image_path['scheme'] ) && ( $image_path['scheme'] !== $site_url['scheme'] ) ) { | |
$path = str_replace( $image_path['scheme'], $site_url['scheme'], $path ); | |
} | |
if ( 0 === strpos( $path, $dir['baseurl'] . '/' ) ) { | |
$path = substr( $path, strlen( $dir['baseurl'] . '/' ) ); | |
} | |
$sql = $wpdb->prepare( | |
"SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s", | |
$path | |
); | |
$post_id = $wpdb->get_var( $sql ); | |
/** | |
* Filter an attachment id found by URL. | |
* | |
* @since 4.2.0 | |
* | |
* @param int|null $post_id The post_id (if any) found by the function. | |
* @param string $url The URL being looked up. | |
*/ | |
return (int) apply_filters( 'attachment_url_to_postid', $post_id, $url ); | |
} | |
/** | |
* Get the URL of a specifically-sized image to be used in a Customizer-set location | |
* | |
* @param string $img the image URL that's stored in the Customizer settings | |
* @param string|array $size='parallax_background' the predefined image size to return | |
* @return string the URL to the appropriately-sized image (or the original URL if the size was not found) | |
*/ | |
function get_resized_theme_mod_image( $img, $size='parallax_background' ) { | |
/** | |
* If we're in the admin area (or in the Customizer), skip this process | |
*/ | |
if ( is_admin() ) | |
return $img; | |
/** | |
* Attempt to get the ID of the image attachment | |
*/ | |
$id = $this->attachment_url_to_postid( $img ); | |
if ( empty( $id ) || is_wp_error( $id ) || ! is_numeric( $id ) ) { | |
return $img; | |
} | |
/** | |
* Get the URL to the appropriately-sized image | |
*/ | |
$img = wp_get_attachment_image_src( $id, $size ); | |
if ( is_array( $img ) && esc_url( $img[0] ) ) { | |
return esc_url( $img[0] ); | |
} | |
return $img; | |
} | |
/** | |
* This is an alias of the My_Customizer_Theme::get_resized_theme_mod_image() method that | |
* requests the 'home_box_icon' image size instead of the 'parallax_background' size | |
*/ | |
function get_resized_theme_mod_icon( $img ) { | |
return $this->get_resized_theme_mod_image( $img, 'home_box_icon' ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment