WordPress.org

Ready to get started?Download WordPress

Forums

WordPress SEO by Yoast
[resolved] Get the custom seo description for og:description (20 posts)

  1. Beee
    Member
    Posted 3 years ago #

    I want to be able to 'pull' the custom description for a page, as set in the SEO plugin and use it in the header for og:description

    I want to use it for pages where the content field is not used, but only an iframe like http://www.djmonitor.com/live so I can't use the excerpt

  2. Beee
    Member
    Posted 3 years ago #

    After using this I saw the description is automaticly copied from the meta description, but I'ld still like to 'fill' that og:description tag, if possible.

  3. Joost de Valk
    Member
    Plugin Author

    Posted 3 years ago #

    It is possible by using WPSEO_Frontend::metadesc() I think.

  4. Beee
    Member
    Posted 3 years ago #

    I tested with
    WPSEO_Frontend::metadesc()
    $wpseo_front->metadesc()
    $wpseo_frontend->metadesc()
    nothing works

    WPSEO_Frontend::metadesc() inserts the normal meta tag (incl <meta> not just the value)

    $wpseo_front->metadesc() and $wpseo_frontend->metadesc() break the site

  5. Beee
    Member
    Posted 3 years ago #

    anyone have a solution ???

  6. Ahrengot
    Member
    Posted 3 years ago #

    Looks like it works when you send false as an argument to the function.

    in other words WPSEO_Frontend::metadesc(false); – I'm using that right now to create my og:description tags based on the meta description from WordPress SEO.

    WPSEO_Frontend::title(); grabs the title as well :)

    Here's the snippet i use to generate my open graph/Facebook tags with WordPress SEO

    <meta property="fb:admins" content="*Facebook ID*" />
    <mata property="og:author" content="Jens Ahrengot Boddum" />
    
    <?php
        	$og_title = get_bloginfo('name');
        	if (class_exists('WPSEO_Frontend')) $og_title = WPSEO_Frontend::title();
    
        	$og_description = get_bloginfo('description');
        	if (class_exists('WPSEO_Frontend')) $og_description = WPSEO_Frontend::metadesc(false);
    
        	$og_type = "article";
    		if (is_front_page()) $og_type = "website";
    
        	$og_url = get_permalink();
    	if (is_front_page()) $og_url = get_bloginfo('url');
    
        	echo '<meta property="og:type" content="' . $og_type . '"/>' . "\n";
        	echo '<meta property="og:url" content="' . $og_url . '"/>' . "\n";
        	echo '<meta property="og:site_name" content="' . get_bloginfo("name") . '"/>' . "\n";
        	echo '<meta property="og:title" content="' . $og_title . '" />' . "\n";
        	echo '<meta property="og:description" content="' . $og_description . '" />' . "\n";
    
        	if (has_post_thumbnail()){
    	    	global $post;
    	    	$img_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'thumbnail');
    	    	echo '<meta property="og:image" content="' . $img_src[0] . '" />' . "\n";
    	    }
        ?>
  7. Beee
    Member
    Posted 3 years ago #

    Cool. Will try it (when I get home)

  8. Beee
    Member
    Posted 3 years ago #

    when i tested I got this error

    Missing argument 1 for WPSEO_Frontend::title(),
    called in /***/fb-include.inc on line 42 and
    defined in <b>/***/wp-content/plugins/wordpress-seo/frontend/class-frontend.php</b>
    on line <b>86</b>
  9. Beee
    Member
    Posted 3 years ago #

    this works, so using that for now.... thnx

    if (class_exists('WPSEO_Frontend')) $og_description = WPSEO_Frontend::metadesc(false);
    echo '<meta property="og:description" content="' . $og_description . '" />' . "\n";
  10. Beee
    Member
    Posted 3 years ago #

    I ran into a new issue... what if $og_description is empty, so I adapted it a bit...

    $default_description = "BLA BLA BLA";
    if (class_exists('WPSEO_Frontend'))
    {
    	$og_description = WPSEO_Frontend::metadesc(false);
    	if (!$og_description)
    	{
    		$og_description = $default_description;
    	}
    }
    else
    {
    	$og_description = $default_description;
    }
    echo '<meta property="og:description" content="' . $og_description . '" />' . "\n";
  11. Ahrengot
    Member
    Posted 3 years ago #

    Good to hear it works. By the way, I don't think WPSEO_Frontend::metadesc(false) would ever return an empty string. As far as I know it grabs a description from within the content when you haven't manually created one.

    I could be wrong though ...

  12. Beee
    Member
    Posted 3 years ago #

    it will if no description is entered (it did on my site)...
    that's how I got to it...

  13. Ahrengot
    Member
    Posted 3 years ago #

    For me it just generates a meta description from my content when i don't manually enter one.

  14. Beee
    Member
    Posted 3 years ago #

    then you probably don't show the meta tag at all...
    that makes sense. because I noticed if the tag description is not used (and i mean not show at all, empty is not 'not used'), it takes the first x words/chars.

  15. Beee
    Member
    Posted 2 years ago #

    I ran into a new problem (probably due to updates on the plugin).

    The site breaks on this piece of code (which you can see in the source code on http://www.berryplasman.com/tag/wordpress/ because if I remove it, it works fine). I think WPSEO_Frontend::metadesc(false); breaks it but I hope Yoast can answer this...

    The error is "Using $this when not in object context in /public_html/wp-content/plugins/wordpress-seo/frontend/class-frontend.php on line 555"

    if (class_exists('WPSEO_Frontend'))
    	{
    		$og_description = WPSEO_Frontend::metadesc(false);
    		if ($og_description)
    		{
    			echo '<meta property="og:description" content="' . $og_description . '" />' . "\n";
    		}
    	}
    	else
    	{
    		$og_description = $default_description;
    	}
  16. Adam Capriola
    Member
    Posted 2 years ago #

    I got this error as well. I'm about to give up though and just go with the plugin defaults.

    I think you need to do something like this:

    $object = new WPSEO_Frontend();
    $og_title = $object->title();

    But I can't quite get it to work.

  17. Joost de Valk
    Member
    Plugin Author

    Posted 2 years ago #

    $wpseo = new WPSEO_Frontend();
    echo $wpseo->title();

    That should work.

  18. Joost de Valk
    Member
    Plugin Author

    Posted 2 years ago #

    (don't rely on this stuff though, it might change anytime)

  19. Beee
    Member
    Posted 2 years ago #

    Thanks Yoast, Adam also found it and explained it here.

  20. Beee
    Member
    Posted 2 years ago #

    I found out today Yoast's plugin does most of the work I described above... better look into that first ;)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic