情報が古い可能性があります。
サンプルテーマのダウンロード
WordPress3.8.1で確認したサンプルテーマです。よろしければどうぞ。
過去の「flexibleSearch.js」に関する記事
- Ajax検索のjQueryプラグイン「flexibleSearch.js」のバージョン2.0.0がリリースされました!
- 高速Ajax検索のjQueryプラグイン「flexibleSearch.js」を試してみた ~Movable Type編~
- 高速Ajax検索のjQueryプラグイン「flexibleSearch.js」を試してみた ~WordPress編~
INDEX
- 1. テーマを作成する
- 2. flexibleSearchをダウンロードする
- 3. パーマリンクを変更する
- 4. 検索用のJSONファイルを生成する
- 5. flexibleSearchをWordPressで読み込む
- 6. flexibleSearchを使用するための設定を行う
- 7. 検索フォーム用と検索結果表示用のHTML要素を配置する
- 8. テーマの表示
1. テーマを作成する

WordPressのテーマを作成します。
WordPressをインストールしているディレクトリの wp-content/themes の配下に flexiblesearch というテーマフォルダとファイル郡を作成しました。
2. flexibleSearchをダウンロードする

flexibleSearchのバージョン2.0.0をダウンロードしてテーマフォルダの js フォルダ配下に flexibleSearchを配置します。
3. パーマリンクを変更する
![]()
デフォルトのパラメータベースのパーマリンクですとflexibleSearchが誤作動で検索するので、パーマリンクを数字ベースに変更しました。
デフォルトでなければ数字ベースのパーマリンク以外を選択しても大丈夫です。
4. 検索用のJSONファイルを生成する
検索用のJSONファイルを生成する為の処理をテーマの functions.php に記述します。
今回はお試しでテーマを表示した時に flexiblesearch.json という検索用のJSONファイルをWordPressをインストールしているディレクトリの wp-content の配下に書き出すようにしました。

<?php
/**
* 改行コードとタグの除去します
*/
if ( ! function_exists( 'del_tags_escapestring' ) ) :
function del_tags_escapestring( $value ) {
$value = str_replace( array( "\n","\t" ), "", $value );
$value = strip_tags( $value );
return $value;
}
endif;
/**
* flexibleSearchの検索用のJSONファイルを生成します
* お気軽に試せるようにinitフックを使用していますが、本来は別のタイミングが良いでしょう
*/
add_action( 'init', 'hook_init' );
if ( ! function_exists( 'hook_init' ) ) :
function hook_init() {
$items = '';
// 管理画面および、flexibleSearchの検索結果ページではJSONファイルを生成しません
if( is_admin() || isset( $_GET[ 'search' ] ) ) return;
// 投稿データを全件取得します
$posts = get_posts( array( 'posts_per_page' => -1 ) );
foreach ( $posts as $post ) {
$width = '';
$height = '';
// 本文を取得します
$content = apply_filters( 'the_content', $post->post_content );
$content = del_tags_escapestring( $content );
// 抜粋を取得します
$excerpt = $post->post_excerpt;
$excerpt = del_tags_escapestring( $excerpt );
// get_the_post_thumbnailを使用するとimgタグを生成するので、タグが検索キーワードとして引っかかってしまい注意が必要です
//$image = get_the_post_thumbnail( $post->ID );
// アイキャッチ画像のURLを取得します
$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'post-thumbnail' );
$image = '';
if ( $image_attributes ) {
$image = $image_attributes[0];
// 必要に応じてwitdhやheight属性を使用してください
//$width = $image_attributes[1];
//$height = $image_attributes[2];
}
// カテゴリを取得します
$post_categorys = get_the_category( $post->ID );
$categorys = '';
if ( $post_categorys ) {
foreach( $post_categorys as $category ) {
// カンマ区切りでカテゴリの名称を入れます
$categorys = $categorys . $category->name;
if( $category !== end( $post_categorys ) ) {
$categorys = $categorys . ',';
}
}
}
// タグを取得します
$post_tags = get_the_tags( $post->ID );
$tags = '';
if ( $post_tags ) {
foreach( $post_tags as $tag ) {
// カンマ区切りでタグの名称を入れます
$tags = $tags . $tag->name;
if( $tag !== end( $post_tags ) ) {
$tags = $tags . ',';
}
}
}
// JSON形式で出力するデータを配列にセットします
$items[] = array(
'title' => $post->post_title,
'excerpt' => $excerpt,
'content' => $content,
'permalink' => get_permalink( $post->ID ),
'tag' => $tags,
'image' => $image,
'category' => $categorys,
// 必要に応じてwitdhやheight属性を使用してください
//'width' => $width,
//'height' => $height,
);
}
$result[ 'items' ] = $items;
// wp-content配下に検索用のJSONファイル "flexiblesearch.json" を書き出します
file_put_contents( WP_CONTENT_DIR . '/flexiblesearch.json', json_encode( $result ) );
}
endif;
5. flexibleSearchをWordPressで読み込む
flexibleSearch関連のJSファイルを読み込む処理を functions.php に記述します。
<?php
/**
* flexibleSearch関連のJSファイルを読み込みます
*/
add_action( 'wp_enqueue_scripts', 'hook_wp_enqueue_scripts' );
if ( ! function_exists( 'hook_wp_enqueue_scripts' ) ) :
function hook_wp_enqueue_scripts() {
// "mustache.js" と "flexibleSearch.min.js" の読み込みます
wp_enqueue_script( 'mustache', get_template_directory_uri() . '/js/flexibleSearch/mustache.js', array( 'jquery' ) );
wp_enqueue_script( 'flexible-search', get_template_directory_uri() . '/js/flexibleSearch/flexibleSearch.min.js', array( 'jquery' ) );
// "flexibleSearch-config.js" は "(function ($) {" の部分を "jQuery(function ($) {" とWordPress用に書き換える必要があるため今回は使用しません
//wp_enqueue_script( 'flexible-search-config', get_template_directory_uri() . '/js/flexibleSearch/flexibleSearch-config.js', array( 'jquery' ) );
}
endif;
上記の functions.php のコードが実際に展開されると以下のようになります。
<script type='text/javascript' src='http://localhost:8888/wordpress/wp-includes/js/jquery/jquery.js?ver=1.10.2'></script> <script type='text/javascript' src='http://localhost:8888/wordpress/wp-content/themes/flexiblesearch/js/flexibleSearch/mustache.js?ver=3.8.1'></script> <script type='text/javascript' src='http://localhost:8888/wordpress/wp-content/themes/flexiblesearch/js/flexibleSearch/flexibleSearch.min.js?ver=3.8.1'></script>
6. flexibleSearchを使用するための設定を行う
テーマの header.php に flexibleSearch を使用するための処理を記述します。
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>" />
<title><?php wp_title( '|', true, 'right' ); ?><?php bloginfo( 'name' ); ?></title>
<link rel="stylesheet" href="<?php bloginfo ( 'stylesheet_url' ); ?>" >
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Rancho" />
<?php /** wp_head();を定義していない場合、functions.phpで定義しているスクリプトの読み込みが行われません */ ?>
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<script>
// WordPressでは "(function($){" ではなく、"jQuery(function($){" のように書くようにしましょう
jQuery(function($){
// flexibleSearchの設定を行います
$( '#search' ).flexibleSearch({
// 検索中のローディング画像のパスを指定します
loadingImgPath : "<?php echo get_template_directory_uri(); ?>/js/flexibleSearch/loading.gif",
// 検索用のJSONファイルまでのパスを指定します
searchDataPath : "<?php echo esc_url( home_url() ); ?>/wp-content/flexiblesearch.json",
// 検索結果を表示するHTML要素のidを指定します
//resultBlockId : "content",
// 検索結果のメッセージのテンプレートを指定します
resultMsgTmpl: ['<div id="fs-result-msg">',
'<p>{{#keywords}}「{{keywords}}」が {{/keywords}}{{count}} 件見つかりました。',
'({{firstPage}}〜{{lastPage}} ページ中 {{currentPage}} ページ目を表示)</p>',
'</div>'].join(""),
// 検索結果のテンプレートを指定します
resultItemTmpl: ['<div id="fs-result-item">',
'<ul>',
'{{#items}}',
'<li><a href="{{permalink}}">',
'{{#image}}',
'<img src="{{image}}" alt=""{{#width}} width="{{width}}"{{/width}}{{#height}} height="{{height}}"{{/height}} />',
'{{/image}}',
'<br />{{&title}}',
'</a></li>',
'{{/items}}',
'</ul>',
'</div>'
].join(""),
// 1ページあたりの最大表示件数を指定します
paginateCount: 5
});
});
</script>
7. 検索フォーム用と検索結果表示用のHTML要素を配置する
テーマの index.php に検索フォーム用と検索結果表示用のHTML要素を記述します。
<?php /** flexibleSearchの検索フォームを使用するための要素です */ ?>
<div id="search"></div>
<?php /** flexibleSearchの検索結果を表示するための要素です */ ?>
<div id="fs-result"></div>
8. テーマの表示
トップページ

テーマのトップページを見てみましょう。この時に検索用のJSONファイル flexiblesearch.json が生成されます。
検索結果ページ

検索キーワードを入力して検索してみます。
ページネーション

ページネーションも表示されます。
flexibleSearchのバージョンアップで検索ワードを保持するようになったので、リクエストは発生してしまいますが、StaticPressなどの静的サイト生成のプラグインと組み合わせると高速検索を組み込んだサイトが構築可能かもしれませんね!