set image from external url as featured image if its not present - php

i m trying to set a fallback image which is already uploaded to the site/present in media library as featured image if there's no featured image when i click on publish or update but it doesn't set the image. nothing happens
code in functions.php
function thumb_check($post_id){
$url="http://example.com/uploads/flower.png";
if (has_post_thumbnail( $post_id)){}
else { update_post_meta($post_id,'_thumbnail_id',$url);}
}
add_action( 'save_post', 'thumb_check' );

_thumbnail_id only works for WP photo id not URLs. Take a look at at this post https://wordpress.org/support/topic/_thumbnail_id-not-display-when-full-url-of-image
Here is my suggestion:
UPDATE: url_to_postid doesn't seem to work very well on my end. You should go to Media then look for your fallback image id and manually placing the image id in the code bellow. To see the image id look at the url it should look like http://example.com/wp-admin/upload.php?item=51 where 51 is the image id. Also, I replaced update_post_meta with set_post_thumbnail
functions.php
function thumb_check($post_id) {
if(!has_post_thumbnail($post_id)) {
set_post_thumbnail($post_id, 51)
}
}
add_action('save_post', 'thumb_check');

Related

How to get image URL from attachment ID and place it in an image src in Wordpress PHP?

I'm quite new to PHP but my desired functionality is to pull the image attachment URL from it's ID (generated through an ACF Custom Field) and use that as the src for an image.
This is the code that I have right now which should dynamically pull the image URL from the ID but doesn't seem to work:
<?php echo wp_get_attachment_url( the_field('upload_image') ); ?>
This is the code that works but isn't dynamic:
<?php echo wp_get_attachment_url( 257 ); ?>
Note the 257 is the ID of the image - I just need this to be able to be pulled dynamically by appropriating the first code snippet to take that attachment ID from the custom post field (which it should be doing in the first snippet but isn't).
Can anyone help out?
In acf the_field just output the item directly. In your case you need to use get_field function to get the id of the uploaded image. Because get_field returns the item.

Set thumbnail with an image URL in Wordpress

I'm currently just experimenting with wordpress PHP functions. There is a thing I don't understand. I made a plugin which gets photos in URL format, then puts them in a shortcode which just pastes the individual photo to Wordpress posts. How should I make a shortcode which would just set the thumbnail for the specific post it was posted to?
bit of source code:
function fone(){
global $images;
return ($images[0]);
}
add_shortcode( 'one', 'fone');
tried searching about set_post_thumbnail(), but it needs a thumbnail ID which I don't have. Any help?
You should check this function:
media_sideload_image($url, $post_id=0, $desc='', $return='html');

Blog page featured image being overridden by blog post featured image

On my Wordpress blog page (home.php) template, I'm trying to display the featured image set for that page at the top, then the content archive grid underneath. However, instead of displaying the featured image I set for the page, the first blog post's featured image is displayed instead. Additionally (related), if I remove the first blog post's featured image, it uses another post image on the content archives page. I'm guessing the two problems are related, but I'm not sure. Any help would be appreciated.
Here's the code I put in my functions.php
//Add featured images
add_action( 'genesis_before_content_sidebar_wrap', 'ws_post_image', 8 );
function ws_post_image() {
global $post;
wp_reset_postdata();
echo the_post_thumbnail($post->id); /*you can use medium, large or a custom size */
}
WordPress ignores anything you set on the "Page for Posts". Instead of treating it like a page in the general sense, it treats it more like an archive of posts and the "page" is merely a URL. That said, you can access the ID of the page and then use that to get the title, featured image, or other custom fields you assign to that page:
add_action( 'genesis_before_content_sidebar_wrap', 'ws_post_image', 8 );
function ws_post_image() {
echo get_the_post_thumbnail( get_option( 'page_for_posts' ), 'large' ); // Or use whatever size you need
}
This says, "Get me the featured image on the page I set as the Blog page in Settings->Reading". The ID of that page is stored in your options table with the name page_for_posts.

Featured Image Not Showing in Wordpress

I'm trying to show a featured image in Wordpress, I've uploaded the image (put it as the smallest image possible) but when I view the blog list view (category.php) I just get the blog post header, and no image.
Anybody know how to show the image? Checked the source, and doesn't show an image at all.
Hope someone can help, would be much appreciated.
In addition to setting the featured image in WP admin, you need rendering code somewhere in your template files. Something like this:
if ( has_post_thumbnail( get_the_ID() ) ) {
echo get_the_post_thumbnail( get_the_ID() );
}
I use this code in my header.php theme file, but you may prefer it within page.php or elsewhere (category.php seems appropriate in your case).
Check the documentation for help changing the dimensions, adding class attributes, etc. to the <img/> tag generated.
http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail

wordpress add thumbnail to post Excerpts plugin or method

I am looking for a way to add thumbnail images to post Excerpts.
I've installed: SuperSlider-Excerpt and Thumbnail For Excerpts plugins but I need to specifically assign the thumbnail that I want to use per post or automatically get the featured image to display as the thumbnail.
Anyone know a method or plugin that can do this please?
You can do this pretty easily with Wordpress. Like Francy said, the_post_thumbnail() will add the featured image you have picked on the post edit page in the admin section.
First you need to set up your thumbnails in your functions.php file. This is in the root directory of your theme (wp-content/themes/your-theme). Write this:
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 200, 200, false ); // Set your size, and clipping options
This would make thumbnails 200x200. You may want something else, so put whatever number you want in there.
Now that thumbnails are active, you can use the the_post_thumbnail() throughout your site, inside the loop, to display the thumbnail you've picked.
For example, if you want them to appear on the index page, head to index.php in your theme root directory, and write , right before . Experiment with where these appear, and get it in the right place for you.
Try within the loop:
the_post_thumbnail("thumbnail");
Or what also helped me was:
<?php
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'thumbnail');
$theImageSource = $image[0];
?>

Categories