Vicky Arulsingam
Forum Replies Created
-
Forum: Plugins
In reply to: [Post Avatar] Problem with custom function that adds Post Avatar og:imageThat is possible. I’ll have to double check their code. Perhaps we should take this to email since this isn’t a Post Avatar issue ??
My email is [email protected].
Forum: Plugins
In reply to: [Post Avatar] Problem with custom function that adds Post Avatar og:imageSo the first section is looking for an image at least 199 pixels for both dimensions, right? It actually needs to be at least 200px in each dimension, so I can change that.
You don’t need to change it. It’s looking for an image with height or width that is greater than 199 i.e. 200px
Why does it need to look again for another image and check its size, if it does that first?
If you’ve placed any
<img>
tags within the post content, it will get an image from there. You can omit this if you want.However, there is a potential problem I spotted: In that second function, you set $og_image = ”; at the start, and then it checks for post image being the required
You’ll note that the last bit of code displays the default image so that will be returned by the code. You can always set the default image url first.
This is the revised second function without checking for the images in post content as well as default url set at the beginning.
function gklpa_get_ogimage( $min_dimension = 199, $size = 'full' ){ global $post; $og_image = 'URL TO DEFAULT IMAGE'; if( is_single() ) { $featured_image = gklpa_prep_featured_image( $min_dimension, $size ); // featured image meets criteria so no need to proceed if( $featured_image != '' ) return; $post_avatar = gklpa_prep_post_avatar( $min_dimension ); // post avatar meets criteria so set that as open graph image if( $post_avatar != '' ){ $og_image = $post_avatar; } return $og_image; } }
Forum: Plugins
In reply to: [Post Avatar] Problem with custom function that adds Post Avatar og:imageThe code is bit long as I’ve split it into different functions that check for the following:
1. Determine if featured image (based on defined attachment size) meets the dimension criteria. If it does then there’s no need to do anything.
2. If the featured image does not meet the defined criteria then the code checks for the post avatar image.
3. If the post avatar image does not exist, the code will look for any images included within the content and return the first image that meets the dimension criteria.
4. If there’s still nothing available, then show the default image.You can set the image dimensions to check for and what type of attachment size you want to verify against in
$valid_og_image = gklpa_get_ogimage( 199, 'full' );
Set the default URL in$og_image = esc_url( 'URL to default image' );
add_action( 'wp_head', 'gklpa_ogimage', 99 ); /* * Display the open graph image meta */ function gklpa_ogimage(){ $og_image_meta = ''; $valid_og_image = gklpa_get_ogimage( 199, 'full' ); if( $valid_og_image != '' ) $og_image_meta = '<meta property="og:image" content="' . esc_url( $valid_og_image ) . '" />' . "\n"; echo $og_image_meta; } /* * Function that checks for an additional image to include in the open graph meta (either post avatar, images in content or default image) * * @param int $min_dim Value to check height/width against * @param string $size Image attachment size to check (for featured images) e.g. thumbnail, medium, full. Defaults to 'full' * @returns string $og_image Valid open graph image * */ function gklpa_get_ogimage( $min_dimension = 199, $size = 'full' ){ global $post; $og_image = ''; if( is_single() ) { $featured_image = gklpa_prep_featured_image( $min_dimension, $size ); // featured image meets criteria so no need to proceed if( $featured_image != '' ) return; $post_avatar = gklpa_prep_post_avatar( $min_dimension ); // post avatar meets criteria so set that as open graph image if( $post_avatar != '' ){ $og_image = $post_avatar; } else { // check for valid images within the post $og_image = gklpa_prep_content_image( $min_dimension ); } // No valid image so let's call the default; if( $og_image == '' ){ $og_image = esc_url( 'URL to default image' ); } return $og_image; } } /* * Ensures that the featured image meets the image dimension criteria * * @param int $min_dim Value to check height/width against * @param string $size Image attachment size to return e.g. thumbnail, medium, full. Defaults to 'full' * @returns string $image_url Valid featured image url or empty string * */ function gklpa_prep_featured_image( $min_dim, $size = 'full' ){ global $post; $image_url = ''; // get attachment id of featured image $image_id = get_post_thumbnail_id( $post->ID ); if ( $image_id != '' ) { // featured image exists so get image details based on attachment size $image = wp_get_attachment_image_src( $image_id, $size ); $width = $image[1]; $height = $image[2]; // make sure featured image meets required dimensions and set url if( $width > $min_dim || $height > $min_dim ){ $image_url = $image[0]; } } return $image_url; } /* * Ensures that the post avatar image meets the image dimension criteria * * @param int $min_dim Value to check height/width against * @returns string $avatar_url Valid post avatar url or empty string * */ function gklpa_prep_post_avatar( $min_dim ){ global $post; $avatar_array = gkl_get_postavatar($post); $avatar_url = ''; // No Post Avatar so get out of here if( is_null( $avatar_array ) ) { return; // Post Avatar exists return the URL } else { $avatar_url = esc_url( $avatar_array['avatar_url'] ); } return $avatar_url; } /* * Checks post content for images and ensures that image meets the image dimension criteria * Based on https://css-tricks.com/snippets/wordpress/get-the-first-image-from-a-post/ * * @param int $min_dim Value to check height/width against * @returns string $image_url Valid image url or empty string * */ function gklpa_prep_content_image( $min_dim ) { global $post, $posts; $image_url = ''; $image_list = array(); ob_start(); ob_end_clean(); $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); // Get images from post content if( $matches ){ foreach( $matches[1] as $image_url ){ // get image dimensions list($width, $height, $type, $attr) = getimagesize( esc_url( $image_url) ); // return the first image that meets criteria if( $width > $min_dim || $height > $min_dim ) { return esc_url( $image_url ); } } } }
Forum: Plugins
In reply to: [Post Avatar] Problem with custom function that adds Post Avatar og:imageLet me look into this. I’ll have some code for you to try out later.
Forum: Plugins
In reply to: [Post Avatar] Problem with custom function that adds Post Avatar og:imageRegarding the additional section of code, that is something you should take up with the WordPress SEO plugin if it’s necessary or not. I not familiar with the open graph protocol to give you an adequate answer.
But if it is, all you’d need to do is drop that code in your functions.php
As for the Post Avatar, you don’t need to use
return;
after setting the$og_image
. So the following code will work, only outputting the post avatar image meta info if it exists.add_action( 'wp_head', 'gklpa_ogimage', 99 ); function gklpa_ogimage(){ global $post; $og_image = ''; // Only display on the post itself if( is_single() ){ $avatar_array = gkl_get_postavatar($post); // No Post Avatar so get out of here if( is_null( $avatar_array ) ) { return; // Post Avatar exists so let's display it } else { $og_image = '<meta property="og:image" content="' . esc_url( $avatar_array['avatar_url'] ) . '" />' . "\n"; echo $og_image; } } }
Forum: Plugins
In reply to: [Post Avatar] Problem with custom function that adds Post Avatar og:imageYour code is correct.
Forum: Plugins
In reply to: [Post Avatar] Problem with custom function that adds Post Avatar og:imageWordPress SEO is supposed to set the default image, however, I noticed that since we added it to the function code, it’s only showing up once, so I guess the function’s default is overriding the other one.
That’s what is happening. Let’s revise the code so that if there isn’t a post avatar it will just fall back to the default image generated by WordPress SEO.
// No post avatar, don't do anything if( isnull( $avatar_array ) ) { return; } else { $og_image = '<meta property="og:image" content="' . esc_url( $avatar_array['avatar_url'] ) . '" />' . "\n"; }
Forum: Plugins
In reply to: [Post Avatar] Problem with custom function that adds Post Avatar og:imageDo you have any other code that adds the default image?
In my test, if a featured image has been set, default image is not created within
<head>
(unlike in link #1)Same thing goes when a post avatar is set, no default image is included in the
<head>
code.Forum: Plugins
In reply to: [Post Avatar] Problem with custom function that adds Post Avatar og:imageWhat is the size of the default image in relation to post avatar image?
Can you provide me some links:
1) Post with featured image
2) Post with post avatar image
3) Post with default imageForum: Plugins
In reply to: [Post Avatar] Problem with custom function that adds Post Avatar og:imageDoes the open graph code all need to be together?
e.g.
<head> <meta property="og:title" content="Post Title" /> <meta property="og:type" content="article" /> <meta property="og:url" content="https://url/to/post/" /> <meta property="og:image" content="https://url/to/post/avatar/image" /> <!-- other meta content here --> </head>
or can it be spread out
e.g.<meta property="og:title" content="Post Title" /> <meta property="og:type" content="article" /> <meta property="og:url" content="https://url/to/post/" /> <!-- other meta content here --> <meta property="og:image" content="https://url/to/post/avatar/image" />
If the latter one is allowed you can change this:
add_action( 'wpseo_opengraph', 'gklpa_ogimage', 99 );
to
add_action( 'wp_head', 'gklpa_ogimage', 99 );
Forum: Plugins
In reply to: [Post Avatar] Problem with custom function that adds Post Avatar og:imageNo, Featured Image refers to an image selected from the Media Library that you set as the “Featured Image”.
I’ll look further into how WordPress SEO does it and make code revisions.
Forum: Plugins
In reply to: [Post Avatar] Problem with custom function that adds Post Avatar og:imageIs the Add to Any button different from the WordPress SEO plugin?
If I understand correctly, on posts without avatars, you want the main image (part of the post content) to show up on the link instead of the default?
Because in the current code setup which image will be shown follows this heirarchy:
Featured Image
Post Avatar
Default ImageImages that are part of the post content will not be taken into account.
Can you give me further details on how you include images? Are they added from attachments in the Media Library or are they external images?
Forum: Plugins
In reply to: [Post Avatar] Problem with custom function that adds Post Avatar og:imageThat bit is to display the post avatar if it exists.
You’ll need to change the bit of code
isnull(
tois_null(
I forgot to put in the underscore.This is what I get for writing code on my phone. ??
Forum: Plugins
In reply to: [Post Avatar] Problem with custom function that adds Post Avatar og:imageSorry about that.
There shouldn’t be a space between the $ and avatar_array
add_action( 'wpseo_opengraph', 'gklpa_ogimage', 99 ); function gklpa_ogimage(){ global $post; $og_image = ''; // featured image exists, no need to proceed if( has_post_thumbnail( $post->ID ) ) return; // Only display on the post itself if( is_single() ){ $avatar_array = gkl_get_postavatar($post); // If there's not post avatar set a default image if( isnull( $avatar_array ) ) { $og_image = '<meta property="og:image" content="default url here" />'; } else { $og_image = '<meta property="og:image" content="' . esc_url( $avatar_array['avatar_url'] ) . '" />' . "\n"; } } echo $og_image; }
Forum: Plugins
In reply to: [Post Avatar] Problem with custom function that adds Post Avatar og:imageThis is how the code should look.
add_action( 'wpseo_opengraph', 'gklpa_ogimage', 99 ); function gklpa_ogimage(){ global $post; $og_image = ''; // featured image exists, no need to proceed if( has_post_thumbnail( $post->ID ) ) return; // Only display on the post itself if( is_single() ){ $avatar_array = gkl_get_postavatar($post); // If there's not post avatar set a default image if( isnull( $ avatar_array ) ) { $og_image = '<meta property="og:image" content="default url here" />'; } else { $og_image = '<meta property="og:image" content="' . esc_url( $avatar_array['avatar_url'] ) . '" />' . "\n"; } } echo $og_image; }