ワードプレスで自作テーマを制作する際に、最低限必要なテンプレートファイル(.php)を一括で作成してくれるバッチファイルの作成方法です。

tree

バッチひとつで、以下のようなディレクトリとテンプレートファイルを作成してしまいたい。

.bat
├── src ( theme name )
│   ├── functions.php
│   ├── index.php
│   ├── header.php
│   ├── footer.php
│   ├── sidebar.php
│   ├── page.php
│   ├── single.php
│   ├── archive.php
│   ├── category.php
│   ├── 404.php
│   ├── screenshot.png ( pngを生成するだけ )
│   ├── style.css
│   └── assets
│       ├── common
│       │   ├── css
│       │   │   ├── main.scss
│       │   │   └── layout.scss
│       │   ├── img
│       │   │   ├── logo
│       │   │   └── other
│       │   └── js
│       │       └── script.js
│       └── other
└── items
    ├── sample.file
    ├── sample.file
    └── sample.file

※「sample.file」は作成されません

ファイルの内容

あくまでファイルの作成が目的なので、とりあえず functions.php , header.php , footer.php 以外の index.php とか single.php とかへ記載するソースは共通とする。

functions.php

<?php
// functions.php

header.php

<!DOCTYPE html>
<html lang="ja">
<head>
<?php wp_head(); ?>
</head>
<body>
    <main>

footer.php

    </main>
    
<?php wp_footer(); ?>
</body>
</html>

index.php とか

<?php
/*
*****.php
*/
?>

<?php get_header(); ?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php the_title(); ?>
    <?php the_content(); ?>
<?php endwhile; endif; ?>

<?php get_footer(); ?>

style.css

@charset "UTF-8";
/*
Theme Name: theme name
Theme URI: theme URL
Author: theme author
Author URI: author URL
Description: theme description
Version: 0.0.1
License: license
License URL: license URL
Tags: tag1,tag2,tag3
Text Domain: text domain
*/

script.js

window.addEventListener('DOMContentLoaded', function(){
});

バッチ(.bat)

必要なテンプレートファイルや、記載するソースに応じて内容を変更してご活用ください。

@echo off

:: ディレクトリ作成
set srcDir="src ( theme name )\"
set assetsDir=%srcDir%assets\
set commonDir=%assetsDir%common\
set cssDir=%commonDir%css\
set jsDir=%commonDir%js\

md items
md %assetsDir%other\
md %cssDir%
md %jsDir%
md %commonDir%img\logo\
md %commonDir%img\other\

:: css
echo @charset 'utf-8'^; > %cssDir%main.scss
copy %cssDir%main.scss %cssDir%layout.scss > nul

:: js
(
echo.window.addEventListener('DOMContentLoaded', function(^){
echo.^}^);
) > %jsDir%script.js

:: functions.php
(
echo.^<?php
echo.// functions.php
) > %srcDir%functions.php

:: header.php
(
echo.^<^!DOCTYPE html^>
echo.^<html lang^="ja"^>
echo.^<head^>
echo.    ^<?php wp_head(^)^; ?^>
echo.^</head^>
echo.
echo.^<body^>
echo.    ^<main^>
) > %srcDir%header.php

:: footer.php
(
echo.    ^</main^>
echo.
echo.^<?php wp_footer(^)^; ?^>
echo.^</body^>
echo.^</html^>
) > %srcDir%footer.php


:: 作成したい [template.php] の一覧を作成する ( 必要に応じて変更してください )
(
echo.index.php
echo.page.php
echo.single.php
echo.archive.php
echo.category.php
echo.404.php
echo.sidebar.php
) > template_list.txt

:: index.php とか
for /f %%i in (template_list.txt) do (
    (
    echo.^<?php
    echo./*
    echo.%%i
    echo.*/
    echo.?^>
    echo.
    echo.^<?php get_header(^)^; ?^>
    echo.
    echo.^<?php if ( have_posts(^) ^) ^: while ( have_posts(^) ^) ^: the_post(^)^; ?^>
    echo.    ^<?php the_title(^)^; ?^>
    echo.    ^<?php the_content(^)^; ?^>
    echo.^<?php endwhile^; endif^; ?^>
    echo.
    echo.^<?php get_footer(^)^; ?^>
    ) > %srcDir%%%i
)

:: 作成した一覧を削除
del template_list.txt

:: style.css
(
echo.@charset "UTF-8"^;
echo./*
echo.Theme Name^: theme name
echo.Theme URI^: theme URL
echo.Author^: theme author
echo.Author URI^: author URL
echo.Description^: theme description
echo.Version^: 0.0.1
echo.License^: license
echo.License URL^: license URL
echo.Tags^: tag1,tag2,tag3
echo.Text Domain^: text domain
echo.*/
) > %srcDir%style.css

:: screenshot.png
type nul > %srcDir%screenshot.png


:: ------------------------------------------
echo;
echo Complete !
echo;
:: ------------------------------------------

pause

デスクトップにある「sample」フォルダで実行すると、以下のような構成になります

Desktop\sample\
├── src ( theme name )
│   ├── functions.php
│   ├── index.php
│   ├── header.php
│   ├── footer.php
~ 省略 ~~~
│   ├── style.css
│   └── assets
~ 省略 ~~~
└── items

参考

Windows のコマンド | Microsoft Learn

バッチファイル基礎文法リファレンス – Qiita

バッチファイルで配列を使う