Kifsif
Kifsif

Reputation: 3843

Show all image sizes

This is a shortcode:

<?php

/**
 * Get all the registered image sizes along with their dimensions
 */

function show_all_image_sizes(){

    $image_sizes = wp_get_registered_image_subsizes();
    
    return '<pre>' . print_r($image_sizes) . '</pre>';
}

Using it:

enter image description here

Result:

enter image description here

Rendered HTML:

enter image description here

What is going on here? Namely:

  1. Why the sizes are printed above Trololo? I seem to return something from the function but this return has nothing to do with this output. So, what did the output is a mystery to me.
  2. Why print_r didn't return a prettified array of image sizes? And how to adjust the code to achieve the desired result?

Upvotes: 1

Views: 643

Answers (2)

Kifsif
Kifsif

Reputation: 3843

I should have put

print_r($image_sizes, true);

Upvotes: 1

ItsGeorge
ItsGeorge

Reputation: 2201

  1. In your function, try replacing return with echo
<?php

/**
 * Get all the registered image sizes along with their dimensions
 */

function show_all_image_sizes(){

    $image_sizes = wp_get_registered_image_subsizes();
    
    echo '<pre>',print_r($image_sizes),'</pre>';
}
  1. The use of print_r here is presumably just for debugging. If you want to output the data in that array in your html, you'll need to loop over it and echo strings of html with those values.

Upvotes: 1

Related Questions