wp_upload_dir()
is perfect. It is the only place where you can expect write permissions.
Store the attachment ID, not a path. Then you get the image with:
wp_get_attachment_url( $attach_id )
Sometimes a user may have deleted the image per media library. To avoid problems with missing files check the return value from wp_get_attachment_url()
first. Excerpt from one of my plugins:
/**
* Returns image markup.
* Empty string if there is no image.
*
* @param int $attach_id
* @return string
*/
protected function get_image_tag( $attach_id )
{
$img_url = (string) wp_get_attachment_url( $attach_id );
if ( '' == $img_url )
{
return '';
}
$img = "<img src="https://wordpress.stackexchange.com/questions/43076/$img_url" alt="" width="$this->width" height="$this->height">";
return $img;
}
Related Posts:
- Replace image urls(absolute instead of relative) by using filter in single page
- Server Path Information
- How can I make add_image_size() crop from the top?
- Resizing all images
- How to call plugin path in JS?
- Automatically Import Image into Posts from URLs on the Web?
- How can I attach images to a post from the media library?
- Rename existing old uploaded images containing accented French characters (not during upload)
- Thumbnail with different sizes
- Disabling post images for not logged in users
- How to upload thousands of images?
- There’s some way to add a wrapper around posted images?
- How to show category images (from plugin) on a regular page (page-example.php)?
- Saving/Writing/Adding file to upload folder
- How to limit 1 image per post on homepage only?
- Flickr plugin that will display photos from multiple group pools? [closed]
- Getting media library popup in custom plugin admin page
- How to change ID of an attachment in wp_posts and its related tables correctly?
- bulk change of image setting “link to” to “link to: image url “
- Bulk Image Uploader to create new post from each image [closed]
- Regenerate images with automatic ALT and TITLE attributes
- Images not showing after changing wp-content folder name
- Preview featured image using frontend post plugin
- How to Delete all Unused photos Function
- How to resize images due to container width automatically server side/without CSS
- How to create a managed image repository in wordpress
- Added slug after URL permalink last slash returns different content
- Retrieving a path when dealing with composer packages
- wp_generate_attachment_metadata is creating an empty array
- Contact Form 7 Get Image Function [closed]
- How generate 200% image size and save with suffix @2x
- Load external images via proxy
- How to upload WordPress Images to remote web server and display those images in WordPress gallery [closed]
- Retrieving Meta from Image Attachment
- How to upload multiple files/images when developing a wordpress plugin?
- Implementing an image hosting website using WordPress? [closed]
- Redirect to another page using contact form 7? [closed]
- How to apply lazy loading in background images [closed]
- Woocommerce showing images blurry in description page
- How to get path or root of plugin folder, not file or dir?
- Slashes stripped in ACF
- Getting taxonomy images to display on single-post with their terms
- Photo Gallery Plugin and Touch Devices
- How to Resize the Custom Post Images?
- How to limit image size for the entire website ? But without editing the post (css/plugin?!)
- Images under 1MB are not being compressed
- Correct Plugin File Path
- Blurry images when loading the page first time
- Search by Attachment ID
- Overwrite add_image_size defined by theme
- Some images on WordPress site are suddenly gone
- Simple sildeshow/image-upload plugin?
- Offer Download Links for Product Images
- how to remove share button from image overlay
- Optimal image size for wordpress?
- Copy/paste local images not working in WordPress
- How to add height and width to images on a page(using WP Bakery) to improve Google’s CLS score?
- Is classical wordpress eidtor a good option for me to include a large table with a large number of images on my webpage?
- insert og image link in wordpress post
- Optimise wordpress database using SQL after deleting registered images from Media Folder
- I need to register and resize images, safe to delete plugin afterwards?
- Displaying all the images from all of my articles
- Product Page loads terribly slow when product gallery images are added
- Failed GET Request From admin.php To Fetch .min.js File From Unminifed Directory
- WordPress how do I change images used in plugins?
- Remove special characters from filenames
- Why is my site still showing insecure icon even if I have SSL certificate?
- Help finding a plugin – masonry type post grid
- How to wrap image tag into div for post only?
- Insert Sponsor logo in custom own theme
- flip horizontal images in bulk
- Optimize and delete images in WordPress
- Upload image to wordpress using ionic/cordova with WP REST API V2
- Unexpected plugin_dir_path Output
- Is there any hook to change media html in post? [duplicate]
- Add image to post
- How to force regenerate thumbnails in WordPress posts?
- resizes images on the fly
- export high ress images to pdf
- Any way to edit/crop my featured image upon uploading?
- adding image in the header of my dev widget
- How to select featured images for 1500 posts?
- How to tell if a script is being executed from a plugin, a theme or a child theme in WordPress?
- How to show post attachment image
- Advanced Custom Fields (ACF) Plugin – Random Image in Sidebar
- Display alt text as tool tip on page
- Include paths and functions for external scripts
- Adding Relevant Post Images
- Images not showing and plugins not installing on wordpress server move
- Any plugins that upload, resize, and display images? [closed]
- How can I server scaled images?
- Adding hero images to blog posts
- Image Layout in Posts
- Is there a WordPress Plugin like Imsanity that keeps high resolution originals? [closed]
- Images get uploaded itself from the computer
- how to embed a crop feature for cropping images uploaded by the user
- WP-DBManager Plugin Can’t Find Mysql Path
- Achieve the following layout
- How To Convert External Image URLs To Own Website’s Uploaded Images Automatically?
- How do i display the number of images on the “post screen”