Skip to content

Instantly share code, notes, and snippets.

@mehrshaddarzi
Last active April 12, 2023 22:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mehrshaddarzi/5568dc6f5caef7364ece2504daa52c4a to your computer and use it in GitHub Desktop.
Save mehrshaddarzi/5568dc6f5caef7364ece2504daa52c4a to your computer and use it in GitHub Desktop.
wordpress image thumbnail list Hook
<?php
// Upload to Custom folder in media_handle_upload
https://wordpress.stackexchange.com/questions/290257/media-handle-upload-with-custom-upload-folder
//Download image and to attachment
https://codex.wordpress.org/Function_Reference/media_handle_sideload
//change SrcSet
https://gschoppe.com/wordpress/fix-broken-images-after-wordpress-4-4-update/
function gs_change_attachment_image_markup($attributes){
if (isset($attributes['src'])) {
$attributes['data-flickity-lazyload-src'] = $attributes['src'];
$attributes['src'] = '';
}
if (isset($attributes['srcset'])) {
$attributes['data-flickity-lazyload-srcset'] = $attributes['srcset'];
$attributes['srcset'] = '';
}
return $attributes;
}
add_filter( 'wp_get_attachment_image_attributes', 'gs_change_attachment_image_markup' );
//change Attr filter
https://www.sitepoint.com/wordpress-change-img-tag-html/
https://jhtechservices.com/changing-your-image-markup-in-wordpress/
//get path image by name size
function scaled_image_path($attachment_id, $size = 'thumbnail') {
$file = get_attached_file($attachment_id, true);
if (empty($size) || $size === 'full') {
// for the original size get_attached_file is fine
return realpath($file);
}
if (! wp_attachment_is_image($attachment_id) ) {
return false; // the id is not referring to a media
}
$info = image_get_intermediate_size($attachment_id, $size);
if (!is_array($info) || ! isset($info['file'])) {
return false; // probably a bad size argument
}
return realpath(str_replace(wp_basename($file), $info['file'], $file));
}
//get path attachment base
$fullsize_path = get_attached_file( $attachment_id ); // Full path
//Upload and convert image to base64
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$attachment_id = media_handle_upload( 'image', $_POST['post_id'] );
if ( is_wp_error( $attachment_id ) ) {
$image="";
} else {
$img_file = scaled_image_path($attachment_id, $size = 'sarbaz');
// Read image path, convert to base64 encoding
$imgData = base64_encode(file_get_contents($img_file));
// Format the image SRC: data:{mime};base64,{data};
$src = 'data:'.mime_content_type($img_file).';base64,'.$imgData;
$image = $src;
wp_delete_attachment( $attachment_id, true );
}
//Get Attachemnt info
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' => $attachment->guid,
'title' => $attachment->post_title
);
}
https://stackoverflow.com/questions/14200815/how-to-hook-into-wordpress-thumbnail-generation
/*
ad_image_size with custom jpeg quality
*/
https://github.com/StickyBeat/custom-image-size-quality
https://wordpress.stackexchange.com/questions/74103/set-jpeg-compression-for-specific-custom-image-sizes
/*
prefilter name in wordpress
*/
add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );
function custom_upload_filter( $file ){
$file['name'] = 'wordpress-is-awesome-' . $file['name'];
return $file;
}
// rename file after upload
function wp_modify_uploaded_file_names($file) {
$info = pathinfo($file['name']);
$ext = empty($info['extension']) ? '' : '.' . $info['extension'];
$name = basename($file['name'], $ext);
$file['name'] = uniqid() . $ext; // uniqid method
// $file['name'] = md5($name) . $ext; // md5 method
// $file['name'] = base64_encode($name) . $ext; // base64 method
return $file;
}
add_filter('wp_handle_upload_prefilter', 'wp_modify_uploaded_file_names', 1, 1);
/**
* Remove Orginal Image Size
*/
add_filter( 'wp_generate_attachment_metadata', 'delete_fullsize_image' );
function delete_fullsize_image( $metadata )
{
$upload_dir = wp_upload_dir();
$full_image_path = trailingslashit( $upload_dir['basedir'] ) . $metadata['file'];
$deleted = unlink( $full_image_path );
return $metadata;
}
/**
* Get All image Size
*/
get_intermediate_image_sizes() => https://codex.wordpress.org/Function_Reference/get_intermediate_image_size
/**
* Snippet Name: Disable auto creating of image sizes
* Snippet URL: http://www.wpcustoms.net/snippets/disable-auto-creating-image-sizes/
*/
function wpc_unset_imagesizes($sizes){
unset( $sizes['thumbnail']);
unset( $sizes['medium']);
unset( $sizes['large']);
}
add_filter('intermediate_image_sizes_advanced', 'wpc_unset_imagesizes' );
/**
* How to Add your Custom Image Size into Media Uploader
*/
function add_custom_sizes_to_editor($sizes) {
unset( $sizes['thumbnail']);
unset( $sizes['medium']);
unset( $sizes['large']);
$custom_sizes = array(
'thumbnail-custom' => 'Custom Thumbnails' //300x200
);
if( !emptyempty($sizes) )
return array_merge($sizes, $custom_sizes);
else
return $custom_sizes;
}
add_filter('image_size_names_choose', 'add_custom_sizes_to_editor');
/*
* this hook will be fired while you uploading a picture
*/
add_filter( 'intermediate_image_sizes', 'rudr_reduce_image_sizes' );
function rudr_reduce_image_sizes( $sizes ){
/*
* $sizes - all image sizes array Array ( [0] => thumbnail [1] => medium [2] => large [3] => post-thumbnail )
* get_post_type() to get post type
*/
$type = get_post_type($_REQUEST['post_id']); // $_REQUEST['post_id'] post id the image uploads to
foreach( $sizes as $key => $value ){
/*
* use switch if there are a lot of post types
*/
if( $type == 'page' ) {
if( $value == 'regionfeatured' ){ // turn off 'regionfeatured' for pages
unset( $sizes[$key] );
}
} else if ( $type == 'region' ) {
if( !in_array( $value, array('regionfeatured','misha335') ) ){ // for regions turn off everyting except 'regionfeatured' and 'misha335'
unset( $sizes[$key] );
}
} else {
if( $value != 'thumbnail' ){ // turn off everything except thumbnail
unset( $sizes[$key] );
}
}
}
return $sizes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment