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?