Last active
July 19, 2022 23:21
-
-
Save chikuwa24/b785c6be50bd47a897ef7bd451db23c5 to your computer and use it in GitHub Desktop.
Blogcard for Wordpress - PHP
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 | |
//---------------------------------------// | |
// 外部&内部(wp)リンク対応ブログカード | |
// OpenGraph.phpを使用してます | |
//---------------------------------------// | |
// 端末がスマホの場合はtrueを返す | |
function is_mobile() { | |
$useragents = array( | |
'iPhone', // iPhone | |
'iPod', // iPod touch | |
'Android.*Mobile', // 1.5+ Android *** Only mobile | |
'Windows.*Phone', // *** Windows Phone | |
'dream', // Pre 1.5 Android | |
'CUPCAKE', // 1.5+ Android | |
'blackberry9500', // Storm | |
'blackberry9530', // Storm | |
'blackberry9520', // Storm v2 | |
'blackberry9550', // Storm v2 | |
'blackberry9800', // Torch | |
'webOS', // Palm Pre Experimental | |
'incognito', // Other iPhone browser | |
'webmate' // Other iPhone browser | |
); | |
$pattern = '/'.implode('|', $useragents).'/i'; | |
return preg_match($pattern, $_SERVER['HTTP_USER_AGENT']); | |
} | |
// 端末判定 xs: SmartPhone / sm:Tablet / pc: Pc | |
function is_my_mobile() { | |
$size = $_SESSION[windowSize]; | |
if($size == 0) { | |
if(is_mobile()) {return 'xs';} | |
elseif(wp_is_mobile()) {return 'sm';} | |
else {return 'pc'; } | |
} | |
elseif($size <= 767) {return 'xs';} | |
elseif(768 <= $size && $size <= 991) {return 'sm';} | |
else {return 'pc'; } | |
} | |
// 端末別 文字数編集 | |
function get_formed_str($str, $type) { | |
// 文字数設定(マルチバイト文字数) | |
$arrc = array( | |
't' => array( // Title | |
'xs' => 20, // SmartPhone | |
'xm' => 40, // Tablet | |
'pc' => 40 // Pc | |
), | |
'd' => array( // Description | |
'xs' => 30, // SmartPhone | |
'xm' => 55, // Tablet | |
'pc' => 55 // Pc | |
) | |
); | |
$estr = mb_substr($str, 0, $arrc[$type][is_my_mobile()]); | |
if (mb_strlen($str) > $arrc[$type][is_my_mobile()]) $estr.='…'; | |
return $estr; | |
} | |
// 記事IDを指定して抜粋文を取得する | |
function ltl_get_the_excerpt($post_id){ | |
global $post; | |
$post = get_post($post_id); | |
setup_postdata($post_id); | |
// $output = get_the_excerpt(); // 通常 | |
// $output = get_post_meta($post_id,'_yoast_wpseo_metadesc',true); // YoastSEO | |
$output = get_post_meta($post_id,'_aioseop_description',true); // AllinOneSEO | |
return $output; | |
} | |
//ブログカード生成 | |
function nlink_scode($atts) { | |
extract(shortcode_atts(array( | |
'url'=>"", | |
'title'=>"", | |
'excerpt'=>"" | |
),$atts)); | |
//アイキャッチ画像がない場合の画像を指定 | |
$no_image = 'https://*********/noimage.jpg'; | |
if(strpos($url,'http') !== false) { | |
//絶対パスの場合(外部リンク) | |
// OpenGraph(OGP情報取得) | |
$graph = OpenGraph::fetch($url); | |
if ($graph) { | |
$title = $graph->title; | |
$description = $graph->description; | |
$image = $graph->image; | |
} | |
// 下部にURLを表示する | |
$bottom_url = $url; | |
//アイキャッチ画像を取得 | |
$img_path = (strlen($image) > 1 ) ? $image : $no_image; | |
// 別タブで開く | |
$target = 'target="_blank" rel="noopener noreferrer"'; | |
}else{ | |
//相対パスの場合(自サイト) | |
$host = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://') . $_SERVER['HTTP_HOST']; | |
$url = $host.$url; | |
$more = ' more'; // 記事を読む>> を表示 | |
$id = url_to_postid($url); //URLから投稿IDを取得 | |
//タイトルを取得 | |
if(empty($title)){ | |
$title = esc_html(get_the_title($id)); | |
} | |
//説明文を取得 | |
if(empty($description)){ | |
$description = esc_html(ltl_get_the_excerpt($id)); | |
} | |
//アイキャッチ画像を取得 | |
if(has_post_thumbnail($id)) { | |
$img = wp_get_attachment_image_src(get_post_thumbnail_id($id), 'medium'); | |
$img_path = $img[0]; | |
}else{ | |
$img_path = $no_image; | |
} | |
} | |
// HTML生成 | |
$nlink .=' | |
<div class="blogcard '.$more.'"> | |
<a href="'. $url .'" '. $target .'> | |
<div class="photo"><img src="'.$img_path.'" alt="" /></div> | |
<div class="description"> | |
<div class="blogcard-title">'. get_formed_str($title, 't') .' </div> | |
<div class="blogcard-description">'. get_formed_str($description, 'd') .'</div> | |
</div> | |
<div class="bottom-url">'.$bottom_url.'</div> | |
</a> | |
</div>'; | |
return $nlink; | |
} | |
add_shortcode("nlink", "nlink_scode"); | |
//---------------------------------------// | |
// 外部&内部(wp)リンク対応ブログカード | |
//---------------------------------------// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment