I am working with this two Wordpress Function for my Pinterest Button. What i'm trying to achieve is the flow chart below.

Function Catch That Image
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "http://www.mywebsite.com/wp-content/themes/Lord%20of%20Avernus%20-%20Abaddon/Images/Deafult_Img.png";
}
return $first_img;
}
Function Get Featured Image
function get_featured_image( $size = 'full' ) {
global $post;
if ( has_post_thumbnail($post->ID) ) {
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), $size);
return $featured_image[0];
}
return false;
}
Wordpress Featured Thumbnail
<?php the_post_thumbnail(); ?>
As you can see in my flow chart, I am trying to combine the two functions above. The problem is it's not working.
This is my code:
Function Consolidated Pinterest Image Function
function pinterest_image_snatcher($size = 'full' ) {
global $post;
if ( has_post_thumbnail($post->ID) ) {
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), $size);
return $featured_image[0];
}
else
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "http://www.mywebsite.com/wp-content/themes/Lord%20of%20Avernus%20-%20Abaddon/Images/Deafult_Img.png";
}
return $first_img;
}
The first two functions above is working really fine but the third one is not! Could anyone help to consolidate the two function above. Everyone is welcome to modify the codes.
Please help me out Dear PHP Experts. My Code is messed up and NOT working. Do you mind to modify this according to the flow chart? thank you!
How to add Pinterest button for WordPress Blogs
Try this:
function pinterest_image_snatcher( $size = 'full' ) {
global $post;
$first_img = '';
if ( has_post_thumbnail($post->ID) ) {
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), $size);
$first_img = $featured_image[0];
} else {
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches[1][0];
if( empty($first_img) ) {
$first_img = "http://www.mywebsite.com/wp-content/themes/Lord%20of%20Avernus%20-%20Abaddon/Images/Deafult_Img.png";
}
}
return $first_img;
}
I just fixed missing brackets as @royal-bg mentioned in the comments & changed logic a bit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With