How to get thumbnail instead of the first image? - php

I have a following piece of code:
function get_first_image_url($html) {
if (preg_match('/<img.+?src="(.+?)"/', $html, $matches)) {
return $matches[1];
}
}
How can I change it to be able to get the thumbnail instead of first image?

It depends entirely on how your theme is set up. Some may be placing the thumbnail as a background image source, in an entirely new div, etc., and it may vary on each and every page template.
Luckily WordPress has a slew of functions to help you grab the thumbnail programatically without parsing the HTML, such as get_the_post_thumbnail(), get_the_post_thumbnail_url(), wp_get_attachment_image(), and multiple others that allow you get the the thumbnail based on the post ID, image ID, or whatever fields you have access to at that point.
There's also some non-native functions people have come up with that will fetch the attachment ID based on the URL (like a reversewp_get_attachment_image()) which would allow you to grab the "thumbnail size" based on the URL if that's what you need: Get attachment ID by file path in WordPress

Related

Wordpress image remove width parameter from url and remove height width tags

I have uploaded few images to wordpress and the image in slider comes with 550px only.
While my original width of the image is 1024px.
So wordpress is making the url something like this :
<img class="amazingslider-img-elem-3" data-originalwidth="550" data-originalheight="217" style="position:absolute;max-width:100%;width:100%;height:auto;left:0%;top:0%;margin-top:0.09728867623604587%;" src="http://i0.wp.com/uwf.org.in/jibaint/wp-content/uploads/2016/01/slider1.jpg?w=550">
so i want to remove this high lighted tags and parameters in url.
This question has been answered very well over on Wordpress Stack Exchange. You can read more here: https://wordpress.stackexchange.com/questions/29881/stop-wordpress-from-hardcoding-img-width-and-height-attributes
There are 2 main approaches. The first is that when you retrieve the image, rather than using get_the_post_thumbnail that you get the attachment id and then you can get the source and change the markup yourself
The second may be the solution for you - it's a change to override the default image markup that Wordpress uses by replacing/overriding the image_downsize function.
Both of those approaches are very well explained on the link I mentioned above

Get page Body contents in MODX

My MODX site needs to grab the first image from all pages for Open Graph Meta tags, which will be plugged into the Head chunk for all templates.
The problem with this is that not all images are located in the content part of a page. Some are located inside Chunks and others inside TVs. (Finding an image tag from the content is not an issue.)
It might be possible to get all Chunks and TVs and loop through their values to check for images.
But is there a way to get the <body> contents of the resource?
Probably several ways, you can try writing a plugin to parse through the entire content of a page, looks like the OnWebPageComplete event may be the one to use (take a look at the different events to see if one is more appropriate}
You can try and grab the resource from the cache, keeping in mind that any chnks/snippets/TVs called in the page un-cached will not show up in the resource cache file.
You can get a list of TVs once you have loaded a resource & then use getTVValue to get the value.
If you have an image in a chunk, getChunk might work [might, I've never tried to use it that way] to get it's contents but I would image that the image in a chunk would come from a TV ~ so you should be able to retrieve it with getTVValue.
You could also just setup a TV for the OpenGraph image and explicitly set it on a page by page basis.
Probably writing a plugin & some regex is going to be the least painful way of going about it.

Wordpress: make a post's video URL accessible from the loop

I'm looking for solution to a particular situation. In my theme each post is on a single page (single.php). Single page has a slider inside of the loop that is populated with attachment images. In that slider, I need to add some videos that are hosted on YouTube.
What I think I need is to write a plugin that allows to add YouTube URLs to a post in UI input fields in post editing window. These links would be then accessed using post id from single.php, so that I could loop through them, using foreach and render them as as html5 video.
I need a general advice on how this can be done. I've got a working slider that needs to host these videos along attachment images, but using attachment video does not work and it doesn't seem to be a viable solution (it's bandwidth intensive).
Any help is appreciated.
Thanks
I've sorted this out using a simpler approach, namely, I've used shortcodes to wrap content that's in the slider. It worked fine and saved plenty of time; it's more intuitive in comparison to using image attachments for creating the content, too.

Adding Custom post type title to attached images caption attribute in Wordpress

I trying to resolve an issue that a friend of mine is having with an image heavy Wordpress site.
The site has multiple Custom Post Types and each Post Type has custom fields that allow multiple images to be uploaded.
The problem i'm trying to solve is this; The site has around 40,000 images, most of which are attached to the custom posts mentioned above, these images are used to showcase properties and are combined into preview pages to show to potential clients. Locating the correct images is difficult and i need to add some custom meta data to the images so make them easier to group/navigate.
Ideally i need to add the attachments parent post title into the alt or caption attributes for every image on the site.
Theoretically this would be done by using a default hook to add_filter('foo... I'm just not quite sure how to structure this so that it can run through all old images and future images.
A nudge in the right direction would be very appreciated.
WP_Image_Editor objects that you can perform operations on:
wp_save_image_editor_file
image_editor_save_pre
wp_save_image_editor_file
wp_image_editor_before_change
the first hook is triggered when an image is edited (crop/re-size whatever)
the second one is triggered when you crop/rotate the image and press the Save button (and it's a pre-filter)
for the 3rd one it is important to note that you should use $image->save( $filename, $mime_type ); inside your callback, or you will be left with an image that doesn't get saved. And if you want to do so, then better don't return the $saved with no value/NULL.

Fetch Images from URL - Like Facebook Thumbnail

I am building a "Reddit" like site.
The User can post an URL from which I want to get the correct image with PHP.
What I would need is a script which sites like Facebook or Tumblr use to fetch the Images.
I saw already scripts which get the images by getting the HTML Content and searching for "img" tags.
Are there any better methods/scripts available?
Maybe even scripts which will order the images by the size: The bigger the image the more important it is.
Thanks for answers
You may want to check out PHPQuery, it will allow you easily iterate through all images on a given website. You can then work out the areas of each image and sort them accordingly.
It depends a bit for what you're looking for and what the image is that the user would like to have with his post. To give you an example: I once wrote a method that searches for a logo of a company on the company's website. To do so, I searched for, indeed, the img-tags using simple_html_dom and filtered those tags on the existence of logo in the alt-tag. The results are displayed to the user to select the right image; it could be that you find multiple images fitting your purpose.
I would indeed, as you proposed, have a look at the size and skip small images (e.g. smaller dan lets say 50 px).

Categories