Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set featured image programmatically from url?

I have some post id's and I want to set these posts' featured images from same url.

Here is my adding post codes:

$catid = get_cat_ID("XX Cat");

$my_post = array(); 
$my_post['post_title'] = $title; 
$my_post['post_content'] = $description; 
$my_post['post_status'] = 'publish'; 
$my_post['post_author'] = 1; 
$my_post['post_category'] = array( $catid ); 

$post_id = wp_insert_post( $my_post );

Example: post_id = 1 I want to set featured image to: example.com/image.png

How can I do this?

like image 812
Cris Denolatter Avatar asked Jan 07 '17 18:01

Cris Denolatter


People also ask

How do you set a featured image in a URL?

Click on the settings icon at the top of the page. By default, you will start in the Block section. Click on the Document section. Scroll down until you have found the Featured Image from URL option.

How do I add a featured image in WordPress post programmatically?

Create HTML Form We require a form with file input and a submit button to add the featured image via coding. So, in the page template, paste the HTML below. This code shows a form with a file input field and a submit button. We can also include a hidden field with a nonce value.

How do I find the featured image in WordPress programmatically?

Using the built-in WordPress function get_the_post_thumbnail() to display the featured image of a post in a <img> tag. This is the easiest way to display a post's featured image in a WordPress loop.


1 Answers

You can set an image as post featured thumbnail when it is in your media library. To add an image in your media library you need to upload it to your server.

try this code:

// Add Featured Image to Post
$image_url        = 'http://s.wordpress.org/style/images/wp-header-logo.png'; // Define the image URL here
$image_name       = 'wp-header-logo.png';
$upload_dir       = wp_upload_dir(); // Set upload folder
$image_data       = file_get_contents($image_url); // Get image data
$unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name ); // Generate unique name
$filename         = basename( $unique_file_name ); // Create image file name

// Check folder permission and define file location
if( wp_mkdir_p( $upload_dir['path'] ) ) {
  $file = $upload_dir['path'] . '/' . $filename;
} else {
  $file = $upload_dir['basedir'] . '/' . $filename;
}

// Create the image  file on the server
file_put_contents( $file, $image_data );

// Check image file type
$wp_filetype = wp_check_filetype( $filename, null );

// Set attachment data
$attachment = array(
    'post_mime_type' => $wp_filetype['type'],
    'post_title'     => sanitize_file_name( $filename ),
    'post_content'   => '',
    'post_status'    => 'inherit'
);

// Create the attachment
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );

// Include image.php
require_once(ABSPATH . 'wp-admin/includes/image.php');

// Define attachment metadata
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );

// Assign metadata to attachment
wp_update_attachment_metadata( $attach_id, $attach_data );

// And finally assign featured image to post
set_post_thumbnail( $post_id, $attach_id );

ref url : http://www.wpexplorer.com/wordpress-featured-image-url/

Modified as your requirement: or that purpose ignore WordPress standard and upload your all post single image on your custom folder and add this image path or direct external image url into post as extra attribute meta field and when you will show post on your theme then just use your img with help of post id. demo code: for setting image

<?php
    update_post_meta ( 7, 'imgkey', 'www.url.path' );//7 is post id
?>

for getting image on your theme page where you want to show it

<?php
    $img_value = get_post_meta( get_the_ID(), 'imgkey', true );
?>
<img src="<?php echo $img_value?>">

Note if you are new in WordPress post custom meta fields then read this article: https://codex.wordpress.org/Custom_Fields
or
unofficial article about custom fields: https://premium.wpmudev.org/blog/creating-custom-fields-manually

like image 56
Hassan Saeed Avatar answered Oct 20 '22 00:10

Hassan Saeed