How to Make a Command Line for Your WordPress Site

Are you that level of WordPress user that could use the WordPress back-end blindfolded (figuratively of course)? You know – that user who is so fluent that doing simple tasks in the administration panel are irritating because it’s too slow? That user who maybe knows HTML and possibly even CSS but doesn’t want to venture into hardcore coding?

Well, I was that user, and after a little time snooping around in the codex, I devised a solution to quench my thirst for more WordPress power: a simple in-house plugin that adds a command line to the back-end. And in this post, I’m going to show you how to make it.

What You’ll Need for this Tutorial

  • A code editor (for simplicity I recommend: Notepad++ – Windows, TextWrangler – Mac)
  • A method of compressing folders to .zip extension (I recommend: IZArc – Windows, iZip – Mac)
  • A WordPress site
  • The ability to upload and activate plugins in the WordPress back-end
  • Absolutely NO coding knowledge (though it wouldn’t hurt to know a bit of HTML and CSS)

The Concept

The concept of a command line for WordPress has been thought of before. The difference is the existing plugins use syntax nearly identical to a real computer command line.

This command line’s syntax is more human friendly allowing you to easily memorize the commands you want to run, making it more convenient and less confusing.Furthermore, this tutorial will provide a cognitive learning opportunity that will allow you to become more fluent with the code side of WordPress.

The Goal

The end result is to be a command line for WordPress in the form of a plugin. The purpose of the command line is to provide you, being a WordPress webmaster, with a less tedious means of performing day-to-day tasks. The plugin will be extensible to leave the door open for more functionality.

Lets Get Started

Step 1 – Preparing your development environment:

Download the project JavaScript, CSS and images here (3KB – etc.zip)

The files will be in the archive “etc.zip.” Extract them. (If you do not have archiving software, there are links to free ones at the beginning of this tutorial.)

Create a folder anywhere on your computer called “CommandLine.” This will be the folder that consists of our plugin.

Place the “etc” folder, of which you’ve extracted from “etc.zip,” inside the CommandLine folder.

Step 2 – Coding the core

The following code is the header information that is required in every plugin as it gives WordPress all the information it needs to know about the plugin. Go ahead and copy & paste this code into your code editor and save the file to the CommandLine folder as ‘CommandLine.php’. You can change the information to your details as per the file headerguidelines provided by WordPress.

1
2
3
4
5
6
7
8
<?php
/*
Plugin Name: Command Line
Plugin URI: http://your-website-URL
Description: A command line utility for the WordPress guru
Author: Your Name Version: 1.0
Author URI: http://you-website-URL
*/

The segment of code we’re going to be adding will ensure that our plugin cannot be called directly via a URL. Add the following to your code editor underneath what you’ve already added.

1
if ( !defined( 'ABSPATH' ) ){ exit(); }

This next piece of code is going to load our plugin class. We are using object orientated programmingto keep our code organized. Add the following to your code editor.

1
include(realpath(dirname(__FILE__))."/class-console.php");

Now on to functions. The first function we are going to write will make sure our JavaScript is present when we are on our plugin page.

1
2
3
4
5
6
7
8
9
function CMDL_scripts(){
// ensure that our JavaScript only runs on our plugin //
if( isset( $_REQUEST['page'] ) AND stripos($_REQUEST['page'], "console-") !== false):
// Register our JavaScript and pass jQuery as per the WordPress Codex //
wp_register_script( "CMDL_SCRIPT", plugins_url()."/CommandLine/etc/main.js", array("jquery"));
// Enqueue our JavaScript and terms //
wp_enqueue_script( "CMDL_SCRIPT" );
endif;
}

Now we do nearly exactly as we just did except this time including our CSS stylesheet.

1
2
3
4
5
6
7
8
function CMDL_style(){
// ensure that our CSS only runs on our plugin //
if( isset( $_REQUEST['page'] ) AND stripos($_REQUEST['page'], "console-") !== false):
// Register our JavaScript and pass jQuery as per the WordPress Codex //
wp_register_style( "CMDL_STYLE", plugins_url()."/CommandLine/etc/style.css");
// Enqueue our JavaScript and terms // wp_enqueue_style( "CMDL_STYLE" );
endif;
}

Now we’re ready to add the code that actually adds the plugin to the WordPress back-end. We’ll be registering a custom menu page as opposed to appending a settings page to the WordPress “Settings” menu in the back-end.

1
2
3
function CMDL_custom_menu_page(){
add_menu_page( "CommandLine", "Command Line", "manage_options", "console-main", "CMDL_GUI", plugins_url()."/CommandLine/etc/icon20.png", 26 );
}

The first of the functions below is an AJAX listener. Using AJAX will allow for a more fluid user experience. The second function is a function that creates an instance of our class and displays the console.

1
2
3
4
5
6
7
8
9
10
11
12
function CMDL_ajax(){
if( isset( $_REQUEST["command"] ) AND !empty( $_REQUEST["command"] ) ){
$CMD = new CMDL;
$CMD->runCommand( stripcslashes($_REQUEST["command"]) );
}
die;
}</code>

function CMDL_GUI(){
$console = new CMDL;
$console::Console();
}

Now for the last few lines of code for our 'CommandLine.php' file. The following are all action listeners. They wait for the do_action of their respective tags to be called and execute a function specified by the second parameter.

1
2
3
4
5
6
7
8
// When the admin menu is being created, run register_custom_menu_page() function //
add_action('admin_menu', 'CMDL_custom_menu_page');
// AJAX listeners //
add_action("wp_ajax_CMDL_command", "CMDL_ajax");
// When the admin page is readying script, enqueue our script as well //
add_action("admin_enqueue_scripts", "CMDL_style");
// When the admin page is first loading, make sure our script is registered //
add_action("admin_init", "CMDL_scripts");

Don't forget to add this to the end of your file: "?>"

Step 3 - Coding the CMDL class

To prevent this post from being boring, I will not just be providing a couple of sentences as a description for a code segment. Instead, I will try to introduce common problems faced by developers and provide some insight on how to deal with them. I will also explain the more complex and intricate lines of code in more detail.

Open your code editor and create a new file. Call the file 'class-console.php' and save it to the 'CommandLine' folder. I will explain each function one at a time. You should add them to your ''class-console.php' file as they are explained.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
class CMDL{
// 0 for no limit //
const QUERY_LIMIT = 0;
// This array will hold all of our actions.//
public static $_ACTIONS = array(
"delete" => "delete",
"remove" => "delete",
"change" => "change",
"switch" => "change",
"activate" => "activate",
"deactivate" => "deactivate",
"trash" => "trash",
);
// This array will hold all of our identifiers //
public static $_IDENTIFIERS = array(
"user" => "user",
"users" => "user",
"person" => "user",
"people" => "user",
"post" => "post",
"posts" => "post",
"article" => "post",
"articles" => "post",
"theme" => "theme",
"themes" => "theme",
"plugin" => "plugin",
"plugins" => "plugin",
"media" => "media",
"image" => "media",
"video" => "media",
"audio" => "media",
);

In the code above we have started our class called CMDL. We then went on to set two arrays, the actions and the identifiers. The actions are the primary instruction, the equivalent of a command in a real command line. The identifiers are there to determine the type of data that the values are.

We also defined a class constant called "QUERY_LIMIT". When equal to 0, this will indicate that actions are to be performed on as many results as discovered by a database query. When set to a value greater than 0, this constant dictates how many results the action will take affect on.

The following code needs to be added to your code editor. I will not go into any great detail as this function simply deletes a directory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Don't worry about this function (DO COPY THIS) //
public static function deleteDir($dirPath) {
if (substr($dirPath, strlen($dirPath) - 1, 1) != '/'){
$dirPath .= '/';
}
$files = glob($dirPath . '*', GLOB_MARK);
foreach ($files as $file) {
if(is_dir($file)){
self::deleteDir($file);
}else{
unlink($file);
}
}
rmdir($dirPath);
}

This next function is arguably redundant, but I thought it necessary to keep other functions 'clean'. The function simply outputs an error and cancels the script execution, basically the same as running echo followed by exit.

1
2
3
public function throwError( $error ){
wp_die( $error );
}

Ah! Here's is one that'll wake you up. Firstly I will need to explain the syntax of a command line input. The command line input follows this basic syntax and strictly must be done like this:

COMMAND IDENTIFIER "value 1" "value 2"

Command line syntax example
An example of the command line syntax

The only leniency in this syntax is the option of using single quotes (') as opposed to double quotes (") and the fact that commands and identifiers are NOT case-sensitive. As for everything else (including the spaces between values), it is very strict and simply will not work any other way.

This next function inspects a command line input and extracts the values into an array and then returns that array and what's left of the command line input (the command and identifier).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// This function gathers and removes values from a command //
public function extract_values( $input ){
$_VALUES = array();
$length = strlen( $input );
$temporary = (string) "";
$g=0;
for($i=0; $i &lt; $length; $i++){
if($input[$i] == '"' AND strpos(substr($input, $i+1), '"') !== false){
array_push($_VALUES, substr($input, $i+1, strpos(substr($input, $i+1), '"')));
$input = substr_replace($input, "", $i, strpos(substr($input, $i+1), '"')+2);
}elseif($input[$i] == "'" AND strpos(substr($input, $i+1), "'") !== false){
array_push($_VALUES, substr($input, $i+1, strpos(substr($input, $i+1), "'")));
$input = substr_replace($input, "", $i, strpos(substr($input, $i+1), "'")+2);
}
}
return array($_VALUES, $input);
}

IMPORTANT: The above code doesn't play nice with the code block. Any instance of "& l t ;" (without spaces) should be changed to "<"

I want to break down a few things in this function:

Consider this line:

1
array_push($_VALUES, substr($input, $i+1, strpos(substr($input, $i+1), '"')));

And assume our command line input was DELETE USER "Fred" The value of $i when the if condition is satisfied is 12. So we can substitute $i:

1
array_push($_VALUES, substr($input, 13, strpos(substr($input, 13), '"')));

We can deduce that substr($input, 13) is equal to Fred" and so the strpos() (string position) of " will be 4. Leaving us with

1
array_push($_VALUES, substr($input, 13, 4));

Which appends the value "Fred" (without quotes) to the $_VALUES array.

On to our next function. This function looks at what is left of the string returned by our "extract_values" function and determines what the action (command) is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// This function tells us the action that we are performing or throws an error //
public function determineAction( $input ){
$results = $this->extract_values( $input );
$modified = trim($results[1]);
if(strpos($modified, " ") !== false){
$command = substr($modified, 0, strpos($modified, " "));
}else{ $command = $modified;
} if( array_key_exists(strtolower($command), self::$_ACTIONS) ){
$CMD = self::$_ACTIONS[strtolower($command)];
$modified = preg_replace('/ss+/', ' ', $modified);
$modified = explode(" ", $modified);
if($modified[1]){
return array($CMD, $modified[1]);
}else{
$this->throwError( "No identifier was found." );
}
}else{
$this->throwError( "The command '".$command."' is undefined." );
}
}

In this function I feel I need to explain the role of "$this". The $this variable is a reserved variable and refers to the class of which it is existent within, in this case the class CMDL.

I also will explain the line:

1
$modified = preg_replace('/ss+/', ' ', $modified);

What this line does is uses regular expression (REGEX) to remove occurrences of two or more spaces.

Just in case you don't know what you're doing right now, you should be adding these functions to your "class-console.php" file as we go through them.

Important: Like before... These code block are not doing their job well. the REGEX will look to you like "/ss+/" what it should be is "/ss+/"

The next function is one that is used to determine the identifier specified. There really isn't all too much to this function ergo I'm providing minute information.

1
2
3
4
5
6
7
8
9
10
public function determineIdentifier( $input ){
$results = $this->determineAction( $input );
$modified = trim($results[1]);
if( array_key_exists(strtolower($modified), self::$_IDENTIFIERS) ){
$ID = self::$_IDENTIFIERS[strtolower($modified)];
return array($ID, $results[0]);
}else{
$this->throwError( "The identifier '".$modified."' is undefined." );
}
}

This next function is essentially the interface that the front end uses to communicate with our functions.

1
2
3
4
5
6
7
8
9
10
11
12
public function runCommand( $input ){
$result = $this->determineIdentifier( $input );
$ID = $result[0];
$CMD = $result[1];
$Values = $this->extract_values( $input );
$Values = $Values[0];
if( method_exists( $this, $CMD ) ){
$this->$CMD( $ID, $Values );
}else{
$this->throwError( "The command '".$CMD."' is undefined." );
}
}

If for whatever reason you couldn't work out what this functions does, quite simply, it runs a command.

I want to signal out the line:

1
$this->$CMD( $ID, $Values );

This line of code runs a function of $CMD passing the arguments $ID (our identifier) and $Values (our values from extract_values). So if we again consider our example from earlier (DELETE USER "FRED"), this eventuates to this line executing as: delete( "user", array("Fred") ).

Now the code gets messy. We are now moving into creating our actual commands functionality. I've provided the functionality:

  • Delete (users, posts, themes & media)
  • Trash (posts)
  • Change (Theme)
  • Deactivate (plugins)
  • Activate (plugins)

When I explain each of the command functions, the structure will become apparent. By this I mean if you wanted to create your own command, you would just need to create a function as the name of your command and except the arguments $ID (identifier) and $Values.

We'll start with the delete command, which works with all of our defined identifiers except plugins.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public function delete( $ID, $Values ){
global $wpdb;
switch( $ID ){
case "user":
foreach($Values as $value){
$user = get_user_by('login', $value);
if($user){
wp_delete_user( $user->ID );
}else{
$this->throwError( "No such user: ".$value );
break;
}
}
break;
case "post":
foreach($Values as $value){
$limit = (self::QUERY_LIMIT) ? "LIMIT 0, ".self::QUERY_LIMIT : "" ;
$myrows = $wpdb->get_results( "SELECT id FROM $wpdb->posts WHERE post_title = '$value' AND post_type = 'post' ".$limit );
foreach($myrows as $row){
wp_delete_post( $row->id, true );
}
}
break;
case "theme":
$themeDir = realpath(get_theme_root( $value )) . "/";
foreach($Values as $value){
$value = strtolower($value);
if( is_dir( $themeDir . $value ) ){
$subjectDir = $themeDir . $value;
}elseif( is_dir( $themeDir . str_replace(" ", "", $value) ) ){
$subjectDir = $themeDir . str_replace(" ", "", $value);
}elseif( is_dir( $themeDir . str_replace(" ", "-", $value) ) ){
$subjectDir = $themeDir . str_replace(" ", "-", $value);
}if( !$subjectDir ){
$this->throwError( "The theme '".$value."' does not exist." );
}else{
self::deleteDir( $subjectDir );
}
}
break;
case "plugin":
$this->throwError( "Delete command does not support identifier: plugin" );
break;
case "media":
foreach($Values as $value){
$limit = (self::QUERY_LIMIT) ? "LIMIT 0, ".self::QUERY_LIMIT : "" ;
$myrows = $wpdb->get_results( "SELECT id FROM $wpdb->posts WHERE post_title = '$value' AND post_type = 'attachment' ".$limit );
foreach($myrows as $row){
wp_delete_post( $row->id, true );
}
}
break;
}
}

The foreach loop at the start of the function loops through every supplied value. We're using a switch control structure to execute code dependent on the identifier supplied.

Now lets look at the trash function. As previously mentioned, this function only works on posts. It's pretty obvious from the name that this function trashes posts.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public function trash( $ID, $Values ){
global $wpdb;
if( $ID == "post" ){
foreach($Values as $value){
$limit = (self::QUERY_LIMIT) ? "LIMIT 0, ".self::QUERY_LIMIT : "" ;
$myrows = $wpdb->get_results( "SELECT id FROM $wpdb->posts WHERE post_title = '$value' AND post_type = 'post' ".$limit );
foreach($myrows as $row){
wp_trash_post( $row->id, true );
}
}
}else{
$this->throwError( "Trash command does not support identifier: ".$ID );
}
}

Moving on... we'll now look at the change function. Not as blatantly revealing as the other function names, this function changes your current theme. Earlier I mentioned that I would be introducing some issues of coding in WordPress, here is a good example: If the name of the theme folder happens to have capital letters in the name then this will return a message saying that the theme was not found (As is the case when deleting themes).

If you really wanted to you could add some additional if statements to the function to ensure it finds any theme you could ever possibly find. I installed a bunch of themes from a blank search in WordPress and successfully ran the change command on all of them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public function change( $ID, $Values ){
if( $ID == "theme" ){
foreach($Values as $value){
$value = trim($value);
if(wp_get_theme( $value )->exists()){
update_option("template", $value);
update_option("stylesheet", $value);
do_action("switch_theme");
}elseif(wp_get_theme( strtolower($value) )->exists()){
update_option("template", strtolower($value));
update_option("stylesheet", strtolower($value));
do_action("switch_theme");
}elseif(wp_get_theme( strtolower(str_replace(" ", "", $value)) )->exists()){
update_option("template", strtolower(str_replace(" ", "", $value)));
update_option("stylesheet", strtolower(str_replace(" ", "", $value)));
do_action("switch_theme");
}elseif(wp_get_theme( strtolower(str_replace(" ", "-", $value)) )->exists()){
update_option("template", strtolower(str_replace(" ", "-", $value)));
update_option("stylesheet", strtolower(str_replace(" ", "-", $value)));
do_action("switch_theme");
}else{
$this->throwError( "The theme ".$value." was not found.");
}
}
}else{
$this->throwError( "Change command does not support identifier: ".$ID );
}
}

Now we get into the first of two ridiculously long functions. The deactivate function deactivates the plugins specified. This was by far the hardest of the lot to code. The plugin loops through all of the values. For every value, the function:

  • Gathers an array of the entire contents of the /wp-content/plugins/ directory
  • Determines whether each array item is a directory or file (because plugins can be in either form)
  • If it's a file, it checks each line until it finds the plugin header information
  • If it's a directory, it checks every line within every file within the directory until it finds the plugin header information
  • Extracts the name of the plugin
  • Finally deactivates the plugin.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public function deactivate( $ID, $Values){
if( $ID == "plugin" ){
foreach($Values as $value){
$path = ABSPATH . "wp-content/plugins/";
if( is_dir( $path ) ){
$plugins = scandir($path);
foreach($plugins as $plugin){
if($plugin != "." AND $plugin != ".." AND $plugin != "index.php"){
if( is_file( $path . $plugin ) ){
$handle = file( $path . $plugin );
foreach($handle as $line){
if( stripos( $line, "plugin name" ) !== false){
$final = trim(substr(strstr($line, ":"), 1));
break;
}else{
continue;
}
}
if( $final == $value ){
$file = $path . $plugin; deactivate_plugins( $file );
break;
}
}elseif( is_dir( $path . $plugin ) ){
$within = scandir($path.$plugin);
foreach($within as $file){ if($plugin != "." AND $plugin != ".."){
if( is_file( $path . $plugin . "/" . $file ) ){
$handle = file( $path . $plugin . "/" . $file );
foreach($handle as $line){
if( stripos( $line, "plugin name" ) !== false){
$final = trim(substr(strstr($line, ":"), 1));
break;
}else{
continue;
}
}
if( $final == $value ){
$fileX = $path . $plugin . "/" . $file;
deactivate_plugins( $fileX );
break;
}
}
}
}
}
}
}
}
}
}else{
$this->throwError( "Deactivate command does not support identifier: ".$ID );
}
}

The final command function is the activate function. As you probably guessed this function activates the plugins specified. This function is nearly identical to the deactivate function, although performing an antonymous function - ironic, I know. Anyway here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public function activate( $ID, $Values){
if( $ID == "plugin" ){
foreach($Values as $value){
$path = ABSPATH . "wp-content/plugins/";
if( is_dir( $path ) ){
$plugins = scandir($path);
foreach($plugins as $plugin){
if($plugin != "." AND $plugin != ".." AND $plugin != "index.php"){
if( is_file( $path . $plugin ) ){
$handle = file( $path . $plugin );
foreach($handle as $line){
if( stripos( $line, "plugin name" ) !== false){
$final = trim(substr(strstr($line, ":"), 1));
break;
}else{
continue;
}
}
if( $final == $value ){
$file = $path . $plugin; activate_plugin( $file );
break;
}
}elseif( is_dir( $path . $plugin ) ){
$within = scandir($path.$plugin);
foreach($within as $file){
if($plugin != "." AND $plugin != ".."){
if( is_file( $path . $plugin . "/" . $file ) ){
$handle = file( $path . $plugin . "/" . $file );
foreach($handle as $line){
if( stripos( $line, "plugin name" ) !== false){
$final = trim(substr(strstr($line, ":"), 1));
break;
}else{
continue;
}
}
if( $final == $value ){
$fileX = $path . $plugin . "/" . $file;
activate_plugin( $fileX );
break;
}
}
}
}
}
}
}
}
}
}else{
$this->throwError( "Activate command does not support identifier: ".$ID );
}
}

This should be the last piece of code you'll have to copy. It is the code that actually displays a command console on our plugin page.

1
2
3
4
5
6
public static function Console( $Width=null, $Height=null ){
$width = ($Width) ? "width: ".$Width." !important;" : "";
$height = ($Height) ? "height: ".$Height." !important;" : "";
$Cstyle = ($width.$height) ? 'style="'.$width.$height.'overflow:auto;"' : '';
echo '<div class="wrap"><div id="CMDL-32" class="icon32"><br /></div><h2>Command Line</h2><br/><div '.$Cstyle.' class="CMDL-console"><div id="CMDL-inner-console">Your WordPress Console<span id="CMDL-status"></span></div><input spellcheck="false" autocomplete="off" id="CMDL-console-input" type="text" style="width:100%;outline: #000 !important;color:#ffffff;background-color: #000 !important;border: 0px solid #000;font-size: 16px;font-family:Lucida Console;"/></div></div>';
}

NOTE: If you've been paying attention as you've been copying the code, you'd have noticed that there is a class that needs closing, so ensure that you paste "}" right now to just after you pasted the last function. This will end the class. Also remember to close the PHP code with a "? >". So just in case you're passively skimming through copying and pasting;

Copy And Paste:

} ?>

Step 4 - customization

As I have mentioned this plugin is extensible. I also mentioned that you didn't need to know how to code to follow this tutorial and I'm going to honor that claim. I'm now going to take you through the process of adding more functionality to your command line.

Since we have to assume you do not have any coding knowledge, you will be bound by what the WordPress Codexhas to offer.

Things to consider:

    • The codex may not always have the appropriate resources you're looking for
    • If you're adding the following example, it must be placed in the file "class-console.php"
    • copying and pasting code to the end of your document will result in an error. If you do choose to add the next example to your code it must be inside the CMDL class.

We are going to add another command called "UPDATE", this could be extended to change a variety of WordPress settings, but we're just going to have it change the WordPress tagline just as a proof of concept.

Scroll through your code until you find where your last function, "activate", ends.

On a new line add the following code:

1
2
3
4
5
6
7
8
9
public function update( $ID, $Values ){
if( $ID == "tagline" ){
foreach($Values as $value){
// Our next code will be pasted here //
}
}else{
$this->throwError( "Update command does not support identifier: ".$ID );
}
}

This adds an update function, allowing the identifier "tagline". Which leads us to adding the identifier "tagline".

In your code editor find where we added the "$_IDENTIFIERS" code. That code should end like this:

1
2
"audio" => "media",
);

After the apostrophe ',' and on the next line down, add this line:

1
"tagline" => "tagline",

Ensure the ");" still ends the array.

We are now going to do something very similar to actually add the command.

In your code editor find where we added the "$_ACTIONS" code. That code should end like this:

1
2
"trash" => "trash",
);

After the apostrophe ',' and on the next line down, add this line:

1
"update" => "update",

Ensure the ");" still ends the array.

Find and delete the line "// Our next code will be pasted here //" in the function we just added. In its place we are going to put the code that actually updates the tagline.

Add this code:

1
update_option( "blogdescription", $value );

And you're done! Now we can change the tagline of our WordPress site by running the command:

UPDATE TAGLINE "My New Tagline"

Step 5 - Check and install

Now that you have copied all of the code, you should save you files and make sure you have:

      • A folder called "CommandLine" <ul >
      • Consisting of:
      • CommandLine.php
      • class-console.php
      • A folder called "etc" <ul >
      • Consisting of:
      • main.js
      • style.css
      • icon.png
      • icon20.png

If you have all of the files then you should compress the CommandLine folder to a .zip file.

Highlighted buttons & boxes leading to the upload form

After you have the zipped folder, open WordPress and upload the zip file from the "Plugins" menu. You should then activate the plugin. You should now see the menu "Command Line".

Using the Command Line Plugin

Command Line in WP menu

Listed below are the syntax of the commands we added during this tutorial.

DELETE USERS | POSTS | THEMES | MEDIA "Value A" "Value B" "Value C"...

TRASH POSTS "Post Title A" "Post Title B" "Post Title C"...

CHANGE THEME "Theme Name"

DEACTIVATE PLUGINS "Plugin Name A" "Plugin Name B" "Plugin Name C"...

ACTIVATE PLUGINS "Plugin Name A" "Plugin Name B" "Plugin Name C"...

UPDATE TAGLINE "My new tagline"

NOTE: The spaces between the quotes of the values are not optional. When passing more than one value the spaces between the quoted values are needed.

Command Line errors
Command Line errors

When using the command line you can press the up arrow on your keyboard to return the command you last ran since you loaded the page. Entering the command "clear" (without quotes), will reset the command line. Another thing to note is the lime colored box in the top-right corner of the command line. This box will become amber when a command is running and return to lime when there are no commands executing. Any errors occurred during execution will be returned to the command line in red.

And That's It

To wrap up this tutorial I will leave you with the links to the codex pages of the WordPress core functions we used and the php.net pages.

If you are having difficulties with getting the plugin to work, leave a comment and I'll help you out.

Useful links

WordPress

php.net

Comments (3)

Participate