Summary: Setting up all the required tools and packages for PHP and WordPress development, and important git commands to know.
Setting up Local Development Environment
To be able to develop with PHP and WordPress locally, The following tools makes it easy to install and manage.
- LocalWP
LocalWP is one of the best tool to automatically install WordPress along with custom selection of versions of PHP, MySQL, WebServers such as Nginx and Apache.
Download LocalWP form here localwp.com
- Mailhog
Mailhog is a open source project which makes it easy to manage email testing with a fake SMTP server running underneath. It encapsulates the SMTP protocol with extensions and does not require specific backend implementations. It comes pre installed with LocalWP so we don’t need to install it separately.
For manual install checkout out github.com/mailhog.
Setting up an Integrated Development Environment (IDE)
IDE makes the developing experience a lot simple as it makes the code writing efficient by auto complete, debugging and more. there are countless IDE like VsCode, Vim, Sublime, etc. But for the PHP and WordPress development best choice are VsCode and PHPStorm.
- PHPStorm is a really good IDE for PHP with countless customisation, easy debugging and best performance but the only catch is its a paid application.
- VsCode can do everything that PHPStorm can do with its countless extensions plus VsCode is a free to use and open source.
For extension list for VsCode which is good to have for PHP and WordPress checkout Visual Studio Code Editor for WordPress Development.
Installing Packages
- Node
Node is nothing but a javascript runtime environment which lets us develop javascript frontend and backend applications. React, Express are some of the framework which can be used to develop with Node.
Best way to install node is to use “nvm” which is a node version manager which lets us install multiple versions of node in same machine and switch between them seamlessly.
Install using these commands:
# install nvm
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
# install specific version of node
$ nvm install 16.20.0
# list all installed versions of node
$ nvm ls
- Homebrew fo macos
Homebrew is the package manager for macos, which lets us install and manage package easily.
Install using these commands:
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- PHP
PHP is the server side scripting language which is used to build websites, web applications and APIs.
Install using these commands:
# for macos
$ brew install php
# this command will install latest version of php you can also install specific version using following command:
$ brew install php@8.1
- Composer
Composer is the package manager for PHP it makes is easy to install and manage packages in a PHP project.
Install using these commands:
$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php -r "if (hash_file('sha384', 'composer-setup.php') === 'e21205b207c3ff031906575712edab6f13eb0b361f2085f1f1237b7126d785e826a450292b6cfd1d64d92e6563bbde02') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
$ php composer-setup.php
# to make composer globally accessible use following command
$ sudo mv composer.phar /usr/local/bin/composer
For latest version of composer use there official installation guide Download Composer.
- wget
wget is the CLI tool which helps us retrieve files from the we servers using HTTP, HTTPS, and FTP protocol.
Install using these command:
# for macos
$ brew install wget
- GitHub CLI
GitHub CLI is a command-line tool that brings pull requests, issues, GitHub Actions, and other GitHub features to your terminal, so you can do all your work in one place.
Install using these command:
# for macos
$ brew install gh
Install for other system form cli.github.com.
PHP_CodeSniffer (PHPCS)
PHPCS is the code reviewer tool which helps to review the code against the coding standards and gives warnings when there are any violations.
Install using these command:
# downloading phpcs
$ curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
$ chmod +x phpcs.phar
# use sudo to avoid permission error
$ sudo mv phpcs.phar /usr/local/bin/phpcs
# downloading phpcbf
$ curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar
$ chmod +x phpcbf.phar
# use sudo to avoid permission error
$ sudo mv phpcbf.phar /usr/local/bin/phpcbf
To install WPCS and PHPCompatibility:
$ cd ~/Documents
$ mkdir Coding-Standards && cd $_
# clone all required git repos
$ git clone -b main https://github.com/WordPress/WordPress-Coding-Standards.git wpcs
$ git clone -b main https://github.com/Automattic/VIP-Coding-Standards.git
$ git clone -b master https://github.com/wimg/PHPCompatibility.git PHPCompatibility
# linking cloned repos to wpcs folder
$ ln -s ${PWD}/PHPCompatibility/PHPCompatibility wpcs/PHPCompatibility
$ ln -s ${PWD}/VIP-Coding-Standards/WordPressVIPMinimum wpcs/WordPressVIPMinimum
# remember this path
$ phpcs --config-set installed_paths ${PWD}/wpcs
# to check coding standards and rule sets that are available
$ phpcs -i
# should return: The installed coding standards are MySource, PEAR, PSR1, PSR2, PSR12, Squiz, Zend, PHPCompatibility, WordPress, WordPress-Core, WordPress-Docs, WordPress-Extra and WordPressVIPMinimum
# to update WPCS and PHPComatibility
# this will return installed_path
$ phpcs --config-show
# navigate to that path and do
$ cd {returned path}
$ git pull origin main
$ cd PHPCompatibility
$ git pull origin main
Setting up Z shell and Oh My Zsh for fast command line work
Zsh is the alternative to the default bash CLI client, you can customise and manage Zsh configuration with framework like Oh My Zsh.
Install using these command:
# for macos
$ brew install zsh
# install Oh My Zsh
$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
For more details and usage about Oh My Zsh check out github.comohmyzsh.
For exploring features and how to customise zsh checkout this videos:
Introduction to Git
Git
Git is distributed version control system which allows multiple developers to work on same code simultaneously, git helps to stop accidental overwriting of each others changes.
Install using these commands:
# for mac
$ brew install git
For other os checkout git official download Git – Download.
Git initialisation:
# initialise current folder using git
$ git init
# to get information about git repo like current branch, staged changes, and more
# tracked files means git knows about and are added to the repository
# untracked files that are not yet added to repository
$ git status
Staging in git:
# staged file are which are ready to be committed
# to make single file staged use
$ git add file_name
# to make all the files in directory stages use "--all" or ".'
$ git add .
# unstage a file while retaining the changes in working directory
$ git restore --staged
Committing in git:
# commit means that the repo is ready to be pushed
# a commit needs a message which should be short and descriptive
$ git commit -m "Commit Message"
# to get log of commits in a repo use
$ git log
Branches in git:
# git branches makes keeping code separate like main branch
# and develop branch while main branch will have our production
# code while develop branch will have development code
# note: if you get the "not a valid object name: main" error
# while creating a branch then
# make sure you have at least once committed after using "git init"
# to create a new branch use
$ git branch branch_name
Changing branches in git:
# to list all branches identify current branch
$ git branch
# this will give list of branches and "*" at front of current branch eample
# *main
# foo
# switch branches use
$ git checkout name_of_branch_to_switch
# note: if you have made some changes in second branch then
# you have to first commit those changes before switching branch
# to create new branch from current branch use
$ git checkout -b new_branch_name
Merging branches in git:
# merge is used merge one branch into another
# first checkout to the branch in which you want other branch to merge
# then use
$ git merge other_branch
# if there is a merge conflict then git will automatically open default
# editor or you will manually need to open the conflited files to resolve
# them after that you can commit and the merge will be completed
Deleting branches in git:
# to delete a branch
$ git banch -d name_fo_the_branch_to_delete
Undoing commits in git:
# note: use git log to get hash of commits
# to undo the changes in a previous commit by creating a
# new commit that undoes the changes use
$ git revert bad_commit_hash
# to undo the changes in a previous commit by deleting all
# the commits after that
$ git reset bad_commit_hash
Interacting with remote repositories in git:
# to clone a remote repo
$ git clone repo_url
# to cone specific branch
$ git clone --single-branch --branch branch_name repo_url
# to push code changes after commit to remote repo use
$ git push
# to fetch changes from remote use
$ git fetch origin branch_name
# to pull changes form remote use
$ git pull origin branch_name
# Note: fetch only updates our local copy of the remote branch and does not merge them into current branch while pull merges those changes into current branch.
Git advanced usage:
# GitHub submodules is the feature that allow us to include
# another git repo as subdirectories inside our main git repo
# to add submodule use
$ git submodule add repo_url path_where_to_keep_submodule
# to clone a repo with submodules first clone the repo then use
$ git submodule update --init --recursive
# git stash is used when you want to switch branch but don't
# want to commit to the current branch
# to stash use
$ git stash
# to clear stash
$ git stash clear
# git archive is a git command used to create a compressed file containing the all files in repo of specific branch or commit
$ git archive --format=zip --output=archive.zip branch_name
For more details checkout official git docs git/doc.
Leave a Reply