未加星标

Advanced WordPress Development: Using the Command Line

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏
October 8, 2016 #advanced development #command line #terminal

While mention of the command line strikes fear into the hearts of many developers, the truth is that it is an easy to use and immensely helpful tool for development.

Once you understand how it works, the command line is straightforward to use and will streamline how you work, saving you a bunch of time.

This is the fourth post in our six-part series focusing on WordPress for advanced developers. This series follows on from our popular WordPress Development for Intermediate Users , which introduced you to some meaty coding topics, including theme development in detail, making themes customizer-ready, building plugins, custom post types and taxonomies, queries and loops, custom fields and metadata, and localization.

In this tutorial, I’ll show you how you can use the command line to perform common tasks and make your lifeso much easier.

Note: It’s important that you have a working knowledge ofphp as this is the foundational language of WordPress for this series, which covers advanced topics aimed at developers. I’ll be referring to code snippets throughout this series.

What is the Command Line?

Back in the good old days, there was no real user interface. You could only get things done by issuing text-based commands using the terminal. Nowadays, we don’t have terminals we have highly integrated computers that come with terminal emulators that allow us to issue commands using a graphical user interface.

Commands can do anything from resizing an image to creating new files and switching between monitors. Anything you do via a user interface could potentially be done with commands issued in the terminal.

What makes commands even more useful is that they can be grouped together to perform many complex tasks in one go. You could write a very short script using existing commands that, when issued, would crop and optimize all images in a directory and then add them to a single zip file.

Do I Need the Terminal?

Up to a certain point, you don’t need to use the terminal, it just makes your life easier by allowing you to accomplish complex tasks a lot quicker. After a while you will come across scenarios where you must use the terminal.

A good example is accessing remote servers using SSH or coding anything in Node where you’ll need to use npm to install packages and issue commands.

In a nutshell: the terminal is not a requirement for WordPress development work, but is an amazingly helpful tool to learn that will open new horizons and prepare you to work with languages like Node.

windows, Unix and OSX Terminals

If you’re on a Unix-based system like Ubuntu, CentOS or OSX, you don’t really need to worry about setting up the command line. Apart from some minor differences, which you won’t bump into for a while, commands are all the same and many useful ones come bundled with your operating system.

Windows is another matter. Instead of a terminal it has the command prompt, which is far less useful for our purposes. I recommend installing Cygwin , which will provide many useful commands that we’ll use in this tutorial. If you need help with installation check out this handy installation guide.

Getting Started: Terminal Basics

From here on out I will presume that you’ve installed Cygwin and you have access to most commands that linux and OSX users have.

When you launch a terminal window you’ll notice that it contains a fair bit of information.


Advanced WordPress Development: Using the Command Line
Basic OSX terminal.

If yours looks a bit different, don’t worry about it, it will still work just fine. The screenshot above shows the user you are logged in as, the machine you are logged into, and the current directory you are in.

My username is “danielpataki,” the machine I am logged into is “MacBook-Pro.local” and my location is “~”. The tilde character is a short way to say “the current user’s home directory.” On my Mac this is the same as “/users/danielpataki.”

Listing and Changing Directories

To change directories you can use the cd command, followed by an absolute or relative path. I have a WordPress installation on my computer, the absolute path is “/users/danielpataki/vvv/www/wordpress-default”. Since I am in the home directory I could get there using either of the following commands:

The first command uses the relative path , which is calculated from the current directory. The second command uses the absolute path , which starts from the very top of the file system.


Advanced WordPress Development: Using the Command Line

1.6 million WordPress Superheroes read and trust our blog. Join them and get daily posts delivered to your inbox - free!

To list the contents of a directory use the ls command. It should produce output similar to the screenshot below:


Advanced WordPress Development: Using the Command Line
Listing A Directory

Note: hidden files will not be shown when listing the directory like this. You’ll need to add the -a option to the command to include them: ls -a


Advanced WordPress Development: Using the Command Line
Hidden Files The Anatomy of a Terminal Command

When I first started using the terminal, I was always confused about all the little additions commands use, such as options, arguments, flags, operators, switches, oh my… I later found out that the confusion is well founded there is no governing body behind naming these things.

Let’s simply map out what variations commands can have and you’ll see things much more clearly.

I’ve created a faux command which contains most elements you’ll see in commands.

The name of the command is “loremipsum,” the idea is that it would spit out some filler text for you. I’ve used two flags . “-p” is supposed to wrap paragraphs in p tags. “-h” is supposed to create headings using heading tags.

The command also has a single argument: “ http://fillerama.io/, ” the source of the lorem ipsum text. I’ve added a parameter: “ length=5”, which should generate five paragraphs.

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: WindowsWordPHPMacBookLinuxUbuntu
分页:12
转载请注明
本文标题:Advanced WordPress Development: Using the Command Line
本站链接:http://www.codesec.net/view/482412.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(25)