プログラミング

プログラミング

PR

ワードプレスにプラス・アルファの機能性を!コードスニペット20選!【後半】

2,359 views

読了時間 : 約3分6秒

ワードプレスのテーマをコードするときに、とりわけレギュラーにやるなら、必要なときにコピペできるスニペットをツールボックスに入れておくと大変便利です。

本記事では、前半と後半に分けて、ワードプレスの機能性を向上させてくれるようなスニペットを20集めました。

前半はこちら↓

https://seleqt.net/programming/wordpress-snippe…tend-wordpre-1/

 

 

11.WordPressの引用文をカスタマイズする

 

引用文で引用できる文字数が限られている場合がありますよね。このスニペットを使えば、引用したい原文を自分で編集した引用文(my_excerpts)に置き換えることができます。

下のコードを[functions.php]内にペーストします。

<?php add_filter('the_excerpt', 'my_excerpts');
function my_excerpts($content = false) {
global $post;
$mycontent = $post->post_excerpt;
$mycontent = $post->post_content;
$mycontent = strip_shortcodes($mycontent);
$mycontent = str_replace(']]>', ']]&gt;', $mycontent);
$mycontent = strip_tags($mycontent);
$excerpt_length = 55;
$words = explode(' ', $mycontent, $excerpt_length + 1);
if(count($words) > $excerpt_length) :
array_pop($words);
array_push($words, '...');
$mycontent = implode(' ', $words);
endif;
$mycontent = '<p>' . $mycontent . '</p>';
// Make sure to return the content
return $mycontent;
}
?>
view raw gistfile1.txt hosted with ❤ by GitHub

次に、画像のコードをLoop内にペーストします。

<?php echo my_excerpts(); ?>
view raw gistfile1.txt hosted with ❤ by GitHub

 

 

12.投稿タイトルを外部リンクにリダイレクトする

 

投稿のタイトルをクリックしたときに、カスタムフィールドで外部リンクにリダイレクトさせることができる便利なスニペットです。

下のスニペットを[functions.php]ファイルにペーストします。

//Custom Redirection
function print_post_title() {
global $post;
$thePostID = $post->ID;
$post_id = get_post($thePostID);
$title = $post_id->post_title;
$perm = get_permalink($post_id);
$post_keys = array(); $post_val = array();
$post_keys = get_post_custom_keys($thePostID);
if (!empty($post_keys)) {
foreach ($post_keys as $pkey) {
if ($pkey=='url') {
$post_val = get_post_custom_values($pkey);
}
}
if (empty($post_val)) {
$link = $perm;
} else {
$link = $post_val[0];
}
} else {
$link = $perm;
}
echo '<h2><a href="'.$link.'" rel="bookmark" title="'.$title.'">'.$title.'</a></h2>';
view raw gistfile1.txt hosted with ❤ by GitHub

‘url’と名前のつけたカスタムフィールドを追加し、リダイレクト先のリンクをバリューフィールドにペーストします。

 

 

13.選択されたカテゴリ/タグ内に投稿が1件しかない場合に、投稿に直接リダイレクトする

 

選択されたカテゴリまたはタグ内に投稿が1件しかない場合に、その投稿に直接飛ぶことができるスニペットです。

テーマの[functions.php]ファイルに下のコードをペーストするだけです。

function stf_redirect_to_post(){
global $wp_query;
// If there is one post on archive page
if( is_archive() && $wp_query->post_count == 1 ){
// Setup post data
the_post();
// Get permalink
$post_url = get_permalink();
// Redirect to post page
wp_redirect( $post_url );
}
} add_action('template_redirect', 'stf_redirect_to_post');
view raw gistfile1.txt hosted with ❤ by GitHub

 

 

14.今後の投稿をリスト化して表示する

 

今後投稿する予定になっている記事をリストにして表示してくれるスニペットです。

リストを表示してほしい箇所に下のコードをペーストします。クエリ内のショーポストのバリュー値を変えて、リスト内に表示する投稿やその数を変更することも可能です。

<?php
$my_query = new WP_Query('post_status=future&order=DESC&showposts=5');
if ($my_query->have_posts()) {
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
<li><?php the_title(); ?></li>
<?php endwhile;
}
?>
view raw gistfile1.txt hosted with ❤ by GitHub

 

 

15.プラグインを使わずに外部のWordPressページのスクリーンショットを表示する

 

シンプルなURLひとつで、どんなウェブサイトのスクリーンショットも表示できるようになります。

http://s.wordpress.com/mshots/v1/http%3A%2F%2Fspeckyboy.com%2F?w=500
view raw gistfile1.txt hosted with ❤ by GitHub

上の画像の“speckyboy.com”部分を任意のリンクに置き換えて、(‘w=500’)をリサイズするだけで簡単にできます。

実際にどんな風になるかを知るには、こちらをクリックしてみてください。<<リンク>>

 

 

16.一つひとつのRSS投稿の最後にコンテンツを追加する

 

下のコードをfunctions.phpにペーストすれば、のみに表示される限定コンテンツを簡単に追加できます。

function feedFilter($query) {
if ($query->is_feed) {
add_filter('the_content','feedContentFilter');
}
return $query;
}
add_filter('pre_get_posts','feedFilter');
function feedContentFilter($content) {
$content .= '<p>Extra RSS content goes here... </p>';
return $content;
}
view raw gistfile1.txt hosted with ❤ by GitHub

 

 

17.WordPressのパスワードをリセットする

 

WordPressのパスワードを忘れてしまっても、PhpMyAdmin Sqlウィンドウで下の画像のコマンドを実行すれば解決です。

UPDATE `wp_users` SET `user_pass` = MD5('NEW_PASSWORD') WHERE `wp_users`.`user_login` =`YOUR_USER_NAME` LIMIT 1;
view raw gistfile1.txt hosted with ❤ by GitHub

 

 

18.閲覧者の使用ブラウザを特定する

 

それぞれのブラウザに合わせたスタイルシートを使いたい場合は、スニペットが便利です。閲覧者がどのブラウザを使っているのかを特定して、それに合わせたクラスを作成してくれます。作成されたクラスを元にカスタムスタイルシートが作れます。

add_filter('body_class','browser_body_class');
function browser_body_class($classes) {
global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone;
if($is_lynx) $classes[] = 'lynx';
elseif($is_gecko) $classes[] = 'gecko';
elseif($is_opera) $classes[] = 'opera';
elseif($is_NS4) $classes[] = 'ns4';
elseif($is_safari) $classes[] = 'safari';
elseif($is_chrome) $classes[] = 'chrome';
elseif($is_IE) $classes[] = 'ie';
else $classes[] = 'unknown';
if($is_iphone) $classes[] = 'iphone';
return $classes;
}
view raw gistfile1.txt hosted with ❤ by GitHub

 

 

19.グーグルユーザが使用した検索ワードを表示する

 

閲覧者があなたのサイトにたどり着くのに、どんな検索ワードを使ったのかを表示してくれます。ヘッダーセクション以外の場所に、画像のコードをペーストするだけで使えます。

<?php
$refer = $_SERVER["HTTP_REFERER"];
if (strpos($refer, "google")) {
$refer_string = parse_url($refer, PHP_URL_QUERY);
parse_str($refer_string, $vars);
$search_terms = $vars['q'];
echo 'Welcome Google visitor! You searched for the following terms to get here: ';
echo $search_terms;
};
?>
view raw gistfile1.txt hosted with ❤ by GitHub

 

 

20.MacとWindowsで違うコンテンツを表示する

 

ソフトウェアDLなど、MacユーザとWindowsユーザで違うコンテンツを表示したい場合もありますよね。下のコードを[functions.php]ファイルにペーストすれば、必要に応じてコンテンツを変えて対応してくれます。

<?php
if (stristr($_SERVER['HTTP_USER_AGENT'],"mac")) {
echo ‘Hello, I\’m a Mac.’;
} else {
echo ‘And I\’m a PC.’;
}
?>
view raw gistfile1.txt hosted with ❤ by GitHub

 

 

※本記事は、20 Code Snippets for Extending the Functionality of WordPressを翻訳・再構成したものです。

 

 

関連記事

 

 




おすすめ新着記事

おすすめタグ