上間ウェブ店

【スニペット】wordpress

2019/06/12

よく使うワードプレスのスニペットです。

メインループ + 基本のタグ

if (have_posts()):
  while (have_posts()):
    the_post();
    if (get_the_post_thumbnail()):
      the_post_thumbnail();
    endif;
    the_title();
    the_permalink();
    the_time("Y/m/d");
    the_content();
  endwhile;
endif;

サブループ

サブループはファイル分割して再利用性を高めるためにset_query_varを使う

呼び出す側

$args = array(
  'post_type' => 'post',
  'post_status' => 'publish',
  'orderby' => 'date',
  'order' => 'DESC',
  'tax_query' => array(
    array(
      'taxonomy' => 'category',
      'field' => 'slug',
      'terms' => 'tech',
    )
  )
);
$the_query = new WP_Query($args);
set_query_var('tech', $the_query);
if($the_query->have_posts()):
  get_element('techList'); // 分割したファイルを呼び出す独自関数(中身はget_template_partと同じ)
endif;

呼び出される側

$the_query = get_query_var('tech');
while ($the_query->have_posts()): $the_query->the_post();
  the_title();
  the_permalink();
  the_time("Y/m/d");
  etc...
endwhile;

カテゴリーとかタグのリストを独自htmlで出したい時

$categories = get_the_category();
if ($categories):
  echo '<ul class="categories">';
  foreach ($categories as $category):
    echo '<li class="category">';
    echo '<a href="/'.$category->slug.'">' . $category->name . '</a>';
    echo '</li>';
  endforeach;
  echo '</ul>';
endif;

$tags = get_the_tags();
if ($tags):
  echo '<ul class="tags">';
  foreach ($tags as $tag):
    echo '<li class="tag">';
    echo '<a href="/'.$tag->slug.'">' . $tag->name . '</a>';
    echo '</li>';
  endforeach;
  echo '</ul>';
endif;

ヘッドタグの掃除

remove_action( 'wp_head', 'wp_generator' ); // バージョン情報削除
remove_action( 'wp_head', 'rsd_link' ); // 外部アプリケーションから情報を取得するタグ削除
remove_action( 'wp_head', 'wlwmanifest_link' ); // Windows Live Writer用のタグ削除
remove_action( 'wp_head', 'index_rel_link' ); // linkタグ削除
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 ); // 「link rel="up"のタグ削除
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 ); // 「link rel="start"」のタグ削除
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 ); //「rel="next"、rel="prev"」のタグ削除
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0 ); // デフォルトパーマリンクのURL削除
remove_action('wp_head', 'feed_links', 2); // フィード関連のタグ削除
remove_action('wp_head', 'feed_links_extra', 3); // フィード関連のタグ削除

//head内、絵文字削除
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('wp_print_styles', 'print_emoji_styles' );
remove_action('admin_print_styles', 'print_emoji_styles');

//head内、Embed系の記述削除
remove_action('wp_head','rest_output_link_wp_head');
remove_action('wp_head','wp_oembed_add_discovery_links');
remove_action('wp_head','wp_oembed_add_host_js');
remove_action('template_redirect', 'rest_output_link_header', 11 );

// 管理バーの削除
add_filter( 'show_admin_bar', '__return_false' );

// すべてのバージョン情報を非表示
function remove_cssjs_ver2( $src ) {
  if ( strpos( $src, 'ver=' ) )
    $src = remove_query_arg( 'ver', $src );
  return $src;
}
add_filter( 'style_loader_src', 'remove_cssjs_ver2', 9999 );
add_filter( 'script_loader_src', 'remove_cssjs_ver2', 9999 );

ログインをメールアドレスのみにする(セキュリティ対策)

function email_login( $user, $username, $password ) {
  $user = get_user_by('email',$username);
  if (!empty($user->user_login)) {
    $username = $user->user_login;
  }
  else {
    $username = '';
  }
  return wp_authenticate_username_password( null, $username, $password );
}
add_filter('authenticate', 'email_login', 20, 3);

pre_get_posts

function change_posts_per_page($query) {
  if ( is_admin() || !$query->is_main_query() ){
    return;
  }
  if ( $query->is_home() || $query->is_archive() ) {
    $query->set( 'posts_per_page', 20 );
    return;
  }
}
add_action( 'pre_get_posts', 'change_posts_per_page' );

get_template_directory_uriが長ったらしくて面倒なのでtheme_dirで発火するように

function theme_dir(){
  $theme_dir = get_template_directory_uri();
  echo $theme_dir;
}

テンプレートを選択できるように

/*
Template Name: template name
*/

クラシックエディタ(ビジュアルエディタ)からh1を除去する

除去するというか、省いて再登録するイメージ。

function custom_editor_settings( $initArray ){
  $initArray['block_formats'] = "見出し2=h2; 見出し3=h3; 見出し4=h4; 見出し5=h5; 段落=p; グループ=div;";
  return $initArray;
}
add_filter( 'tiny_mce_before_init', 'custom_editor_settings' );