DESIRERY

WordPressカスタマイズ

DESIRERYのWordPressカスタマイズのまとめ。テーマは自作のためテンプレなどが根本的に違ったりなどかなり独自要素が強い可能性があります。主に変更点のメモ。

パーマリンクの設定
/%year%/%monthnum%/%day%/%post_id%/
dete優先な形にしています。最後のpost_idは悩み中。言語がマルチバイトなのでどーしようかなーと。IDでも間違いじゃないと脳内変換。忘れたり間違えたりなどがない状態、統一性を考えた結果これかなと。

改ページの変更
記事ページの場合引数にID付のliタグを追加指定できるがそれ以外ではできない。ということで!is_single()の場合改ページで該当関数のreturnの中にあるaタグ前後にID付のリストタグを追加。

  1. // wp-includes/link-template.php
  2. function get_next_posts_link( $label = 'Next Page »', $max_page = 0 ) {
  3. global $paged, $wp_query;
  4.  
  5. if ( !$max_page ) {
  6. $max_page = $wp_query->max_num_pages;
  7. }
  8.  
  9. if ( !$paged )
  10. $paged = 1;
  11.  
  12. $nextpage = intval($paged) + 1;
  13.  
  14. if ( !is_single() && ( empty($paged) || $nextpage <= $max_page) ) {
  15. $attr = apply_filters( 'next_posts_link_attributes', '' );
  16. return '<li id="next"><a href="' . next_posts( $max_page, false ) . "\" $attr>". preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $label) .'</a></li>';
  17. }
  18. }
  19.  
  20. function get_previous_posts_link( $label = '« Previous Page' ) {
  21. global $paged;
  22.  
  23. if ( !is_single() && $paged> 1 ) {
  24. $attr = apply_filters( 'previous_posts_link_attributes', '' );
  25. return '<li id="prev"><a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/', '&$1', $label ) .'</a></li>';
  26. }
  27. }

アーカイブリストの変更
アーカイブリストの「月」を削除し「YYYY-MM」にする、件数表示がaタグの外に出されるためこれをaタグ内に収めるの二つ。

まずsprintf内で__()を使っているので普通に月の情報だけ取り出すように変更し、1バイトであれば先頭に「0」を付け足す。次に件数表示では$afterに件数を入れているので$textの後ろに付け足してあげることでaタグ内に件数が収まる。

  1. // wp-includes/general-template.php
  2. function wp_get_archives($args = '') {
  3. ~~~~~
  4. if ( 'monthly' == $type ) {
  5. $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";
  6. $key = md5($query);
  7. $cache = wp_cache_get( 'wp_get_archives' , 'general');
  8. if ( !isset( $cache[ $key ] ) ) {
  9. $arcresults = $wpdb->get_results($query);
  10. $cache[ $key ] = $arcresults;
  11. wp_cache_add( 'wp_get_archives', $cache, 'general' );
  12. } else {
  13. $arcresults = $cache[ $key ];
  14. }
  15. if ( $arcresults ) {
  16. $afterafter = $after;
  17. foreach ( (array) $arcresults as $arcresult ) {
  18. $url = get_month_link( $arcresult->year, $arcresult->month );
  19. //$text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);
  20. $month = $arcresult->month;
  21. if(strlen($month) == 1) $month = "0". $month;
  22. $text = $arcresult->year. "-". $month;
  23. if ( $show_post_count )
  24. //$after = ' ('.$arcresult->posts.')' . $afterafter;
  25. $text .= ' ('.$arcresult->posts.')' . $afterafter;
  26. $output .= get_archives_link($url, $text, $format, $before, $after);
  27. }
  28. }
  29. } elseif ('yearly' == $type) {
  30. ~~~~~
  31. }

アーカイブタイトルの変更
こちらも「月」の削除。$wp_localeからプロパティを見ているので余計な部分はコメントアウト。月が1バイトだったときは0を先頭に付け足し。

  1. // wp-includes/general-template.php
  2. function wp_title($sep = '»', $display = true, $seplocation = '') {
  3. ~~~~~
  4. if ( !empty($year) ) {
  5. $title = $year;
  6. if ( !empty($monthnum) )
  7. //$title .= "$t_sep" . $wp_locale->get_month($monthnum);
  8. if(strlen($monthnum) == 1) $monthnum = "0". $monthnum;
  9. $title .= "$t_sep" . $monthnum;
  10. if ( !empty($day) )
  11. $title .= "$t_sep" . zeroise($day, 2);
  12. }
  13. ~~~~~
  14. }

補足としてアーカイブのフォーマット変更方法は別にもあります。これを書いてるときに知った。ggrks。

プラグインprettyPhotoをカスタマイズ
書き出されるヘッダ汚いという理由だけですが自分でアップロードしたJSファイルを読み込むように変更。aタグにrelを勝手に入れてくれるようにするためだけに使用。if(!is_admin)内、つまりログインしていない場合の処理をコメントアウト。それにwp_headにフックしているadd_actionもコメントアウト。

  1. // plugins/wp-prettyphoto/wp-prettyphoto.php
  2. public function __construct() {
  3. $this->_buildOptions();
  4. $wpurl = WP_PLUGIN_URL.'/'.str_replace(basename( __FILE__), '', plugin_basename(__FILE__));
  5. if (!is_admin()) {
  6. /*if ($this->wppp_jsreplace == '1') {
  7. // jQuery - removing to make sure we're using 1.3.2
  8. wp_deregister_script('jquery');
  9. wp_register_script('jquery', $wpurl.'js/jquery-1.3.2.min.js', false, '1.3.2');
  10. wp_enqueue_script('jquery');
  11. }
  12. // prettyPhoto JavaScript
  13. wp_register_script('prettyphoto', $wpurl.'js/jquery.prettyPhoto.js', array('jquery'), '2.5.2');
  14. wp_enqueue_script('prettyphoto');
  15. // prettyPhoto CSS
  16. wp_register_style('prettyphoto', $wpurl.'css/prettyPhoto.css', false, '2.5.2', 'screen');
  17. wp_enqueue_style('prettyphoto');*/
  18. }
  19. else {
  20. $this->loadLocale(get_locale());
  21. add_action('admin_init', array(&$this, 'wppp_section_register'));
  22. add_action('plugin_action_links_'.plugin_basename(__FILE__), array(&$this, 'wppp_plugin_links'));
  23. }
  24. if ($this->wppp_usecode  == '1') {
  25. add_shortcode('ppo', array(&$this, 'wppp_shortcode'));
  26. add_shortcode('ppg', array(&$this, 'wppp_shortcode'));
  27. }
  28. //add_action('wp_head', array(&$this, 'wppp_styles'));
  29. if ($this->_doOutput()) {
  30. add_filter('the_content', array(&$this, 'wppp_content_hook'), 99, 1);
  31. add_filter('the_excerpt', array(&$this, 'wppp_content_hook'), 99, 1);
  32. }
  33. }

コメントのAjax化
コメントの取得部分だけ自作。コメント送信の投げる部分はWordPressのwp-comments-post.phpに渡してます。非同期にしているだけ。

まずコメントの取得。JSでphpに非同期で要求してコメントがあったらそれを返して表示。JSはjQueryで実装。

  1. /*
  2. このファイルは自作
  3. includeでDB接続とかはwordPressのものを使用
  4. QueryかけてXMLを出力するだけ
  5. */
  6. $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' ORDER BY comment_date", $targetID));
  7.  
  8. $xml = new XMLWriter();
  9. $xml->openMemory();
  10. $xml->setIndent(1);
  11. $xml->setIndentString('    ');
  12.  
  13. $xml->startDocument('1.0','utf-8');
  14. $xml->startElement('data');
  15.  
  16. foreach($comments as $value) {
  17. $xml->startElement('item');
  18. $xml->writeElement('comment_author', $value->comment_author);
  19. $xml->writeElement('comment_author_url', $value->comment_author_url);
  20. $xml->writeElement('comment_date', $value->comment_date);
  21. $xml->writeElement('comment_content', nl2br($value->comment_content));
  22. $xml->endElement();
  23. }
  24.  
  25. $xml->endElement();
  26.  
  27. header('Content-type: text/xml');
  28. echo $xml->outputMemory(true);

コメント送信はwp-comments-post.phpをそのまま使用しているのでJSだけ。エラーだった場合はXHTMLが帰ってくるのでreplaceでエラー文だけ取得し、表示。エラーでなければ最新のコメントを再取得し表示。正規表現は知らないんでreplaceではてきとーに。エラーのときの処理は以下のような感じ。

  1. error: function(d)
  2. {
  3. error = d.responseText.replace(/(.|\n)+<p>(.+)<\/p>(.|\n)+/, '$2');
  4. if($("#error_" + id).get(0) == undefined) {
  5. $(form).children("dl:last").before('<p id="error_' + id + '">' + error + '</p>');
  6. } else {
  7. $("#error_" + id).text(error);
  8. }
  9. }

Related Posts

Popular Posts

  1. WP Queryの中身
  2. SexyBookmarks カスタマイズ
  3. prettyPhoto
  4. Ajax Comments
  5. jQuery 何番目かを取得