我有一段时间以前的这个类,它在使用admin_head
行动挂钩
<?php
/**
* Simple facebook open graph class for WordPress
*
* @author Ohad Raz
* @version 0.1
*
*/
class simple_open_graph{
/**
* Holds the default image url
* @var string
* @since 0.1
*/
public $default_image_url;
/**
* holds facebook user id for insights (analytics of the Like Buttons)
* @var string
* @since 0.1
*/
public $facebook_account_id;
/**
* Class Constructor
*
* @since 0.1
* @author Ohad Raz
* @access public
*
* @return Void
*/
public function __construct(){
$this->default_image_url = "http://www.example.com/image.jpg";
$this->facebook_account_id = "Your Facebook Account ID";
add_action( \'wp_head\', array($this,\'facebook_open_graph_meta_head\'), 5 );
}
/**
* function to get the content type
*
* @since 0.1
* @author Ohad Raz
* @access public
*
* @return (string) either website or article based on the curent page
*/
public function content_type(){
if (is_single() || is_page())
return "article";
else
return "website";
}
/**
* function to get the image from post_thumb, post content or default image
*
* @since 0.1
* @author Ohad Raz
* @access public
*
* @return (string) image src
*/
public function graph_image(){
if ((function_exists(\'has_post_thumbnail\')) && (has_post_thumbnail())) {
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'\', \'\' );
return $src[0];
} else {
global $post, $posts;
$fbimage = \'\';
$output = preg_match_all(\'/<img.+src=[\\\'"]([^\\\'"]+)[\\\'"].*>/i\',
$post->post_content, $matches);
return $matches [1] [0];
}
return $this->$default_image_url;
}
/**
* Function that actually prints the open graph meta tags
*
* @since 0.1
* @author Ohad Raz
* @access public
*
* @return Void
*/
public function facebook_open_graph_meta_head() {
global $post;
echo \'<meta property="fb:admins" content="\'. $facebook_account_id .\'"/>\'; ?>
<meta property="og:title" content="
<?php
if(is_home()) {
bloginfo(\'name\');
} elseif(is_category()) {
echo single_cat_title();
} elseif(is_author()) {
$curauth = (get_query_var(\'author_name\')) ? get_user_by(\'slug\', get_query_var(\'author_name\')) : get_userdata(get_query_var(\'author\'));
echo $curauth->display_name;
} else {
echo the_title();
} ?>" />
<meta property="og:description" content="<?php echo strip_tags(get_the_excerpt($post->ID)); ?>"/>
<meta property="og:url" content="<?php the_permalink(); ?>"/>
<meta property="og:image" content="<?php echo $this->graph_image(); ?>"/>
<meta property="og:type" content="<?php echo $this->content_type(); ?>"/>
<meta property="og:site_name" content="<?php bloginfo(\'name\'); ?>"/>
<?php
}
}//end class