admin管理员组

文章数量:1122846

What function can I use in a plugin to get the dimensions of every image size (in an array preferably) that is defined in a child theme?

Just for clarification I am not asking how to create a new image size.

What function can I use in a plugin to get the dimensions of every image size (in an array preferably) that is defined in a child theme?

Just for clarification I am not asking how to create a new image size.

Share Improve this question asked Nov 11, 2011 at 18:03 MTTMTT 3,58612 gold badges47 silver badges74 bronze badges 1
  • Searching for something like this, but with an interface, came across the plugin Additional image sizes (zui). It has 500 days without updates but passed the first batch of tests. – brasofilo Commented Jul 6, 2012 at 1:51
Add a comment  | 

8 Answers 8

Reset to default 61

Found it here. The answer is:

global $_wp_additional_image_sizes; 
print '<pre>'; 
print_r( $_wp_additional_image_sizes ); 
print '</pre>';

WordPress core doesn't have a native method for getting intermediate image sizes (i.e. width and height), but the following helper function will get all registered image sizes along with their dimensions:

/**
 * Get all the registered image sizes along with their dimensions
 *
 * @global array $_wp_additional_image_sizes
 *
 * @link http://core.trac.wordpress.org/ticket/18947 Reference ticket
 *
 * @return array $image_sizes The image sizes
 */
function _get_all_image_sizes() {
    global $_wp_additional_image_sizes;

    $default_image_sizes = get_intermediate_image_sizes();

    foreach ( $default_image_sizes as $size ) {
        $image_sizes[ $size ][ 'width' ] = intval( get_option( "{$size}_size_w" ) );
        $image_sizes[ $size ][ 'height' ] = intval( get_option( "{$size}_size_h" ) );
        $image_sizes[ $size ][ 'crop' ] = get_option( "{$size}_crop" ) ? get_option( "{$size}_crop" ) : false;
    }

    if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) ) {
        $image_sizes = array_merge( $image_sizes, $_wp_additional_image_sizes );
    }

    return $image_sizes;
}

Which will output results similar to:

Array
(
    [thumbnail] => Array
        (
            [width] => 150
            [height] => 150
            [crop] => 1
        )

    [medium] => Array
        (
            [width] => 300
            [height] => 300
            [crop] => 
        )

    [medium_large] => Array
        (
            [width] => 768
            [height] => 0
            [crop] => 
        )

    [large] => Array
        (
            [width] => 1024
            [height] => 1024
            [crop] => 
        )

)

If you only need the names of all image sizes, you can use get_intermediate_image_sizes:

<pre>
<?php print_r(get_intermediate_image_sizes()); ?>
</pre>

Since WP 5.3 it is enough to use this function:

wp_get_registered_image_subsizes();

Documentation: https://developer.wordpress.org/reference/functions/wp_get_registered_image_subsizes/

If the other answer did not work, use this code, so it will run after WordPress is initialized.

add_action('init', 'get_all_image_sizes');

function get_all_image_sizes(){
    global $_wp_additional_image_sizes; 
    print '<pre>'; 
    print_r( $_wp_additional_image_sizes ); 
    print '</pre>';
}

Since WP 4.7 you can use wp_get_additional_image_sizes() to get additional image sizes registered (excluding the default image sizes added by WP).

Since WP 5.3 you can use wp_get_registered_image_subsizes() to get all the image sizes registered.

Use the following functions:

get_intermediate_image_sizes(); // get all the image size names.
wp_get_additional_image_sizes(); // get all the additional image size data.
wp_get_registered_image_subsizes(); // get all the image size data.

I knowI am a bit late for the party, but here's my way to display all image sizes in the admin

/**
 * Metabox with all image sizes in Admin
 */
function sixtyseven_register_imagesizes_dashboard_widget() {
        global $wp_meta_boxes;
    
        wp_add_dashboard_widget(
            'imagesizes_dashboard_widget',
            'Registered image sizes',
            'sixtyseven_imagesizes_dashboard_widget_display'
        );
    
        $dashboard = $wp_meta_boxes['dashboard']['normal']['core'];
    
        $new_widget = array( 'imagesizes_dashboard_widget' => $dashboard['imagesizes_dashboard_widget'] );
        unset( $dashboard['imagesizes_dashboard_widget'] );
    
        $sorted_dashboard = array_merge( $new_widget, $dashboard );
        $wp_meta_boxes['dashboard']['normal']['core'] = $sorted_dashboard;
    }
    add_action( 'wp_dashboard_setup', 'sixtyseven_register_imagesizes_dashboard_widget' );
    
    function sixtyseven_imagesizes_dashboard_widget_display() {
        $sizes = wp_get_registered_image_subsizes();
        echo '<ul>';
        foreach($sizes as $name => $values){
            echo '<li>';
            echo '<strong>'.$name.':</strong> ';
            echo 'width: '.$values['width'].'px, ';
            echo 'height: '.$values['height'].'px';
            if($values['crop'] === true){
                echo ', cropped';
            }
            echo '</li>';
        }
        echo '</ul>';
    }

Hope that helps.

本文标签: imagesHow to get a list of all the possible thumbnail sizes set within a theme