Last active
October 4, 2022 02:39
-
-
Save danielribes/11097539 to your computer and use it in GitHub Desktop.
Ejemplo de uso del hook wp_head en Wordpress. Implementado como un plugin, este ejemplo añade el meta tag og:image al HEAD de los posts que tengan imagen destacada
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Image tag para Facebook | |
Plugin Uri: | |
Description: Añade en el HEAD de un post, el meta og:image para que Facebook use por defecto la imagen destacada | |
Version: 0.1 | |
Author: Daniel Ribes | |
Author Uri: http://www.danielribes.com | |
License: MIT License http://opensource.org/licenses/MIT | |
*/ | |
// activa el hook wp_head | |
add_action('wp_head', 'drfbimagetag_output'); | |
/** | |
* Genera el meta tag og:image si estamos en un post y este tiene imagen destacada | |
*/ | |
function drfbimagetag_output() | |
{ | |
// mira si es una pagina de post | |
if( is_single() ) | |
{ | |
// mira si tiene imagen destacada asociada | |
if( has_post_thumbnail() ) | |
{ | |
// recupera y muestra la URL absoluta de la imagen destacada | |
$url_fi = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID)); | |
echo '<meta property="og:image" content="'.$url_fi[0].'" />'; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment