カテゴリー
WordPress

httpからhttpsへリダイレクト

Chrome68からhttpでアクセスすると、アドレスバーに「保護されていません」とアナウンスが出るようになった。まだ灰色の文字なので目立たない(?)が、Chrome70からは赤文字になるらしい。Googleちょっとやりすぎじゃないか…?

WordPressでhttpでのアクセスをhttpsへリダイレクトの一例です。
.htaccessの最初に下記を記述する。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

# BEGIN WordPress
...

SSL認証のシールが貼ってあってもhttpで通信してるサイトとか、結構あるんだと思った。お金がもったいない。
HSTSが使えるなら、そっちのほうがいいとのことです。

4月に同じ内容でブログを書いていた…。

カテゴリー
WordPress

.msgファイルをメディアにアップロードさせたいという謎案件

という案件に遭遇。
.msgはOUTLOOKのメッセージファイルのようです。

MIMEタイプがよくわからん…!
で調べたら、「application/vnd.ms-outlook」とのこと。
下記をFunctions.phpに書いてみる。

function allow_upload_msg( $mimes ) {
    $mimes['msg'] = 'application/vnd.ms-outlook';
    return $mimes;
}
add_filter( 'upload_mimes', 'allow_upload_msg' );

これでOK(未確認)。

参考サイト
http://www.whatisfileextension.com/jp/msg/

カテゴリー
WordPress

WordPressのログイン画面にFavicon出したり

「WordPressのログイン画面にFavicon出せ」という依頼があった。
ログイン画面のheaderにhtmlを表示させるには、アクションフックを使うとよいらしい。

忘れないうちに以下略。

Functions.php

/* ログイン画面にfaviconとapple-touch-iconを表示 */
function admin_favicon() {
echo '<link rel="shortcut icon" href="' . get_stylesheet_directory_uri() . '/favicon.png" />';
echo '<link rel="apple-touch-icon" size="152x152" href="' . get_stylesheet_directory_uri() . '/apple-touch-icon.png" />'; 
}
add_action('login_head', 'admin_favicon');

ちなみに、
add_action(‘admin_head’, ‘admin_favicon’);にすると、
管理画面のheaderに記述できる。

codexにアクションフック一覧がある。
色々ありすぎて読むのが大変。

プラグイン API/アクションフック一覧
https://wpdocs.osdn.jp/%E3%83%97%E3%83%A9%E3%82%B0%E3%82%A4%E3%83%B3_API/%E3%82%A2%E3%82%AF%E3%82%B7%E3%83%A7%E3%83%B3%E3%83%95%E3%83%83%E3%82%AF%E4%B8%80%E8%A6%A7

カテゴリー
WordPress

よくわかんないけどget_postsで表示

Advanced Custom Fields のアドオン Repeater Fieldのループ内で表示がうまくできない、と相談があり。

よくわかんないんだけど、get_postsで表示させた。リピーターフィールドの値をタームに入れて、最新の日付を表示。

$my_postsの中身みて、必要なものを取り出す。取り出した日付の値を、strtotimeで整形しています。

これで良かったのだろうか…。

<?php
$category = get_sub_field('xxxx');
$args = array(
  'post_type' => 'xxxx',
  'posts_per_page' => 1,
  'tax_query' => array(
    array(
      'taxonomy' => 'xxxxxx',
      'field'    => 'slug',
      'terms'    => $category,
    ),
  ),
);
$my_posts = get_posts( $args );
if( $my_posts ) {
	$date = $my_posts[0]->post_date;
	?>
	 <p class="pickup">更新日:<?php echo date('Y.m.d', strtotime($date));?><time datetime="<?php echo date('Y.m.d', strtotime($date));?>"></time></p>
<?php
}
?>

テンプレートタグ/get posts
https://wpdocs.osdn.jp/%E3%83%86%E3%83%B3%E3%83%97%E3%83%AC%E3%83%BC%E3%83%88%E3%82%BF%E3%82%B0/get_posts

カテゴリー
WordPress

fetch feed関数でRSSを取得する

WordPressのfetch feed関数でRSSを取得しました。忘れないうちにメモ。

表示させたいとこへ以下。
※下記のコードは、ACFのリピーターフィールドを指定してるけど、$rss_feedにURLを指定すればよい。

	  	<?php while(the_repeater_field('rss_feed')):
	$rss_feed[] = get_sub_field('rss_url');
	endwhile;
	rss_prog($rss_feed);?>

Functions.phpに以下。htmlの部分は適当に修正する。preg_matchで画像を見つけてサムネイルにしてる。

<?php
//RSS表示
function rss_prog($rss_feed){
  include_once( ABSPATH . WPINC . '/feed.php' );
  $rss = fetch_feed($rss_feed);
  $maxitems = 0;
if ( ! is_wp_error( $rss ) ) : // ちゃんとフィードが生成されているかをチェックします。
    $maxitems = $rss->get_item_quantity( 5 ); 
    $rss_items = $rss->get_items( 0, $maxitems );

endif;
  ?>
<ul class="news-list">
    <?php if ( $maxitems == 0 ) : ?>
        <li>記事がありません</li>
    <?php else : ?>
        <?php // Loop through each feed item and display each item as a hyperlink. ?>
        <?php foreach ( $rss_items as $key => $item) :
        $first_img = '';
  if ( preg_match( '/<img.+?src=[\'"]([^\'"]+?)[\'"].*?>/msi', $item->get_content(), $matches ) ) {
        $first_img = $matches[1];
}?>
            <li><div class="news-img c-col5">
			     <?php if (!$first_img) : ?>
			  <img src="<?php echo get_template_directory_uri(); ?>/image/noimage.png">
    <?php else : ?>
<img src="<?php if($first_img) : echo esc_html( $first_img ); else: "";endif; ?>" width="100" height="60">
	 <?php endif; ?></div><div class="news-text c-col6">
                <p class="n-tit"><a href="<?php echo esc_url( $item->get_permalink() ); ?>"
                    title="<?php printf( __( 'Posted %s', 'my-text-domain' ), $item->get_date('j F Y | g:i a') ); ?>">
				  <?php echo esc_html( $item->get_title() ); ?></a></p>
			  <p class="n-info"><?php echo $item->get_feed()->get_title(); // サイト名 ?><?php echo $item->get_date('j F Y | g:i a'); // サイト名 ?></p>
                
			  </div></li>
        <?php endforeach; ?>
    <?php endif; ?>
</ul>
<?php
}

関数リファレンス/fetch feed
https://wpdocs.osdn.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/fetch_feed

カテゴリー
WordPress

ユーザーアバターの表示方法

表示させるだけなら。

$current_user = wp_get_current_user();
echo get_avatar($current_user-&gt;ID, 56);

色々こねこね。

$current_user = wp_get_current_user();
              $args = array(
                  'height'    => '56',
                  'width'     => '56',
                  'class'     => array('img-class', 'profile-class')
              );
              $alt = $current_user->user_firstname . '\'s Avatar';
              printf( '<a href="%s">%s</a>', get_edit_user_link($current_user->ID), get_avatar($current_user->ID, 56, null, $alt, $args) );

関数リファレンス/get avatar
https://wpdocs.osdn.jp/関数リファレンス/get_avatar

カテゴリー
WordPress

画像やファイルへの直リンクを防ぐ

会員用サイトなどで、ログイン制にしても画像への直接リンクは見えてしまいます。
下記の.htaccessをuploadフォルダへ設置すればOK。

.htaccess

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?sample.com/ [NC]
RewriteRule .(jpg|jpeg|png|gif|pdf|xlsx|zip|pptx)$ - [NC,F,L]
</IfModule>

※RewriteCondのドメインは適時変更してください。

カテゴリー
WordPress

カスタム投稿名を出力

カスタム投稿名(ラベル名)を一発出力!メモー。

<?php echo esc_html(get_post_type_object(get_post_type())->label); ?>

以上!

但し、投稿がないと表示されないので注意(少しはまった)。

カテゴリー
WordPress

URLをショートコードに登録する

メモ。WordPressでホームのURL等をショートコードに登録する。
記事投稿や固定ページで[url]を書くとホームURLが取得できる。

function getUrl(){
	return esc_url( home_url(  ) );
}

add_shortcode( 'url', 'getUrl');

function getTemplateUrl(){
	return get_template_directory_uri();
}

add_shortcode( 'template_url', 'getTemplateUrl');

カテゴリー
WordPress

メディアのデータを一括削除する方法

WordPressのメディアに登録済の3000件のデータを削除する必要があり、
ぽちぽち20件ずつ削除していたのだが、自分の愚かさにハッとなり、検索してみた。
下記、引用です。

https://ja.forums.wordpress.com/topic/986
メディアを削除する方法

サイト全体を空にする(投稿・コメントなども含めて)という操作はこちらで行うことが可能ですが、画像「のみ」をすべて削除というオプションは申し訳ありませんが現在ありません。
一つ少し楽にできる方法は、メディアライブラリページの一ページごとの表示数を変えるというやり方です。

1. メディアライブラリの一覧ページで、右上の「表示オプション」タブをクリック
2. 上部にメニューが表示されるので、その中の「メディア」の数を100や200など大きいものに変更する(デフォルトは20)
3. 「表示オプション」タブを閉じ、一括選択して消去の操作を行う

ということで、右上の表示オプションから「ページごとに表示する項目数」を増やし、
無事作業を終えることができました。ありがとうございます。