Hot answers tagged

43

Here are few things you could try to remove the responsive image support in 4.4: /** * Disable responsive image support (test!) */ // Clean the up the image from wp_get_attachment_image() add_filter( 'wp_get_attachment_image_attributes', function( $attr ) { if( isset( $attr['sizes'] ) ) unset( $attr['sizes'] ); if( isset( $attr['srcset'] ...


40

Came here as this post is among the top hits on the search engine when looking for WordPress image alt and title. Being rather surprised that none of the answers seem to provide a simple solution matching the question's title I'll drop what I came up with in the end hoping it helps future readers. // An attachment/image ID is all that's needed to retrieve ...


37

As of WP 3.7 there is a much better way via the ajax_query_attachments_args filter, as provided in the documentation: add_filter( 'ajax_query_attachments_args', 'show_current_user_attachments' ); function show_current_user_attachments( $query ) { $user_id = get_current_user_id(); if ( $user_id ) { $query['author'] = $user_id; } ...


36

Yes, it's possible and pretty easy. This is the workflow I suggest: Put somewhere a UI to insert the URL of the featured image. Probably best choice is to use 'admin_post_thumbnail_html' filter hook Use 'save_post' action hook to save the URL (after security and validation routine) in a custom post meta Use 'post_thumbnail_html' filter hook to output proper ...


34

There are four valid sizes built in to the WordPress core. the_post_thumbnail('thumbnail'); // Thumbnail (default 150px x 150px max) the_post_thumbnail('medium'); // Medium resolution (default 300px x 300px max) the_post_thumbnail('medium_large'); // Medium Large resolution (default 768px x 0(means automatic height by ratio) max) since WP version 4....


32

Here's an improvement that uses the settings as you tried to do: add_image_size('medium', get_option( 'medium_size_w' ), get_option( 'medium_size_h' ), true );


30

When an image is uploaded in WordPress, it is saved in its original size, and also as some resized copies in different sizes. In this way it becomes easy to use different sizes, of the same image, for different purposes. By default, WordPress creates 3 copies in 3 different sizes: 'thumb' 'medium' 'large' The size in pixels for these 3 sizes can be set in ...


28

Your problem is that you are not providing the correct attachment's ID to get_post_meta() and get_the_title() functions. This is your code to get the alt of the image: $image_alt = get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true); And it is correct, but $attachment->ID is not defined in your code, so, the function does not return ...


25

Good news for WordPress versions 4.2+ Since version 4.2 the handy get_avatar_url() function, introduced as a feature request in ticket #21195 few years ago, now ships with the core: /** * Retrieve the avatar URL. * * @since 4.2.0 * * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash, * ...


23

WordPress core doesn't have a native method for getting intermediate image sizes (i.e. width and height), but the following helper function will get all registered image sizes along with their dimensions: /** * Get all the registered image sizes along with their dimensions * * @global array $_wp_additional_image_sizes * * @link http://core.trac....


22

Philipp, anything is possible if you set your mind to it. You can solve your issue by extending the WordPress image editor class. Note I'm using WordPress 3.7 - I haven't checked any of the below code in earlier versions and in the latest 3.8 release. Image Editor basics WordPress has two built in classes that handle image manipulation: ...


21

function wp_get_attachment( $attachment_id ) { $attachment = get_post( $attachment_id ); return array( 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), 'caption' => $attachment->post_excerpt, 'description' => $attachment->post_content, 'href' => get_permalink( $attachment->ID ), 'src' =&...


21

$image_url = 'adress img'; $upload_dir = wp_upload_dir(); $image_data = file_get_contents( $image_url ); $filename = basename( $image_url ); if ( wp_mkdir_p( $upload_dir['path'] ) ) { $file = $upload_dir['path'] . '/' . $filename; } else { $file = $upload_dir['basedir'] . '/' . $filename; } file_put_contents( $file, $image_data ); $wp_filetype = ...


19

Ok, I've been playing with this for a little bit and have managed to change the output of the Gallery block, with the following caveats: The preview does not match the output. I think this is possible but appears to be a bit more involved. Certain classes and markup are required in the output for the block to be able to parse the content and keep it ...


18

To remove the medium_large image size you can try to remove it with the intermediate_image_sizes filter: add_filter( 'intermediate_image_sizes', function( $sizes ) { return array_filter( $sizes, function( $val ) { return 'medium_large' !== $val; // Filter out 'medium_large' } ); } ); Not sure if you're trying to remove all the ...


17

If you only need the names of all image sizes, you can use get_intermediate_image_sizes: <pre> <?php print_r(get_intermediate_image_sizes()); ?> </pre>


17

There is an easier solution now: function my_gallery_default_type_set_link( $settings ) { $settings['galleryDefaults']['link'] = 'file'; return $settings; } add_filter( 'media_view_settings', 'my_gallery_default_type_set_link'); They added a filter to customize this default value (and other values related to the new media upload popup) in WP 4.0 (...


16

Wordpress codex has the answer, its below. Set the image size by cropping the image and defining a crop position: add_image_size( 'custom-size', 220, 220, array( 'left', 'top' ) ); // Hard crop left top When setting a crop position, the first value in the array is the x axis crop position, the second is the y axis crop position. ...


16

I found the culprit! WordPress 5.3 introduced additional image sizes which can be found via /wp-includes/media.php. Updating my function, like so, removed the extra sizes: function remove_default_image_sizes( $sizes) { unset( $sizes['large']); // Added to remove 1024 unset( $sizes['thumbnail']); unset( $sizes['medium']); unset( $sizes['...


15

The only time setting the quality really matters is right before the image is saved or streamed (for the editor). Both of these have the "image_editor_save_pre" filter there, passing it the instance of the image editor. So you can use that to modify the image in any way you like, including setting the quality. So, something like this should do the job ...


15

Long story short, you can get info about an attachment by using wp.media.attachment() function. This will give you complete data as long as this attachment is already loaded by another script or an wp.media() popup. If the data is not yet loaded, you can load it using .fetch() method on attachment, which works because it is a Backbone.Collection. It is a ...


15

It depends on your image and stylesheet location. But this is the syntax: .theme-image { background-image: url('../images/header-img.jpg'); } The above code is for the structure wp-content - themes - your-theme - images - header-img.jpg - css - style.css You are making the browser come one directory before and search for images ...


15

I don't remember a function that will do precisely that, but it is easily achieved with API overall: Retrieve attachment ID with get_post_thumbnail_id() Retrieve available sizes with get_intermediate_image_sizes() For each size use wp_get_attachment_image_src(), which gives precisely data you need (URL and dimensions).


14

To built on Max Yudin's answer you should use the intermediate_image_sizes_advanced filter, and not image_size_names_choose. Add to functions.php function add_image_insert_override($sizes){ unset( $sizes['thumbnail']); unset( $sizes['medium']); unset( $sizes['large']); return $sizes; } add_filter('intermediate_image_sizes_advanced', '...


14

Problem solved - the image width was being limited by $content_width set to 640 in the functions.php file included with Bones WP theme. Hopefully this information will help anyone else struggling with similar problems.


14

WordPress doesn't store path of generated sizes anywhere, you need to build it. As suggested by @kraftner in comments, wp_get_attachment_metadata() can be used to obtain some of the pieces you need to build the path. An alternative is image_get_intermediate_size(). The missing piece is the absolute path of upload folder. In theory, that can be retrieved ...


14

I want to disable the srcset only when calling a specific thumbnail size (for example only when calling full image size). Here are two ideas (if I understand you correctly): Approach #1 Let's check the size from the post_thumbnail_size filter. If it matches a corresponding size (e.g. full) then we make sure the $image_meta is empty, with the ...


14

A way to do it is to use the (very convenient) editor:image-edit and editor:image-update events triggered by the tinymce wpeditimage plugin to get/set the dom directly (updated to wrap in wp_enqueue_media action): add_action( 'wp_enqueue_media', function () { add_action( 'admin_footer', function () { ?> <script type="text/...


14

Is there any hook that fires once the image is uploaded and the image sizes generated? wp_handle_upload fires after the image is uploaded. After the follow-up question, I discovered that images would not be sized at this point. add_filter( 'wp_handle_upload' 'wpse_256351_upload', 10, 2 ); function wpse_256351_upload( $upload, $context ) { //* Do ...


13

I recently had to do this via a nightly cron script for a social media stream. $parent_id is the ID of the post you want to attach the image to. function uploadRemoteImageAndAttach($image_url, $parent_id){ $image = $image_url; $get = wp_remote_get( $image ); $type = wp_remote_retrieve_header( $get, 'content-type' ); if (!$type) ...


Only top voted, non community-wiki answers of a minimum length are eligible