Skip to content

Instantly share code, notes, and snippets.

@shrinkray
Created September 17, 2024 20:18
Show Gist options
  • Select an option

  • Save shrinkray/830a7b8c4d95769e85186b7f8d40a9db to your computer and use it in GitHub Desktop.

Select an option

Save shrinkray/830a7b8c4d95769e85186b7f8d40a9db to your computer and use it in GitHub Desktop.
Prettier and Composer configs to use with WordPress custom themes
# Script to setup prettier and eslint for a wordpress theme
# Run this script in the root of the theme
# Run the script with bash prettierbuild.sh
# @date 2024-09-15 GRM
# Check if Git is installed
if ! command -v git &> /dev/null; then
echo "Git is not installed. Please install Git first."
exit 1
fi
# Initialize Git and create .gitignore
git init
curl https://raw.githubusercontent.com/github/gitignore/master/WordPress.gitignore > .gitignore
# Check if Node.js v20 is installed
if ! command -v node &> /dev/null || [[ $(node -v) != v20* ]]; then
echo "Node.js v20 is required. Please install it first."
exit 1
fi
# Install npm packages
npm init -y
npm install --save-dev eslint prettier @wordpress/prettier-config @wordpress/eslint-plugin
# Create .prettierrc
cat << EOF > .prettierrc
{
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"printWidth": 100,
"bracketSpacing": true,
"parenSpacing": true,
"semi": true
}
EOF
# Create .eslintrc
cat << EOF > .eslintrc
{
"extends": [
"eslint:recommended",
"plugin:@wordpress/eslint-plugin/recommended"
],
"env": {
"browser": true,
"es6": true,
"jquery": true
},
"parserOptions": {
"ecmaVersion": 2018
}
}
EOF
# Create .editorconfig
cat << EOF > .editorconfig
# WordPress Coding Standards
# https://make.wordpress.org/core/handbook/coding-standards/
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab
[{*.json,*.yml,.babelrc,.eslintrc,.stylelintrc}]
indent_style = space
indent_size = 2
[*.md]
trim_trailing_whitespace = false
[{*.txt,wp-config-sample.php}]
end_of_line = crlf
[{package.json,.travis.yml}]
indent_style = space
indent_size = 2
[*.css]
indent_style = tab
indent_size = 4
[*.scss]
indent_style = tab
indent_size = 4
EOF
# Install Stylelint and its configs
npm install --save-dev stylelint stylelint-config-standard stylelint-config-wordpress
# Create .stylelintrc.json
cat << EOF > .stylelintrc.json
{
"extends": [
"stylelint-config-standard",
"stylelint-config-wordpress"
],
"rules": {
"indentation": 2,
"no-descending-specificity": null
}
}
EOF
# Install Composer if not already installed
if ! command -v composer &> /dev/null; then
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
mv composer.phar /usr/local/bin/composer
fi
# Install PHP tools
composer require --dev squizlabs/php_codesniffer phpcompatibility/php-compatibility friendsofphp/php-cs-fixer php-parallel-lint/php-parallel-lint wp-coding-standards/wpcs
# Configure PHPCS
./vendor/bin/phpcs --config-set installed_paths vendor/wp-coding-standards/wpcs,vendor/phpcompatibility/php-compatibility
# Create phpcs.xml
cat << EOF > phpcs.xml
<?xml version="1.0"?>
<ruleset name="WordPress Theme Coding Standards">
<description>A custom set of code standard rules to check for WordPress themes.</description>
<rule ref="WordPress-Core" />
<rule ref="WordPress-Docs" />
<rule ref="WordPress-Extra" />
<rule ref="WordPress.WP.I18n">
<properties>
<property name="text_domain" type="array" value="your-theme-textdomain"/>
</properties>
</rule>
<rule ref="PHPCompatibility"/>
<config name="testVersion" value="7.4-"/>
<arg name="extensions" value="php"/>
<file>.</file>
<exclude-pattern>/vendor/*</exclude-pattern>
<exclude-pattern>/node_modules/*</exclude-pattern>
</ruleset>
EOF
echo "Setup complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment