Deluxe Blog Tips

Beautifully coded plugins, themes and useful tutorials, tips for WordPress

View my plugins or View my themes on ThemeForest

Page 2 of 39

Grunt: Saving disk space when installing packages

I’ve been using Grunt for WordPress development for a while. It’s a great tool and help a lot in automation tasks like compiling LESS to CSS, minifying CSS/JS files, etc. But one problem I met when using Grunt is in every project I work on, there’s always a folder node_modules taking a lot of disk space (around 100MB). Thing is most projects use the same packages, same tasks, just different configuration. So I think it should be a way to share these packages between projects. Symlink is the first idea, but it requires too much work. Until now…

Continue reading » Grunt: Saving disk space when installing packages

How to resize image on the fly in WordPress

There are several scripts to resize image on the fly such as Aqua Resizer, vt_resizer, script by Konstantin Kovshenin, Dynamic Image Resizer, and so on. They are great and work perfectly. But it seems a lot to me. All I want is a script that resize an image which is passed by attachment ID (not URL), that’s all. And here is what I get:

 * Resize image on the fly
 * @param  int     $attachment_id Attachment ID
 * @param  int     $width         Width
 * @param  int     $height        Height
 * @param  boolean $crop          Crop or not
 * @return string|bool            URL of resized image, false if error
function rw_resize( $attachment_id, $width, $height, $crop = true )
    // Get upload directory info
    $upload_info = wp_upload_dir();
    $upload_dir  = $upload_info['basedir'];
    $upload_url  = $upload_info['baseurl'];

    // Get file path info
    $path      = get_attached_file( $attachment_id );
    $path_info = pathinfo( $path );
    $ext       = $path_info['extension'];
    $rel_path  = str_replace( array( $upload_dir, ".$ext" ), '', $path );
    $suffix    = "{$width}x{$height}";
    $dest_path = "{$upload_dir}{$rel_path}-{$suffix}.{$ext}";
    $url       = "{$upload_url}{$rel_path}-{$suffix}.{$ext}";

    // If file exists: do nothing
    if ( file_exists( $dest_path ) )
        return $url;

    // Generate thumbnail
    if ( image_make_intermediate_size( $path, $width, $height, $crop ) )
        return $url;

    // Fallback to full size
    return "{$upload_url}{$rel_path}.{$ext}";

The script is created with references from the scripts above, mostly from Aqua Resizer and script by Konstantin Kovshenin. I just want a simpler version which shows the logic more clearly. Hope that’s useful for you, too.

Utilize available post fields for Custom Post Type

When store meta data for a post type, we often think of custom fields first. This is reasonable because custom fields are born to store such custom meta data for posts.

As we know each custom field is stored in 1 row in the postmeta table in database. If a post type has large number of custom fields (like a song has artist name, album, year, genre, length, bitrate, …), then for each post of that post type, it will take the same number of rows in the postmeta table. The total number of rows in the postmeta table for only that post type equals to number of posts multiplied by number of custom fields.

Continue reading » Utilize available post fields for Custom Post Type