WordPress.org

Ready to get started?Download WordPress

Forums

Conditional "add_image_size" - is it possible? (2 posts)

  1. ethanbeyer
    Member
    Posted 1 year ago #

    Hello,

    Wondering if it's possible to set up an add_image_size(); command that is either conditional, or fired when an image is made a featured image.

    I've looked all over for someone else trying to do this to no avail.

    [bump moderated]

  2. brianfeister
    Member
    Posted 1 year ago #

    Well, first off - the simplest and most ideal solution is to control this from the media settings page in your WP admin panel.

    Doing this could cause an issue if your theme is using the_post_thumbnail() so you might need to follow the advice on this thread and remove calls to that function in your theme and replace them with calls to a different image size. More info on Post Thumbnails (a.k.a "featured images") - http://codex.wordpress.org/Post_Thumbnails

    Now all of that being considered, those are the best ways to achieve the desired end goal for what you're asking about. However, perhaps your use case is different than what I've described above. If that's the case, here's the problem with your method right now - you're proposing to do add_image_size() when an image is made a featured image, however, at that point in time the image has already been created. This is why set_post_thumbnail_size() (http://codex.wordpress.org/Function_Reference/set_post_thumbnail_size) is the best practice for doing this. It sets the size globally as a filter which then creates that size automatically for new image uploads.

    If you're really desperate, you can try to follow this, this is not working yet - spent a bit of time on it, but you can play around. This hooks to the save_post action so every time a post is saved it checks to see if it has a featured image (post_thumbnail). If it does, then it finds the ID of that image, gets the post metadata for it (guid is the location of the image file on the server) and then programmatically creates the image - the big problem here is that this new image file will not obey the WordPress conventions regarding attaching images to associate them to specific posts, and it also does not register it to the media library, both of which are potentially serious problems. So, it's best to just use set_post_thumbnail_size() but if you're in the mood to tinker, here you go (disclaimer: it's not quite working yet, but pretty close):

    add_action( 'save_post', 'new_size_featured_img' );
    /**
     * Set featured image on posts
     *
     */
    function new_size_featured_img() {
    
    		if ( ! isset( $GLOBALS['post']->ID ) )
    			return NULL;
    
    		if ( ! has_post_thumbnail( get_the_ID() ) )
    			return NULL;
    
    		$args = array(
    			'numberposts'    => 1,
    			'order'          => 'ASC',
    			'post_mime_type' => 'image',
    			'post_parent'    => get_the_ID(),
    			'post_status'    => NULL,
    			'post_type'      => 'attachment'
    		);
    
    		$attached_image = get_children( $args );
    		$image_data = $attached_image[0];
    
    		$file = substr( strrchr( $image_data->guid, '/' ), 1 );
    		$filepath = str_replace( $file, '', $image_data->guid );
    		$file_arr = preg_split( '/\./', $file );
    		$filename = $file_arr[0];
    		$file_ext = $file_arr[1];
    		$upload_dir = wp_upload_dir();
    
    		$crop_width =  700;
    		$crop_height = 500;
    
    		$image = wp_get_image_editor( $image_data->guid );
    		$dimensions = $image->get_size();
    		$image->resize( $value['width'], $value['height'], $value['crop'] );
    
    		$image->save( $upload_dir['path'] . '/' . $filename . '-' . $value['width'] . 'x' . $value['height'] . '.' . $file_ext );
    
    }

Topic Closed

This topic has been closed to new replies.

About this Topic