Function and usage of get_udims() in wordpress


Answers ( 1 )


    The get_udims() function in WordPress is used to calculate the new dimensions for a downsampled image. This function is particularly useful when you want to resize an image while maintaining its aspect ratio. Here's a detailed explanation and sample usage:

    Function Signature:

    get_udims( int $width, int $height ): array


    • Purpose: get_udims() calculates the dimensions to which an image should be resized. This function is often used in scenarios where you need to downscale images to fit certain dimensions while keeping the original aspect ratio intact.
    • Related Function: It's important to note the wp_constrain_dimensions() function, which get_udims() might rely on or work similarly to. wp_constrain_dimensions() is used for constraining image dimensions based on a set maximum width and height.


    1. $width (int, required): This parameter represents the current width of the image that you want to downsample.
    2. $height (int, required): This parameter represents the current height of the image that you're working with.


    • Type: Array
    • Contents: The function returns an array containing the shrunk dimensions (width, height) of the image.

    Sample Usage:

    Suppose you have an image that is 1200px wide and 800px high, and you want to calculate its new dimensions for downsampling:

    // Original dimensions
    $original_width = 1200;
    $original_height = 800;
    // Calculate new dimensions
    $new_dimensions = get_udims($original_width, $original_height);
    // Output the new dimensions
    echo "New Width: " . $new_dimensions[0] . "px";
    echo "New Height: " . $new_dimensions[1] . "px";

    In this example, get_udims() will return the new width and height values based on the downscaling logic implemented within the function. The actual scaling factor or how the new dimensions are calculated will depend on the internal implementation of get_udims(), which is typically designed to maintain the aspect ratio of the image.


    • The behavior of get_udims() can be influenced by various factors, such as maximum permissible dimensions set in WordPress settings or other constraints.
    • Always check the current WordPress documentation as functions and their behavior might change over time.

Leave an answer