WordPressではショートコードを自作して
好きなときに使う事が出来ます。ショート
コードを利用すると、通常記事内では出来
ない事もできる様になったり、面倒なマーク
アップを簡略化できたりと、とても便利
ですので、ぜひ覚えてみてください。
という訳で、WordPressで作っておくと便利なショートコードをいくつかご紹介します。
その前に宣伝。
WordPressのスニペットを集めてシェアする事にしました
ご存知のように、WordPressで使える便利なコードは日々世界中で公開、シェアされています。その背景にWordPressがOSSであることが理由の一つとしてあげられるかと思います。
WordPressはGPLですので、極論を言えば有料のプラグインも無料で配布可能です。このあたりがGPLが一部の方に嫌われる原因でもありますが、沢山のコードを割と自由に使う事が出来るのも利点の一つです。
そのGPLライセンスの特性を利用して、様々な場所で見かける便利なスニペットコードを日本語でまとめてシェアする事にしました。
作った理由は、ある人との約束でもあるのでその方は勿論、多くの方にお役に立てて頂ければ幸いです。
ただし、全てが正しいコードである保障はありません。また、コードも古くなります。ですので、間違ったコードがあればご連絡頂きたく思います。記事ごとにフォームを用意していますのでぜひご一報下さい。
見た目を調整する暇が無かったので今はちょっと見にくい、使いにくいと思いますが、少しずつ調整していこうと思います。
WordPress カスタマイズ スニペット
余談
尚、作りはjQuery スニペット と同じです。こうしたサブコンテンツの作り方は以前記事を書いたのであわせてご参照下さい。
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
function Include_my_php($params = array()) {
extract(shortcode_atts(array(
'file' => 'default'
), $params));
ob_start();
include(get_theme_root() . '/' . get_template() . "/$file.php");
return ob_get_clean();
}
add_shortcode('myphp', 'Include_my_php');
?>
<?php
function Include_my_php($params = array()) {
extract(shortcode_atts(array(
'file' => 'default'
), $params));
ob_start();
include(get_theme_root() . '/' . get_template() . "/$file.php");
return ob_get_clean();
}
add_shortcode('myphp', 'Include_my_php');
?>
via:How to Develop a PHP File Include
ショートコードでQRコード
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
function add_qrcode_in_this_article($atts) {
extract(shortcode_atts(array(
'url' => 'http://example.com',
'size' => '80',
), $atts));
return '<img src="https://chart.googleapis.com/chart?chs=' . $size . 'x' . $size . '&cht=qr&chl=' . $url . '&choe=UTF-8 " alt="QR Code"/>';
}
add_shortcode('myqrcode', 'add_qrcode_in_this_article');
?>
/*ショートコードは [myqrcode url="http://foo.net" size="150"] と書く。*/
<?php
function add_qrcode_in_this_article($atts) {
extract(shortcode_atts(array(
'url' => 'http://example.com',
'size' => '80',
), $atts));
return '<img src="https://chart.googleapis.com/chart?chs=' . $size . 'x' . $size . '&cht=qr&chl=' . $url . '&choe=UTF-8 " alt="QR Code"/>';
}
add_shortcode('myqrcode', 'add_qrcode_in_this_article');
?>
/*ショートコードは [myqrcode url="http://foo.net" size="150"] と書く。*/
簡易的なサイトマップを構築
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
function simple_sitemap(){
global $wpdb;
$args = array('depth' => 0,
'show_date' => NULL,
'date_format' => get_option('date_format'),
'child_of' => 0,
'exclude' => NULL,
'include' => NULL,
'title_li' => '<span class="subheader">固定ページの一覧</span>',
'echo' => 1,
'authors' => NULL,
'sort_column' => 'menu_order, post_title',
'link_before' => NULL,
'link_after' => NULL,
'exclude_tree' => NULL );
echo '<div id="sitemap"><ul>';
wp_list_pages($args);
echo '</ul>';
$args = array('show_option_all' => NULL,
'orderby' => 'name',
'order' => 'ASC',
'show_last_update' => 0,
'style' => 'list',
'show_count' => 0,
'hide_empty' => 1,
'use_desc_for_title' => 1,
'child_of' => 0,
'feed' => NULL,
'feed_type' => NULL,
'feed_image' => NULL,
'exclude' => NULL,
'exclude_tree' => NULL,
'include' => NULL,
'hierarchical' => true,
'title_li' => '<span class="subheader">記事カテゴリ</span>',
'number' => NULL,
'echo' => 1,
'depth' => 0,
'current_category' => 0,
'pad_counts' => 0,
'taxonomy' => 'category',
'walker' => 'Walker_Category' );
echo '<ul>';
echo wp_list_categories( $args );
echo '</ul>';
echo '</div>';
}
add_shortcode('sitemap', 'simple_sitemap');
?>
/*ショートコードで[sitemap]と書くだけ*/
<?php
function simple_sitemap(){
global $wpdb;
$args = array('depth' => 0,
'show_date' => NULL,
'date_format' => get_option('date_format'),
'child_of' => 0,
'exclude' => NULL,
'include' => NULL,
'title_li' => '<span class="subheader">固定ページの一覧</span>',
'echo' => 1,
'authors' => NULL,
'sort_column' => 'menu_order, post_title',
'link_before' => NULL,
'link_after' => NULL,
'exclude_tree' => NULL );
echo '<div id="sitemap"><ul>';
wp_list_pages($args);
echo '</ul>';
$args = array('show_option_all' => NULL,
'orderby' => 'name',
'order' => 'ASC',
'show_last_update' => 0,
'style' => 'list',
'show_count' => 0,
'hide_empty' => 1,
'use_desc_for_title' => 1,
'child_of' => 0,
'feed' => NULL,
'feed_type' => NULL,
'feed_image' => NULL,
'exclude' => NULL,
'exclude_tree' => NULL,
'include' => NULL,
'hierarchical' => true,
'title_li' => '<span class="subheader">記事カテゴリ</span>',
'number' => NULL,
'echo' => 1,
'depth' => 0,
'current_category' => 0,
'pad_counts' => 0,
'taxonomy' => 'category',
'walker' => 'Walker_Category' );
echo '<ul>';
echo wp_list_categories( $args );
echo '</ul>';
echo '</div>';
}
add_shortcode('sitemap', 'simple_sitemap');
?>
/*ショートコードで[sitemap]と書くだけ*/
via:
WordPress Simple Sitemap
ログインユーザーにだけ見せるコンテンツ
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
add_shortcode("hide","hide_shortcode");
function hide_shortcode($x,$text=null){
if(!is_user_logged_in()){
return "ここは非ログインユーザーに見せるテキストです。";
}else{
return do_shortcode($text);
}
}
?>
/* [hide]ここに、ログインユーザーにのみ見せるコンテンツを書く[/hide]
非ログインユーザーには設定したテキストを表示する */
<?php
add_shortcode("hide","hide_shortcode");
function hide_shortcode($x,$text=null){
if(!is_user_logged_in()){
return "ここは非ログインユーザーに見せるテキストです。";
}else{
return do_shortcode($text);
}
}
?>
/* [hide]ここに、ログインユーザーにのみ見せるコンテンツを書く[/hide]
非ログインユーザーには設定したテキストを表示する */
via:
Show content for logged in users
リンク先のスクリーンショットをAPIで実装
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
function api_sc_shot ($attributes) {
extract(shortcode_atts(array(
'url' => '',
), $attributes));
$imageUrl = sc_shot ($url);
if ($imageUrl == '') {
return '';
} else {
return '<img src="' . $imageUrl . '" alt="' . $url . '" />';
}
}
function sc_shot ($url = ''){
return 'http://s.wordpress.com/mshots/v1/' . urlencode(clean_url($url)) . '?w=500';
}
add_shortcode('scshot', 'api_sc_shot');
?>
/*ショートコードは以下のようにする*/
[scshot url="http://example.com"]
<?php
function api_sc_shot ($attributes) {
extract(shortcode_atts(array(
'url' => '',
), $attributes));
$imageUrl = sc_shot ($url);
if ($imageUrl == '') {
return '';
} else {
return '<img src="' . $imageUrl . '" alt="' . $url . '" />';
}
}
function sc_shot ($url = ''){
return 'http://s.wordpress.com/mshots/v1/' . urlencode(clean_url($url)) . '?w=500';
}
add_shortcode('scshot', 'api_sc_shot');
?>
/*ショートコードは以下のようにする*/
[scshot url="http://example.com"]
過去記事参照:
wordpress.comのAPIを使ってスクリーンショットを取得する
RSSフィードにだけコンテンツを表示
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
function feedonly_shortcode( $atts, $content = null) {
if (!is_feed()) return "";
return $content;
}
add_shortcode('feedonly', 'feedonly_shortcode');
?>
/*記事内で以下のショートコードで囲ったコンテンツはフィード内でのみ閲覧可能になる*/
[feedonly]RSSフィード登録者にだけ公開します。[/feedonly]
<?php
function feedonly_shortcode( $atts, $content = null) {
if (!is_feed()) return "";
return $content;
}
add_shortcode('feedonly', 'feedonly_shortcode');
?>
/*記事内で以下のショートコードで囲ったコンテンツはフィード内でのみ閲覧可能になる*/
[feedonly]RSSフィード登録者にだけ公開します。[/feedonly]
via:
Snippet: A “Feed Only” Shortcode for WordPress
任意のカテゴリに属する記事のリンクを羅列する
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
function getCatItems($atts, $content = null) {
extract(shortcode_atts(array(
"num" => '5',
"cat" => ''
), $atts));
global $post;
$oldpost = $post;
$myposts = get_posts('numberposts='.$num.'&order=DESC&orderby=post_date&category='.$cat);
$retHtml='<ul>';
foreach($myposts as $post) :
setup_postdata($post);
$retHtml.='<li><a href="'.get_permalink().'">'.the_title("","",false).'</a></li>';
endforeach;
$retHtml.='</ul>';
$post = $oldpost;
return $retHtml;
}
add_shortcode("list", "getCatItems");
?>
/*ショートコードは以下のように書く*/
[list cat="1" num="10"]
<?php
function getCatItems($atts, $content = null) {
extract(shortcode_atts(array(
"num" => '5',
"cat" => ''
), $atts));
global $post;
$oldpost = $post;
$myposts = get_posts('numberposts='.$num.'&order=DESC&orderby=post_date&category='.$cat);
$retHtml='<ul>';
foreach($myposts as $post) :
setup_postdata($post);
$retHtml.='<li><a href="'.get_permalink().'">'.the_title("","",false).'</a></li>';
endforeach;
$retHtml.='</ul>';
$post = $oldpost;
return $retHtml;
}
add_shortcode("list", "getCatItems");
?>
/*ショートコードは以下のように書く*/
[list cat="1" num="10"]
via:
記事内に特定カテゴリの新着一覧を出すショートコード
他にも
他にも、記事内の好きな場所にアドセンスを貼ったり、Googleマップを簡単に扱う、などいろいろありますが、この辺はよく出回ってるので割愛します。
申し訳ありませんが、明日も宣伝させてください。以上、ショートコードと、新しいコンテンツのWordPress カスタマイズ スニペット のご紹介でした。