Shoora
4/28/2019 - 12:28 PM

[WordPress] Functions to help making $content_width suck less by declaring it based on available image sizes and making it filterable. This

[WordPress] Functions to help making $content_width suck less by declaring it based on available image sizes and making it filterable. This will pass the Theme Check plugin test.

<?php
/**
 * Helps making $content_width suck less (http://wp.me/p208mj-2V) by
 * declaring it based on available image sizes and making it filterable.
 * This will pass the Theme Check plugin test.
 * (https://wordpress.org/plugins/theme-check/)
 *
 * Author:     Caspar Hübinger
 * Author URI: http://glueckpress.com
 */


/**
 * Set the content width in pixels, based on available image sizes.
 *
 * Lowest possible priority to make it available to lower priority callbacks.
 * However, image sizes added by the theme must be declared beforehand.
 * Therefore priority is 1 rather than 0, while 0 is used to add image sizes
 * in the function below.
 *
 * Pitfalls: will not consider image sizes added later than priority 0 via
 * after_setup_theme. Image sizes set by plugins, for example, might remain
 * unconsidered.
 *
 * @global int $content_width
 */
function glueckpress_content_width() {

	// px width of .entry-content when page-full.php is used.
	$content_max = 965;

	// Retrieve the widest of all available image sizes.
	$image_sizes = glueckpress_get_image_sizes();
	$widest_size = glueckpress_get_widest_image_size( $image_sizes );

	// Calculate fix px size against widest image size.
	$content_width = $content_max > $widest_size ? $content_max : $widest_size;

	// Set filter for global.
	$GLOBALS['content_width'] = apply_filters(
		'glueckpress_content_width',
		$content_width
	);
}
add_action( 'after_setup_theme', 'glueckpress_content_width', 1 );

/**
 * Add custom image sizes for the theme.
 *
 * @return void
 */
function glueckpress_add_image_sizes() {

	// custom image size set by theme
	add_image_size( 'hero-image', 1100, 0, false );
}
add_action( 'after_setup_theme', 'glueckpress_add_image_sizes', 0 );


/**
 * Add custom image size name to media select element.
 * https://codex.wordpress.org/Plugin_API/Filter_Reference/image_size_names_choose
 *
 * @param  array $sizes
 * @return array $sizes
 */
function glueckpress_add_image_size_to_select( array $sizes ) {

	$sizes = array_merge(
		$sizes,
		array(
			'hero-image' => __( 'Hero Image', 'textdomain' )
		)
	);

	return $sizes;
}
add_filter( 'image_size_names_choose', 'glueckpress_add_image_size_to_select' );

/**
 * Get default WP image sizes as well as sizes added via add_image_size().
 *
 * @link   https://codex.wordpress.org/Function_Reference/get_intermediate_image_sizes
 * @param  string $size (default: '')
 * @return array $sizes
 */
function glueckpress_get_image_sizes( $size = '' ) {

	$registered_sizes = $GLOBALS['_wp_additional_image_sizes'];
	$sizes = array();
	$wp_default_sizes = get_intermediate_image_sizes();

	foreach( $wp_default_sizes as $_size ) {

		if ( in_array( $_size, array( 'thumbnail', 'medium', 'large' ) ) ) {

			$sizes[ $_size ]['width']  = get_option( $_size . '_size_w' );
			$sizes[ $_size ]['height'] = get_option( $_size . '_size_h' );
			$sizes[ $_size ]['crop']   = (bool) get_option( $_size . '_crop' );

		} elseif ( isset( $registered_sizes[ $_size ] ) ) {

			$sizes[ $_size ] = array(
				'width'  => $registered_sizes[ $_size ]['width'],
				'height' => $registered_sizes[ $_size ]['height'],
				'crop'   => $registered_sizes[ $_size ]['crop'],
			);
		}
	}

	// Passed $size only if available.
	if ( $size )
		return isset( $sizes[ $size ] ) ? $sizes[ $size ] : false;

	return $sizes;
}


/**
 * Retrieve image size with greatest width from an array of given sizes.
 * $sizes must come as array( 'width', 'height', 'crop' ).
 *
 * @param  array $sizes
 * @return integer $widest
 */
function glueckpress_get_widest_image_size( array $sizes ) {

	$i = 0;
	$widths = array();

	foreach ( $sizes as $size ) {

		if ( ! isset( $size['width'] ) )
			continue;

		$widths[ $i ] = $size['width'];
		$i++;
	}

	$widest = ! empty( $widths ) ? max( $widths ) : -1;

	return absint( $widest );
}