Ask your WordPress questions! Pay money and get answers fast! (more info)

Facebook OG Description no excerpt

  • SOLVED

Here is what I use now, which was provided years ago as an answer from this site. Works Great still!


function get_att_images($args = array()) {
global $post;
$defaults = array(
'format' => 'url',
'size' => 'full',
'num' => 0,
'attr' => '',
);

$args = wp_parse_args( $args, $defaults );
if ( has_post_thumbnail() && ( 0 === $args['num'] ) ) {
$id = get_post_thumbnail_id();
$html = wp_get_attachment_image( $id, $args['size'], false, $args['attr'] );
list( $url ) = wp_get_attachment_image_src( $id, $args['size'], false, $args['attr'] );
} else {
$id = _get_image_id( $args['num'] );
$html = wp_get_attachment_image( $id, $args['size'], false, $args['attr'] );
list( $url ) = wp_get_attachment_image_src( $id, $args['size'], false, $args['attr'] );
}
$src = str_replace( home_url(), '', $url );

if ( 'html' === strtolower( $args['format'] ) )
$output = $html;
elseif ( 'url' === strtolower( $args['format'] ) )
$output = $url;
else
$output = $src;

if ( empty( $url ) ) $output = false;
return $output;
}

function insert_fb_in_head() {

global $post;

if ( !is_singular()) //if it is not a post or a page
return;
echo '<meta property="fb:app_id" content="" />';
echo '<meta property="fb:admins" content=""/>';
echo '<meta property="og:url" content="' . get_permalink() . '"/>';
echo '<meta property="og:locale" content="en_US" />';
echo '<meta property="og:title" content="' . get_the_title() . '"/>';
echo '<meta property="og:type" content="article"/>';
echo '<meta property="og:site_name" content=""/>';
$post_thumb = get_att_images();
echo '<meta property="og:image" content="' .( ($post_thumb) ? $post_thumb : '/images/generic-logo.png' ). '" />';
echo "\n";
}

add_action( 'wp_head', 'insert_fb_in_head', 5 );



What I am missing and can't seem to figure out is I need to add the field:
echo '<meta property="og:description" content="" />';

I need it to pull from the post or pages content, not excerpt, since I do not use the excerpt field. I would also need it to limit how much it pulls. If there is no content I need it to pull in a generic sentence.

Answers (3)

2016-03-10

Reigel Gallarde answers:

try this for your insert_fb_in_head function...

function insert_fb_in_head() {
global $post;
if ( !is_singular()) //if it is not a post or a page
return;

echo '<meta property="fb:app_id" content="" />';
echo '<meta property="fb:admins" content=""/>';
echo '<meta property="og:url" content="' . get_permalink() . '"/>';
echo '<meta property="og:locale" content="en_US" />';
echo '<meta property="og:title" content="' . get_the_title() . '"/>';
echo '<meta property="og:type" content="article"/>';
echo '<meta property="og:site_name" content=""/>';
$post_thumb = get_att_images();
echo '<meta property="og:image" content="' .( ($post_thumb) ? $post_thumb : '/images/generic-logo.png' ). '" />';
setup_postdata( $post );
$description = get_the_excerpt();
echo '<meta property="og:description" content="' .( ($description) ? $description : 'Some generic sentence!' ). '" />';
echo "\n";

}


Reigel Gallarde comments:

to limit to a certain number of words, you can use wp_trim_words. This function trims text to a certain number of words and returns the trimmed text.

$description = wp_trim_words( get_the_excerpt(), 20, '' );

2016-03-05

Luis Abarca answers:

You can use if_* tags to generate the description tag.


<?php

$description = '';
$default_description = 'your text here';

if (is_singular()) {
$description = get_the_excerpt();
} else if (is_category()) {
$description = category_description();
} else {
$description = get_bloginfo('description');
}

if ( empty($description) ) {
$description = $default_description;

echo '<meta property="og:description" content="' . $description . '" />';



TWDesigns comments:

Greetings,

For what ever reason when the get_the_excerpt is used it doesn't pull in data. It's my understanding that if I don't use the excerpt field then it's supposed to pull in X characters stripped from the content.

My posts are listings with custom fields and the content is about the company. Thus some of my posts have no content and as mentioned I would like to add, if possible, a statement that if no content is available then it use a generic og:description, as mentioned in my original question.

Thanks!


TWDesigns comments:

Can you add this to my existing function. I think it will then solve my issues :)


Luis Abarca comments:

Sure.


function insert_fb_in_head()
{
global $post;

$description = '';
$default_description = 'your text here';

if ( !is_singular()) //if it is not a post or a page
return;

echo '<meta property="fb:app_id" content="" />';
echo '<meta property="fb:admins" content=""/>';
echo '<meta property="og:url" content="' . get_permalink() . '"/>';
echo '<meta property="og:locale" content="en_US" />';
echo '<meta property="og:title" content="' . get_the_title() . '"/>';
echo '<meta property="og:type" content="article"/>';
echo '<meta property="og:site_name" content=""/>';

$post_thumb = get_att_images();
echo '<meta property="og:image" content="' .( ($post_thumb) ? $post_thumb : '/images/generic-logo.png' ). '" />';
echo "\n";

if (is_singular()) {
$description = get_the_excerpt();
} else if (is_category()) {
$description = category_description();
} else {
$description = get_bloginfo('description');
}

if ( empty($description) ) {
$description = $default_description;
}

echo '<meta property="og:description" content="' . $description . '" />';

}

add_action( 'wp_head', 'insert_fb_in_head', 5 );


TWDesigns comments:

My issue still remains. When I applied the above it only pulls in the custom text even though the post has content. Suggestions?

2016-03-10

Alvaro Rosado answers:

Edit*
sorry, answered twice.


Alvaro Rosado comments:

Try this:



// Function to limit the content words
function limit_content_words($string, $word_limit) {
$words = explode(' ', $string, ($word_limit + 1));
if(count($words) > $word_limit)
array_pop($words);
return implode(' ', $words);
}

function get_att_images($args = array()) {

global $post;

$defaults = array(

'format' => 'url',

'size' => 'full',

'num' => 0,

'attr' => '',

);

$args = wp_parse_args( $args, $defaults );

if ( has_post_thumbnail() && ( 0 === $args['num'] ) ) {

$id = get_post_thumbnail_id();

$html = wp_get_attachment_image( $id, $args['size'], false, $args['attr'] );

list( $url ) = wp_get_attachment_image_src( $id, $args['size'], false, $args['attr'] );

} else {

$id = _get_image_id( $args['num'] );

$html = wp_get_attachment_image( $id, $args['size'], false, $args['attr'] );

list( $url ) = wp_get_attachment_image_src( $id, $args['size'], false, $args['attr'] );

}

$src = str_replace( home_url(), '', $url );



if ( 'html' === strtolower( $args['format'] ) )

$output = $html;

elseif ( 'url' === strtolower( $args['format'] ) )

$output = $url;

else

$output = $src;



if ( empty( $url ) ) $output = false;

return $output;

}



function insert_fb_in_head() {



global $post;



if ( !is_singular()) //if it is not a post or a page

return;

echo '<meta property="fb:app_id" content="" />';

echo '<meta property="fb:admins" content=""/>';

echo '<meta property="og:url" content="' . get_permalink() . '"/>';

echo '<meta property="og:locale" content="en_US" />';

echo '<meta property="og:title" content="' . get_the_title() . '"/>';

echo '<meta property="og:type" content="article"/>';

echo '<meta property="og:site_name" content=""/>';


$content = get_the_content();
// 25 is the word limit, you can change this.
echo '<meta property="og:description" content="' . limit_content_words($content,25) . '" />';


$post_thumb = get_att_images();

echo '<meta property="og:image" content="' .( ($post_thumb) ? $post_thumb : '/images/generic-logo.png' ). '" />';

echo "\n";

}

add_action( 'wp_head', 'insert_fb_in_head', 5 );


TWDesigns comments:

This pulled in a blank og:description on both posts with and without content through the object debugger from facebook. I'm not sure why it was blank both times as I tested it on several posts with content.

Thanks