WordPress function to get dimensions of post_thumbnail previously set by admin in Media Settings

I use the following code in functions.php in order to load jQuery and set variables that I need to use in my jQuery script (width and height of the post thumbnail).

<?php
if( !is_admin()){
   wp_deregister_script('jquery');
   wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"), false, '');
   wp_enqueue_script('jquery');
   wp_localize_script( 'jquery', 'MyThumbSize', array( 'width' => '240px','height' => '160px' ) );
}?>

These variables are hard coded (240px and 160px) but I would like to know if it is possible to automatically obtain these values from a WordPress function and what would be the syntax (Since I’m not a good coder).

Read More

I know these settings are set by the admin the dashboard under Media Settings.

Related posts

Leave a Reply

1 comment

  1. You can get any of the media sizes like so:

       <?php 
        $thumbnail_width = get_option( 'thumbnail_size_w' );
            $thumbnail_height = get_option( 'thumbnail_size_h' );
            $medium_width = get_option( 'medium_size_w' );
            $medium_height = get_option( 'medium_size_h' );
            $large_height = get_option( 'large_size_w' );
            $large_width = get_option( 'large_size_w' );
       ?>
    

    Get rid of the php tags if you are using this in your functions.

    So your code should look like this i think:

    <?php
    $thumbnail_width = get_option( 'thumbnail_size_w' );
    $thumbnail_height = get_option( 'thumbnail_size_h' );
    
    if( !is_admin()){
       wp_deregister_script('jquery');
       wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"), false, '');
       wp_enqueue_script('jquery');
       wp_localize_script( 'jquery', 'MyThumbSize', array( 'width' => $thumbnail_width,'height' => $thumbnail_height ) );
    }?>