WordPressカスタマイズ
DESIRERYのWordPressカスタマイズのまとめ。テーマは自作のためテンプレなどが根本的に違ったりなどかなり独自要素が強い可能性があります。主に変更点のメモ。
パーマリンクの設定
/%year%/%monthnum%/%day%/%post_id%/
dete優先な形にしています。最後のpost_idは悩み中。言語がマルチバイトなのでどーしようかなーと。IDでも間違いじゃないと脳内変換。忘れたり間違えたりなどがない状態、統一性を考えた結果これかなと。
改ページの変更
記事ページの場合引数にID付のliタグを追加指定できるがそれ以外ではできない。ということで!is_single()の場合改ページで該当関数のreturnの中にあるaタグ前後にID付のリストタグを追加。
-
// wp-includes/link-template.php
-
function get_next_posts_link( $label = 'Next Page »', $max_page = 0 ) {
-
global $paged, $wp_query;
-
-
if ( !$max_page ) {
-
$max_page = $wp_query->max_num_pages;
-
}
-
-
if ( !$paged )
-
$paged = 1;
-
-
-
$attr = apply_filters( 'next_posts_link_attributes', '' );
-
return '<li id="next"><a href="' . next_posts( $max_page, false ) . "\" $attr>". preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label) .'</a></li>';
-
}
-
}
-
-
function get_previous_posts_link( $label = '&laquo; Previous Page' ) {
-
global $paged;
-
-
if ( !is_single() && $paged> 1 ) {
-
$attr = apply_filters( 'previous_posts_link_attributes', '' );
-
return '<li id="prev"><a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label ) .'</a></li>';
-
}
-
}
アーカイブリストの変更
アーカイブリストの「月」を削除し「YYYY-MM」にする、件数表示がaタグの外に出されるためこれをaタグ内に収めるの二つ。
まずsprintf内で__()を使っているので普通に月の情報だけ取り出すように変更し、1バイトであれば先頭に「0」を付け足す。次に件数表示では$afterに件数を入れているので$textの後ろに付け足してあげることでaタグ内に件数が収まる。
-
// wp-includes/general-template.php
-
function wp_get_archives($args = '') {
-
~~~~~
-
if ( 'monthly' == $type ) {
-
$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit";
-
$cache = wp_cache_get( 'wp_get_archives' , 'general');
-
$arcresults = $wpdb->get_results($query);
-
$cache[ $key ] = $arcresults;
-
wp_cache_add( 'wp_get_archives', $cache, 'general' );
-
} else {
-
$arcresults = $cache[ $key ];
-
}
-
if ( $arcresults ) {
-
$afterafter = $after;
-
$url = get_month_link( $arcresult->year, $arcresult->month );
-
//$text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);
-
$month = $arcresult->month;
-
$text = $arcresult->year. "-". $month;
-
if ( $show_post_count )
-
//$after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
-
$text .= '&nbsp;('.$arcresult->posts.')' . $afterafter;
-
$output .= get_archives_link($url, $text, $format, $before, $after);
-
}
-
}
-
} elseif ('yearly' == $type) {
-
~~~~~
-
}
アーカイブタイトルの変更
こちらも「月」の削除。$wp_localeからプロパティを見ているので余計な部分はコメントアウト。月が1バイトだったときは0を先頭に付け足し。
-
// wp-includes/general-template.php
-
function wp_title($sep = '&raquo;', $display = true, $seplocation = '') {
-
~~~~~
-
$title = $year;
-
//$title .= "$t_sep" . $wp_locale->get_month($monthnum);
-
$title .= "$t_sep" . $monthnum;
-
$title .= "$t_sep" . zeroise($day, 2);
-
}
-
~~~~~
-
}
補足としてアーカイブのフォーマット変更方法は別にもあります。これを書いてるときに知った。ggrks。
プラグインprettyPhotoをカスタマイズ
書き出されるヘッダ汚いという理由だけですが自分でアップロードしたJSファイルを読み込むように変更。aタグにrelを勝手に入れてくれるようにするためだけに使用。if(!is_admin)内、つまりログインしていない場合の処理をコメントアウト。それにwp_headにフックしているadd_actionもコメントアウト。
-
// plugins/wp-prettyphoto/wp-prettyphoto.php
-
public function __construct() {
-
$this->_buildOptions();
-
if (!is_admin()) {
-
/*if ($this->wppp_jsreplace == '1') {
-
// jQuery - removing to make sure we're using 1.3.2
-
wp_deregister_script('jquery');
-
wp_register_script('jquery', $wpurl.'js/jquery-1.3.2.min.js', false, '1.3.2');
-
wp_enqueue_script('jquery');
-
}
-
// prettyPhoto JavaScript
-
wp_register_script('prettyphoto', $wpurl.'js/jquery.prettyPhoto.js', array('jquery'), '2.5.2');
-
wp_enqueue_script('prettyphoto');
-
// prettyPhoto CSS
-
wp_register_style('prettyphoto', $wpurl.'css/prettyPhoto.css', false, '2.5.2', 'screen');
-
wp_enqueue_style('prettyphoto');*/
-
}
-
else {
-
$this->loadLocale(get_locale());
-
add_action('plugin_action_links_'.plugin_basename(__FILE__), array(&$this, 'wppp_plugin_links'));
-
}
-
if ($this->wppp_usecode == '1') {
-
}
-
//add_action('wp_head', array(&$this, 'wppp_styles'));
-
if ($this->_doOutput()) {
-
}
-
}
コメントのAjax化
コメントの取得部分だけ自作。コメント送信の投げる部分はWordPressのwp-comments-post.phpに渡してます。非同期にしているだけ。
まずコメントの取得。JSでphpに非同期で要求してコメントがあったらそれを返して表示。JSはjQueryで実装。
-
/*
-
このファイルは自作
-
includeでDB接続とかはwordPressのものを使用
-
QueryかけてXMLを出力するだけ
-
*/
-
$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' ORDER BY comment_date", $targetID));
-
-
$xml = new XMLWriter();
-
$xml->openMemory();
-
$xml->setIndent(1);
-
$xml->setIndentString(' ');
-
-
$xml->startDocument('1.0','utf-8');
-
$xml->startElement('data');
-
-
foreach($comments as $value) {
-
$xml->startElement('item');
-
$xml->writeElement('comment_author', $value->comment_author);
-
$xml->writeElement('comment_author_url', $value->comment_author_url);
-
$xml->writeElement('comment_date', $value->comment_date);
-
$xml->endElement();
-
}
-
-
$xml->endElement();
-
-
echo $xml->outputMemory(true);
コメント送信はwp-comments-post.phpをそのまま使用しているのでJSだけ。エラーだった場合はXHTMLが帰ってくるのでreplaceでエラー文だけ取得し、表示。エラーでなければ最新のコメントを再取得し表示。正規表現は知らないんでreplaceではてきとーに。エラーのときの処理は以下のような感じ。
-
error: function(d)
-
{
-
error = d.responseText.replace(/(.|\n)+<p>(.+)<\/p>(.|\n)+/, '$2');
-
if($("#error_" + id).get(0) == undefined) {
-
$(form).children("dl:last").before('<p id="error_' + id + '">' + error + '</p>');
-
} else {
-
$("#error_" + id).text(error);
-
}
-
}