Forum Replies Created

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi, good stuff.

    Maybe replace the underscore with a hyphen? Since that is commonly used as a separator (might also be good for that tiny bit of SEO).

    So “site.com-filename.jpg” instead of “site.com_filename.jpg”.

    Thread Starter jubot

    (@jubot)

    It does make sense. Though on the website I’m making the consistency of a floating bar on the frontpage and post page would look good. Maybe one day the option to regard index.php the same as a static page? Only one page to +1 and so only one count to update.

    Thanks a lot for taking the time to explain. Loving the plugin and such amazing progress in just a few weeks time.

    cheers.

    Thread Starter jubot

    (@jubot)

    So it would have to be a (static) page without posts? Since otherwise it will only like the first post on the page and not the page as a whole. A static page won’t do as my frontpage.

    Is het so that this plugin will only work on pages with a single post on them? But not on a page with multiple posts like index.php and most front pages. Just to be clear: appearing is not an issue, it floats beautifully. By work I mean “like” index.php, home.php, frontpage.php etc. .

    Thread Starter jubot

    (@jubot)

    We went full circle indeed. Technically my site is structured as you mention in the last paragraph so this solution will work (for now). I’m now fiddling with the code from my first post. Have a look if you want, though if not that’s ok because you already helped me out.

    // add automated custom field on post: image thumbnail for rating - find attachment id
    
    // get attachment id
    function pn_get_attachment_id_from_url( $attachment_url = '' ) {
    
    	global $wpdb;
    	$attachment_id = false;
    
    	// If there is no url, return.
    	if ( '' == $attachment_url )
    		return;
    
    	// Get the upload directory paths
    	$upload_dir_paths = wp_upload_dir();
    
    	// Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image
    	if ( false !== strpos( $attachment_url, $upload_dir_paths['baseurl'] ) ) {
    
    		// If this is the URL of an auto-generated thumbnail, get the URL of the original image
    		$attachment_url = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $attachment_url );
    
    		// Remove the upload path base directory from the attachment URL
    		$attachment_url = str_replace( $upload_dir_paths['baseurl'] . '/', '', $attachment_url );
    
    		// Finally, run a custom database query to get the attachment ID from the modified attachment URL
    		$attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $attachment_url ) );
    
    	}
    
    	return $attachment_id;
    }
    
    // add automated custom field on post: image thumbnail for rating - custom field
    function w_thumbnail_src() {
    		$attachment_id = pn_get_attachment_id_from_url( $attachment_url = '' );
            $image = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
            return $image[0]; // thumbnail url
    }
    
    add_action('publish_page', 'add_custom_field_automatically', 'w_thumbnail_src');
    add_action('publish_post', 'add_custom_field_automatically');
    function add_custom_field_automatically($post_id) {
    	global $wpdb;
    	if(!wp_is_post_revision($post_id)) {
    		add_post_meta($post_id, 'thumbnail_rt', w_thumbnail_src(), true);
    	}
    }

    So I define the $attachment_id variable in the second (custom field) function by ‘calling’ the first function: $attachment_id = pn_get_attachment_id_from_url( $attachment_url = ” );

    Not working though. Is this the correct way to of using a return variable from one function in another?

    Thread Starter jubot

    (@jubot)

    brilliant stuff, looks like it is working! thank you! works with default upload folder and sub-domain referring to default upload folder. going to do more testing.

    so far there is only one issue: it only works for newly uploaded images. so selecting an image which is already in the library does not work. could it be that there is only attachment association with a post after upload?

    additionally, would it be possible to make it work on edit/update of the post? by re-initiating the code, or maybe a separate function, ‘update_post_meta()’?

    Thread Starter jubot

    (@jubot)

    Thanks for hanging in there. Ok, I think I got it. Let’s forget about gd star rating thumbnail generation. This is all about wp.

    Images are uploaded through the uploader which means that thumbnails will always be generated. The image is added to the post and is in between <img> tags. If the upload directory is on a subdomain then wp will know because it is defined in options.php. The path is there, regardless of where it is stored. So i figured using ‘<?php wp_get_attachment_image_src( $attachment_id, $size ); ?> ‘ would do the trick, you think so too if I understood correctly. $size Being one of the thumbnail sizes, lets say “medium”, which would grab “image-200×140.jpg”, a thumbnail generated after “image.jpg” was uploaded.

    You said it was possible to grab the image attachment:

    “Attachments are typically associated with posts, even if not featured images. You can normally get all image attachments to a post by using get_posts(array(‘post_type’ => ‘attachment’,’post_parent’ => $post_id, ‘post_mime_type’ => ‘image/jpeg’));. You can then just grab the ID from the first object returned and use wp_get_attachment_image_src() to get the image URL.”

    How would that code look like in a function? See I’m not able to put the two parts of code you mention together in one function (I’m in the deep-end php wise).

    Thread Starter jubot

    (@jubot)

    Thanks for the reply, you raise valid points. I figured that the way I was approaching the custom field was a bit of a detour but it was the only thing I could get to work. Before, I had it working with featured image, but only if I set one manually. Of course I wanted it automated so I installed an auto featured image plugin. but it would only set featured image after post publish, and the custom field function would fire before that so no image source grab. There I noticed it being an imperfect method.

    What I am trying to do is the following:

    I am using gd star rating widget to show post links with scores and thumbnail in the column. I had it working with the post content %IMAGE% tag (there are 3 options: none, %IMAGE%, custom field), but that only works when uploads are stored in the original wp directory. I want to load images from a subdomain so the gd star rating thumbnail generation does not work. another option is using a custom field. It does not fiend any custom fields so I had to create one. I could have saved my weekend and just use default upload folder, but it would be such a waste not to use this opportunity to load images from a subdomain right form the start.

    So bottom line: I need a custom field which contains the url to the thumbnail of the first (and only) image of a post. So image used in post would be “image.jpg”, custom field would contain “img-200×140.jpg”.

Viewing 7 replies - 1 through 7 (of 7 total)