Get attachment or image id by url – WordPress

Today I will talk about one non-standard task that WordPress can't do by default., namely, to take from the database the ID of the picture (or other investment) by her URL. Such a task is often necessary when developing plugins or new WordPress themes..

First we need a function that will make a request to the database and get the required image by URL.

This function can be put in the functions.php file and called anywhere in the template.

How to call a function to pass a URL and return an image ID

Below I will show which will pass the URL of the image to the rename and get the ID of the attachment, also below is the code with which you can resize the image using the standard WordPress function wp_get_attachment_image_src().

I would like to say that this should not be abused, because the task is not completed very quickly.


Купить хостинг WordPress
WordPress tutor
Online tutoring services. List of courses I teach
  • Basic web design course;
  • Site layout;
  • General course on CMS WordPress and continuation of the course on template development;
  • Website development in PHP.
Read more on the page WordPress tutor
*/

Nikolaenko Maxim

Director of web studies ProGrafika. I am developing, website design and promotion. Always glad to new blog readers and good clients.


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Templates for WordPress
The best hosting in Ukraine
Stable hosting for Drupal