Go Back

Setting Up GitHub Actions for WordPress PHP Coding Standards

Setting Up GitHub Actions for WordPress PHP Coding Standards
Posted by
Rishi Mehta
Dec 16 2024

Automating code quality checks is essential for maintaining high standards in a development project, especially in WordPress PHP projects. Using GitHub Actions, we can automate the process of testing code against the WordPress PHP coding standards. This article will guide you through setting up GitHub Actions to automatically check your code for PHP coding standards in a WordPress project.

What You’ll Need

  • A GitHub repository for your WordPress project.
  • Familiarity with GitHub Actions workflows and YAML syntax.
  • PHP and Composer set up on your local development environment for testing locally.

Here are the 5 Steps You Need to Follow

Step 1: Setting Up the Project and Installing Dependencies

In your WordPress project repository, add the phpcs tool and the WordPress Coding Standards package:

1. Install phpcs:

composer require --dev squizlabs/php_codesniffer

2. Install WordPress Coding Standards:

composer require --dev wp-coding-standards/wpcs

3. Add WordPress Coding Standards to phpcs:

./vendor/bin/phpcs --config-set installed_paths vendor/wp-coding-standards/wpcs

4. Set WordPress as the default standard:

./vendor/bin/phpcs --config-set default_standard WordPress

Once installed, you can test if phpcs is working correctly by running:

./vendor/bin/phpcs --standard=WordPress path/to/your/php/files

Step 2: Create the GitHub Actions Workflow

In your project’s root directory, create a folder named .github/workflows if it doesn’t already exist. Inside this folder, create a new YAML file (e.g., phpcs.yml).

Sample Workflow Configuration for PHP Code Standards
Below is a sample configuration to run PHP CodeSniffer using GitHub Actions:

 
name: PHP Coding Standards

on:
  pull_request:
    branches:
      - main
      - develop
  push:
    branches:
      - main
      - develop

jobs:
  phpcs:
    name: PHP Code Standards Check
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.0' # or any compatible PHP version
          extensions: mbstring, intl
          tools: composer

      - name: Install dependencies
        run: composer install

      - name: Run PHPCS
        run: ./vendor/bin/phpcs --standard=WordPress path/to/your/php/files

Explanation of the Workflow File

1. Triggering Events:

The workflow runs on every pull_request and push to the main and develop branches.

2. Jobs:

phpcs job:

The job runs on the latest Ubuntu version (ubuntu-latest).

  • Checkout code: Uses the GitHub checkout action to pull the latest code.
  • Set up PHP: Installs PHP with necessary extensions (you can customize the PHP version and required extensions).
  • Install dependencies: Installs Composer dependencies defined in composer.json.
  • Run PHPCS: Executes the phpcs command to check code against WordPress standards.

Related Article: Top PHP Development Tools for Developers

Step 3: Testing the Workflow

After committing the workflow file, push it to GitHub and create a pull request. GitHub Actions should automatically run the workflow on the pull request or push. You’ll see if there are any issues based on the output of the phpcs run in the Actions tab on GitHub.

Step 4: Reviewing and Fixing Issues

If phpcs flags any issues, you can review them directly in the GitHub Actions logs. Address these issues in your code, then push your changes to rerun the checks automatically.

Step 5: Optional: Adding Auto-Fix for Code Issues

If you want to automatically fix some PHP issues (where possible), you can use phpcbf, the built-in code beautifier that works alongside phpcs:

 
      - name: Run PHPCBF for Auto-Fixing
        run: ./vendor/bin/phpcbf --standard=WordPress path/to/your/php/files

Add this step to your workflow to attempt auto-fixing any simple coding standards issues.

Related Article: Adding WordPress A11y Speak to Announce Events for Accessibility

Conclusion

By setting up GitHub Actions for WordPress PHP coding standards, you can improve the quality and maintainability of your codebase while streamlining your workflow. With this setup, code reviews become more efficient, as you can catch coding standard violations automatically. Happy coding!

Transform your website with our expert WordPress development services. Contact us!

FAQs

1. What are GitHub Actions and how do they benefit WordPress development?

GitHub Actions are presets in form of workflows created to enhance processes within a project. In case of WordPress, it is possible to perform updates that may include quality control of the code or its compliance with coding standards for the php language.

2. How do I set up GitHub Actions for WordPress PHP coding standards?

Setting up GitHub Actions requires creating a new workflow file in the repository. This file will determine the way in which PHP_CodeSniffer with WordPress coding standards will be utilized on your codebase when changes get applied.

3. What is PHP_CodeSniffer and why is it important for WordPress development?

PHP_CodeSniffer is a tool for identifying violations of a defined coding standard in the PHP programming language. It helps in ensuring that code is compliant with WordPress PHP coding standards during WordPress development and enhances the quality of the code.

4. Can GitHub Actions be used to automatically fix coding standard violations?

Yes, GitHub Actions can be configured to run tools like PHP CS Fixer, which can automatically fix certain coding standard violations, making it easier to maintain clean and consistent code.

5. What are the best practices for using GitHub Actions with WordPress PHP coding standards?

Best practices include regularly updating your coding standards rulesets, testing your workflows locally before committing, and using caching to speed up the execution of your actions.

X