Adding Featured Image to Post programatically

For each image/post combination, run the following. $image_name is the filename, $post_id is the post ID. if( $image_name != ” && !has_post_thumbnail( $post_id ) ){ $base = get_stylesheet_directory(); $imgfile= $base . ‘/import/’ . $image_name; // Images are stored inside an imports folder, in the theme directory $filename = basename($imgfile); $upload_file = wp_upload_bits($filename, null, file_get_contents($imgfile)); if … Read more

How to get list of paths (not urls) for an image

This should achieve what you want, however note that there will be backslashes AND forward slashes in the resulting path name. If this causes a problem on your web server you may need to do a further str_replace to forward slashes with backslashes See comments in code for more info $sizes = array_merge(array(‘full’),get_intermediate_image_sizes()); $uploads = … Read more

Get the attachment URL on single.php

The the_attachment_link returns an HTML link, so use this code: if ( $attachments = get_children( array( ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image’, ‘numberposts’ => 1, ‘post_status’ => null, ‘post_parent’ => $post->ID ) ) ); foreach ( $attachments as $attachment ) { echo wp_get_attachment_link( $attachment->ID, ” , true, false, ‘Link to image attachment’ ); } Here … Read more

ID of images from gallery

You can find out the IDs of which images are included in a gallery by switching to the Text editor tab and look for a piece of code like this: The comma-separated list of image IDs are enclosed in the ids attribute If you’re looking to find out IDs of existing images in order to … Read more

copy attachment title to description and alt text

You can make a query to loop through all the attachments, and then update the attachment info for each attachment in the while loop. Something like $args = array( ‘post_type’ => ‘attachments’, ‘post_status’ => ‘any’, ‘posts_per_page’ => -1, ) $query = new WP_Query($args) if($query->have_posts()): while($query->have_posts()): $query->the_post(); // 1. Get the attachment filename here and store … Read more

How to upload image from front end and save in media library?

Now I have an answer for my own question.I solved this issue by using this code.I add this code just for sharing and helping other as it works for me. <input type=”file” name=”my_file_upload” id=”my_file_upload_id” class=”bg_checkbox” > function register_team_show_case_setting() { //register our settings register_setting(‘my_team_show_case_setting’, ‘my_file_upload’); } add_action(‘admin_init’, ‘register_team_show_case_setting’); Code to upload and save image: require_once( ABSPATH … Read more

Change the Permalink for wordpress attachment

Your rule works with the attachment ID, so I’m not sure how you’re using the title, but the answer is almost identical in either case. The filter you want is attachment_link: function wpd_attachment_link( $link, $post_id ){ $post = get_post( $post_id ); return home_url( ‘/images/’ . $post->post_title ); } add_filter( ‘attachment_link’, ‘wpd_attachment_link’, 20, 2 ); Change … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)