1

I'm trying to make a mu-plugin dedicated to make OpenGraph tags for my website, since the current plugin in WP directory arent updated for ages.

But i ran with several headaches trying to get post or page data, something i never tried before.

Using this piece of code:

<?php
/*
    Plugin Name: Open Graph!
    Description: Adiciona tags Open Graph (Facebook) para o site.
    Author: ChronoMania Team
    Version: 1.0
    Author URI: http://chronomania.com.br
*/

function pegarConteudoPost()
{
    if (is_single())
    {
        $texto = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id));
        if empty($texto)
        {
            $texto = wp_trim_words($post->post_content);
        }
        if empty($texto)
        {
            $texto = get_bloginfo('description');
        }
    }
    elseif (is_page())
    {
        $texto = wp_trim_words($page->post_content);
    }
    else
    {
        $texto = get_bloginfo('description');
    }
    return $texto;
}

function pegaURLAtual()
{
    $link = 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
    return $link;
}

function montaOpenGraph()
{
    echo '<meta property="og:title" content="' . wp_title("", false) . '">';
    echo '<meta property="og:site_name" content="' . get_bloginfo('name') . '">';
    echo '<meta property="og:url" content="' . pegaURLAtual() . '">';
    echo '<meta property="og:description" content="' . pegarConteudoPost() . '">';
    echo '<meta property="og:image" content="' . get_template_directory_uri() . '/img/social/ogp_logo.png">';
    echo '<meta property="og:type" content="article">';
}

// Registrando funções
add_action('wp_head', 'montaOpenGraph');

?>

It gives me error 500. So i managed to trace the error to the function pegarConteudoPost().

As i stated before, i dont have much experience with php (so the jury rig code) and never need to work with data outside the loop, so i dig some code from google and put there, with no results.

Any ideas to how can i make this work?

1

You're using non-existent variables in your function, $post_id, $post, $page.

Use get_queried_object to get data from the current page.

if( is_single() || is_page() ){
    $this_page = get_queried_object();
    $excerpt = get_post_field( 'post_excerpt', $this_page->ID );
}
  • Thanks for pointing the error! I get this snippets from WP forum, never imagine a answer considered "correct" for the folks there turned to be a farce. Well, another noob question: will this always return some text, or i need to set an excerpt for every post i make? And excerpts apply for pages? Thanks in advance! – Vico Jan 3 '16 at 23:53
  • Sadly the answer as it is dont solved my problem, since i request to get data from the post, but what i have instead of post content is just empty string – Vico Jan 4 '16 at 8:30
0

After several headaches i managed to solve the issue based on Milo's answer. But i need to add a check if the excerpt returned empty, and if it return empty i need to trim the entire content string to get the desired data.

Final code:

function pegarConteudoPost()
{
    if( is_single() || is_page() )
    {
        $this_page = get_queried_object();
        $excerpt = get_post_field( 'post_excerpt', $this_page->ID );

        if ($excerpt === '')
        {
            $excerpt = wp_trim_words(get_post_field( 'post_content', $this_page->ID ), 55);
        }

        $texto = $excerpt;
    }
    else
    {
        $texto = get_bloginfo('description');
    }
    return $texto;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.