LoginSignup
6
1

WordPressプラグイン All-in-One WP Migration で特定のフォルダやファイルを除外する

Last updated at Posted at 2022-06-14

はじめに

WordPressをローカルから本番環境に移行した際に、開発時に使用していたファイルやデータが残った状態で移行してしまったため、特定のフォルダやファイルを除外する設定がないか調べました。

対象

プラグイン All-in-One WP Migration を使用されている方で、themesフォルダ内に除外したいフォルダ・ファイルが存在する方

環境

  • Local(Local by fly wheel)で構築
  • WordPress 6.0 で確認

ディレクトリ構成

wp-content/themes
├─ mytheme
|  ├─ node_modules
|  ├─ package.json
|  ├─ package-lock.json
|  ├─ ...
|  ├─ index.php
|  └─ style.css
|
└─ twentytwentytwo

結論

All-in-One WP Migration のフィルターフックを使用して除外してあげます。

functions.php
<?php
add_filter(
	'ai1wm_exclude_themes_from_export',
	function ( $exclude_filters ) {
		$exclude_filters[] = 'mytheme/node_modules'; /* mytheme 部分にthemeフォルダの名前を入れる */
		return $exclude_filters;
	}
);

コメント部分にもある通り mytheme 部分には作成したテーマのフォルダ名を入れます。
上記の場合は node_modules フォルダのみの除外になっていますが、複数除外したいフォルダやファイルがある場合は配列にして入れてあげるといいです。

functions.php
<?php
add_filter(
	'ai1wm_exclude_themes_from_export',
	function ( $exclude_filters ) {
		$exclude_filters = array(
			'mytheme/foo',
			'mytheme/bar.js',
			'mytheme/baz.js',
		);
		return $exclude_filters;
	}
);

最後に

themeフォルダの名前を変えたりする際にいちいちパスを変更するのが面倒になりそうだったので変数で管理してみました。

functions.php
<?php

$_theme_name = 'mytheme';

add_filter(
	'ai1wm_exclude_themes_from_export',
	function ( $exclude_filters ) {
		global $_theme_name;
		$exclude_filters = array(
			"{$_theme_name}/foo",
			"{$_theme_name}/bar.js",
			"{$_theme_name}/baz.js",
		);
		return $exclude_filters;
	}
);

初めてQiitaに投稿してみました。間違ってる箇所等ありましたら指摘いただけますと幸いです!

参考文献

6
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1