投稿者「あたい」のアーカイブ

マルチサイト子サイトの管理画面wp-adminのみにbasic認証をかける

※あまりうまく行かないので検証が必要

まず、前に書いたサイトや管理画面含めて全部にかける方法。

これに関連して、管理画面(wp-admin)だけにbasic認証をかける方法です。


ここまではと同じです。

フォルダの中の構成をこういう感じにします。

一番上の階層のindex.phpはのものから中身の変更は無いです。

次に、/wp-admin/の中。

まず、.htaccess。

<Files ~ "^\.(htaccess|htpasswd)$">
deny from all
</Files>
Options -Indexes
AuthUserFile /home/サイトのパス/hoge/wp-admin/.htpasswd
AuthGroupFile /dev/null
AuthName "Please enter your ID and password"
AuthType Basic
require valid-user
order deny,allow

wp-admin/が追加になってますね。

.htpasswdは変更無いです。

次にindex.php

<?php
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* @package WordPress
*/

/**
* Tells WordPress to load the WordPress theme and output it.
*
* @var bool
*/
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/../../wp-blog-header.php' );

階層が一個下がるので、../を追加してあげてください。

これでwp-adminだけにbasic認証が付きますよ。
セキュアです。

CSS3の『flexbox』だけでフッターを一番下(最下部)に固定する

flexboxだとフッター固定がかんたん!
大好きなfooterFixed.jsもあるんですけど、これ結構他JSと干渉しやすいので、これが今の所ベスト対処ですじゃ。

いい加減一発で固定されてほしいよね。

てことで以下。

<body>
<div id="wrapper">
<main>

コンテンツが入ります。

</main>

<footer>

フッターが入ります。

</footer>
</div>
</body>
body,
#wrapper {
display: flex;
flex-direction: column;
min-height: 100vh;
}

footer {
margin-top: auto;
}

これだけですじゃ。
最近更新が多いのは仕事しまくってるからですじゃ。

下部固定バナーをスクロール時非表示で話すとフワっと出す

これでクラス名指定してあげればすぐです。
ちなみに、下部固定バナーをサイトのフッターの上で止めたいやつ と併用すると良きですね。

jQuery(document).ready(function(){
var bnrElm = jQuery(".sticky_footer"); // バナー要素

jQuery(window).on("scroll touchmove", function(e){ //スクロール中に判断する
bnrElm.stop(); //アニメーションしている場合、アニメーションを強制停止
bnrElm.css('display', 'none').delay(500).fadeIn('fast'); //スクロール中は非表示にして、500ミリ秒遅らせて再び表示
});
});

下部固定してる要素がフッターまで来たらフッターの上で止めるJS

スマホサイトとかで流行りのフッター固定バナー的なやつがスクロールしてるうちにフッターにぶち当たったときの処理。
display:flex;状態だとフッターの上に被っちゃうので、被らないようにします。

まずHTML。ちなみにソースの中の「height」は適宜変えてください。
あと、フッターの上で固定させたい要素は、固定させたい場所に書いてください。(position指定無し状態での単純な位置)
↓だとちゃんとfooterの上に書いて、positionとか指定しない場合ちゃんとfooterの上にぴちっと出ると思います。

それと、headerとかcontentとかの構成は適当です。何が大事かって、固定したい要素の#sticky_footerの器をしっかり作るのが大事なのです。
何故なのかは後述します。

<header>
ヘッダ~
</header>

<div id="content">
こんてんつ!
</div>

<div style="height:50px;width:100%;">
<footer id="sticky_footer">

#sticky_footerを固定したい!

</footer>
</div>

<footer id="main_footer">
フッタ~
</footer>

CSSです。ここでどういう要素か定義しておきます。主に装飾。

footer.sticky_footer{
background-color: #ff00ff;
height:50px;
width: 100%;
color:#fff;
text-align: center;
line-height: 50px;
}

JSです。クラス名とかは適宜変えてください。

jQuery(window).on("scroll", function() {
documentHeight = jQuery(document).height();
scrollPosition = jQuery(this).height() + jQuery(this).scrollTop();
footerHeight = jQuery("#site_footer").outerHeight();

if (documentHeight - scrollPosition <= footerHeight) {
jQuery("#sticky_footer").css({
position: "static",
bottom: "auto"
});
} else {
jQuery("#sticky_footer").css({
position: "fixed",
bottom: 0
});
}
});

こんな感じです。

あと上で言ってたことですが、ちゃんと固定したい要素と同じ高さの器を作っておかないと、いざ要素が固定された時に新要素が入ってきた!となってサイトの高さが変わってスクロールバーがグニュっと動いてフッターがガクッと落ちた感じになっちゃいますね。そういうための対策です。
これはあまり他のサイトも書いてなくて困りました。

要素をふわっとスクロールjQueryの紹介

これが一番うまく行った。

参考:jQuery FadeThis

JS

/*
* jQuery FadeThis - v1.0.0
* A lightweight, simple, yet powerful jQuery plugin for appear-as-you-scroll features.
* http://jqueryfadethis.com
*
* Made by lwiesel
* Under MIT License
*/
!function(a,b,c){var d=null,e=[],f=a(b),g=function(){};g.prototype={globals:{pluginName:"fadeThis",bufferTime:300},defaults:{baseName:"slide-",speed:500,easing:"swing",offset:0,reverse:!0,distance:50,scrolledIn:null,scrolledOut:null},init:function(a,b){this.addElements(a,b),this._setEvent(),this._checkVisibleElements()},addElements:function(d,e){var f=d===c.body?b:d,g=a(f===b?"body":f),h=this,i=e&&e.baseName?e.baseName:this.defaults.baseName;return g.is("[class^='"+i+"']")?h._addElement(g,e):g.find("[class^='"+i+"']").each(function(){h._addElement(a(this),e)}),g},_addElement:function(b,c){var d=b.data("plugin-options"),f=a.extend({},this.defaults,c,d),g={element:b,options:f,invp:!1};return e.push(g),console.log(g),this._prepareElement(g),b},_prepareElement:function(a){var b={opacity:0,visibility:"visible",position:"relative"},c=null;if(a.element.hasClass(a.options.baseName+"right"))c="left";else if(a.element.hasClass(a.options.baseName+"left"))c="right";else if(a.element.hasClass(a.options.baseName+"top"))c="bottom";else{if(!a.element.hasClass(a.options.baseName+"bottom"))return!1;c="top"}b[c]=a.options.distance,a.element.css(b)},_setEvent:function(){var a=this;f.on("scroll",function(b){d||(d=setTimeout(function(){a._checkVisibleElements(b),d=null},a.globals.bufferTime))})},_checkVisibleElements:function(b){var c=this;a.each(e,function(a,d){c._isVisible(d)?d.invp||(d.invp=!0,c._triggerFading(d),d.options.scrolledIn&&d.options.scrolledIn.call(d.element,b),d.element.trigger("fadethisscrolledin",b)):d.invp&&(d.invp=!1,d.options.reverse&&c._triggerFading(d,!1),d.options.scrolledOut&&d.options.scrolledOut.call(d.element,b),d.element.trigger("fadethisscrolledout",b))})},_isVisible:function(a){var b=f.scrollTop()+a.options.offset,c=b+f.height()-2*a.options.offset,d=a.element.offset().top,e=d+a.element.height();return e>=b&&c>=d&&c>=e&&d>=b},_triggerFading:function(a,b){b="undefined"!=typeof b?b:!0;var c={opacity:1},d={opacity:0},e=null;if(a.element.hasClass(a.options.baseName+"right"))e="left";else if(a.element.hasClass(a.options.baseName+"left"))e="right";else if(a.element.hasClass(a.options.baseName+"top"))e="bottom";else{if(!a.element.hasClass(a.options.baseName+"bottom"))return!1;e="top"}c[e]=0,d[e]=a.options.distance,b?a.element.stop(!0).animate(c,a.options.speed,a.options.easing):a.element.stop(!0).animate(d,a.options.speed,a.options.easing)}},g.defaults=g.prototype.defaults,g.globals=g.prototype.globals,b.Plugin=new g,a.fn[g.globals.pluginName]=function(c){return this.each(function(){a.data(b,"plugin_"+g.globals.pluginName)?a.data(this,"plugin_"+g.globals.pluginName)||a.data(this,"plugin_"+g.globals.pluginName,b.Plugin.addElements(this,c)):(a.data(b,"plugin_"+g.globals.pluginName,"set"),a.data(this,"plugin_"+g.globals.pluginName,b.Plugin.init(this,c)))}),this}}(jQuery,window,document);

本体とjs読み込み(1.4以上推奨らしい)

<script src="jquery.js"></script>
<script src="jquery.fadethis.min.js"></script>

あとこれをbodyの閉じタグの前に。

<script>$(window).fadeThis();</script>

あとはこんな感じで。
top/bottom/left/rightの切り替えで出てくる方向が変わるヨ

<div class="slide-left"></div>

可変幅の要素を中央寄せする

これするだけ・・・
子要素とかに干渉が無いかはわからないけど今の所うまく行ってる。

div.hoge{
display: table;
margin-right: auto;
margin-left: auto;
}

固定ページにページャー作成する

これはホントに保存版。
固定ページに投稿一覧を出力してページャー作成したいときって、なんでいつもこんなに時間がかかるんだろう・・
ってくらいうまく行かない!!!ということで我ながらカスタマイズして動いたものをどうぞ。

これを固定ページにインクルードして読み込む。

備忘録なので、「posts_per_page」とか「cat」とか、出力されるループ処理とかは適宜変えるとよろしい。

<?php
$paged = (int) get_query_var('paged');

$args = array(
'posts_per_page' => 10,
'cat' => 3,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);
$the_query = new WP_Query($args);

if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
?>
<dl>
<dt><i class="far fa-calendar-alt"></i><?php echo get_the_date('Y年m月d日'); ?></dt>
<dd>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
</dd>
</dl>

<?php endwhile; else: ?>
<p><?php echo "お探しの記事、ページは見つかりませんでした。"; ?></p>
<?php endif; ?>

<?php
if ($the_query->max_num_pages > 1) {
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%/',
'current' => max(1, $paged),
'total' => $the_query->max_num_pages
));
}
wp_reset_postdata();
?>

fontawesome5でリンクの前にアイコン&アイコンの下線を消す

こんな感じです。↓
\f0daの部分はフォントオ~サムのサイトに書いてあるのを適宜入れる。

a.arr:before {
font-family: 'Font Awesome 5 Free';
content: '\f0da';
font-size: 0.9em;
margin: 0px 3px;
font-weight: 900;
}

でもこんな感じになって不格好!!

というときはコレを追記。
解決解決。

display:inline-block;

headerやcontentsのmargin-top入れて上の要素分下げる

WordPressで開発するときとか、ヘッダーをabsoluteとかfixedにするときとか、とりあえずヘッダとかにコンテンツが隠れちゃうってことシバシバあるじゃないですか。
そゆときには以下を使う。これでheaderの下のcontentsがヘッダに隠れないよ!

色々変な値入ってるからカスタマイズしてね。

jQuery(function($){
var headerHight = Number($( 'header#masthead' ).height()) - 6;

$( '#content' )
.css({
'margin-top' : headerHight + 'px',
})

z$( 'header#masthead' )
.css({
'margin-top' : $( '#wpadminbar' ).height() + 'px',
})

});

jQuery(function($){
if($('#wpadminbar').length){
var height1 = $('#wpadminbar').height();
var height2 = $('header#masthead').height();
var height3 = 23;
$('.breadcrumb').css('top',height1+height2+height3+'px');
} else {
var height2 = $('header#masthead').outerHeight(true);
var height3 = 23;
$('.breadcrumb').css('top',height2+height3+'px');
}
});

jQuery(function($){
if($('#wpadminbar').length){
var height1 = $('#wpadminbar').height();
var height2 = $('header#masthead').height();
var height3 = 103;
$('h1.page-entry-header').css('top',height1+height2+height3+'px');
} else {
var height2 = $('header#masthead').outerHeight(true);
var height3 = 103;
$('h1.page-entry-header').css('top',height2+height3+'px');
}
});

マルチサイトWordPressの子サイト別basic認証をつける

管理画面だけにbasic認証をつけたい時はこちら

ベーシック認証をかけたいマルチサイトのURLが例えば、「http://exemple/hoge/」というURLだとしたら、WordPressが設置してあるFTPのルートディレクトリに「hoge」というディレクトリを作成します。

次に、作成したディレクトリの中に「index.php」を設置します。このindex.phpは、ルートディレクトリのindex.phpをコピーしたものになります。

ここで注意したいのが、「wp-blog-header.php」までのパスになります。ルートディレクトリから1階層下の場所にあるので「../ 」をつけなければなりません。

FTPに.htaccessと.htpasswdをアップする

このように3つのファイルが作成したディレクトリに設置されていればOKです。

具体的に
.htaccess

<Files ~ "^\.(htaccess|htpasswd)$">
deny from all
</Files>
Options -Indexes
AuthUserFile /home/****/****/hoge/.htpasswd
AuthGroupFile /dev/null
AuthName "Please enter your ID and password"
AuthType Basic
require valid-user
order deny,allow

index.php

<?php
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* @package WordPress
*/

/**
* Tells WordPress to load the WordPress theme and output it.
*
* @var bool
*/
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/../wp-blog-header.php' );