Home › Forums › General Issues › Get Image Src (URL) from Text Field and Set Featured Image
Hi,
Is it possible to get image source from ACF text field and set it to featured image by using post_thumbnail_html
.
My Custom Field id is: `app_img_link’
The featured image for a post is stored as a post ID (attachment ID), this means that it is an image in the media library and this is required. In order to do this you would need to download and insert the image into media. see media_sideload_image()
@hube2 No, It you can make external src image url to featured image by post_thumbnail_html
.
But i don’t know how can i do it with ACF custom fields value.
Here is the few links:
https://wordpress.stackexchange.com/a/158568/210558
https://wordpress.stackexchange.com/a/342159/210558
If you know how can i get image src url value from custom fields and set it to featured image then plz tell me?
I’ve done something similar with Gravity forms, whereby someone would submit content and I needed to use the added image field to set the featured image on the new post.
The code used this:
$upload_path = rgar($entry, '11');
if (!empty($upload_path)){
$attachmentid = wp_create_image_id($upload_path, $member_post_id);
$setfeatimg = set_post_thumbnail($member_post_id, $attachmentid);
}
Obviously, you would need to alter from a gravity form field to your.
$member_post_id was the post ID I needed to add the image to
function wp_create_image_id( $image_url, $parent_post_id = null ) {
// Bail if the image url isn't valid
if( empty( $image_url ) || ! esc_url( $image_url ) )
return false;
// Escape the url, just to be save
$image_url = esc_url( $image_url );
// Cache info on the wp uploads dir
$wp_upload_dir = wp_upload_dir();
// get the file path
$path = parse_url( $image_url, PHP_URL_PATH );
// File base name, e.g. image.jpg
$file_base_name = basename( $image_url );
// Full path, set up to work with a WP in a subdirectory or default location
if( site_url() != home_url() ) {
$home_path = dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) );
} else {
$home_path = dirname( dirname( dirname( dirname( __FILE__ ) ) ) );
}
// Remove the trailing slash on the home path
$home_path = untrailingslashit( $home_path );
// Combine the two to get the uploaded file path
$uploaded_file_path = $home_path . $path;
// Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype( $file_base_name, null );
// error check
if( !empty( $filetype ) && is_array( $filetype ) ) {
// Create attachment title - basically, pull out the text
$post_title = preg_replace( '/\.[^.]+$/', '', $file_base_name );
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $uploaded_file_path ),
'post_mime_type' => $filetype['type'],
'post_title' => esc_attr( $post_title ),
'post_content' => '',
'post_status' => 'inherit'
);
// Set the post parent id if there is one
if( ! is_null( $parent_post_id ) && absint( $parent_post_id ) )
$attachment['post_parent'] = absint( $parent_post_id );
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $uploaded_file_path );
//Error check
if( !is_wp_error( $attach_id ) ) {
//Generate wp attachment meta data
if( file_exists( ABSPATH . 'wp-admin/includes/image.php') && file_exists( ABSPATH . 'wp-admin/includes/media.php') ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $uploaded_file_path );
wp_update_attachment_metadata( $attach_id, $attach_data );
} // end if file exists check
} // end if error check
return $attach_id;
} else {
return false;
} // end if $filetype
} // end function prg_create_image_id
If you need to get the image path, please read this forum post
@mrskt what you are talking about is using the post_thumbnail_html hook to add a filter that alters the html that is returned by the function get_the_post_thumbnail(). This is not what you asked, you said you wanted to set the featured image for the post from a URL. While they are similar and may appear to have the same result, they are two different things.
@hube2 I already mentioned “Text Field” it means i will add image url in that field as a value and that can be both External or Internal (Media Library) Src.
So did you find any solution for that?
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.