canstockphoto75148059

Tutorial: Add Custom Functionality To WPCD, Part 1

WPCloud Deploy is used by developers, agencies and power-users who want absolute control over the deployment of their WordPress servers and sites. This means customizing WPCD on multiple levels:

  1. Skinning (make it ‘pretty’ and including it in a custom branded customer offering)
  2. Run custom server scripts when provisioning a server for the first time
  3. Run custom scripts when provisioning a site
  4. Add options to the WPCD dashboard for on-demand use.

In this article we’re going to focus on the fourth option and show you how a developer can add custom functions to the WPCD dashboard.

What Can You Do With Custom Functions?

With your custom functions connected to the dashboard you can connect any BASH command you like to a button or toggle. This includes wp-cli commands as well.

For this tutorial we’re going to take our official sample add-on from github and modify one of the commands. You will then be able to take it to connect up your own commands as you see fit.

Get The Sample Add-on

Download our official sample add-on fro github into a folder – either directly or using your favorite git client.

Git Repository: https://github.com/WPCloudPanel/wpcd-sample-addon

If you want to see what it looks like as-is you can upload it to your WPCD site and activate it just as you would any other plugin. At that point you should see a new tab called Sample Add-on with three objects in it.

The tab has some functionality already active – the UPDATE ALL PLUGINS button does work and will attempt to update all plugins on a site.

Explore The Sample Add-on Code

The add-on consists of two core files.

  • wpcd-sample-addon.php: This is located in the root and contains the plugin bootstrap code and other housekeeping items. You will usually modify this to do things like changing the add-on name, description, etc.
  • includes/wpapp-tabs-sample.php: This is where the core logic of the add-on resides.

Close to the end of the includes/wpapp-tabs-sample.php file is a function called sample_action_a. This is the function that currently updates all the plugins and is linked to the Update All Buttons screen object.

Your First New Command

The simplest thing you can do is simply modify the existing BASH command. Believe it or not, the existing command is on a single line. All you have to do is change it to make the button do something completely different.

Let’s change it to a different wp-cli command – how about we issue the regenerate thumbnails command? So, for this example we will modify the line to change the:

wp plugin update --all 

portion of the command to:

wp media regenerate --yes --only-missing

That portion of the file will now look like this:

Theoretically, if you were to run the new code and click on the “Update All Plugins” you would execute this command successfully.

And indeed it does – but you will not know it. This is because you will see an error popup instead.

The error pops up because the output of this new command is different from the old one. So we have to change the code that evaluates the output to determine if the command was successful.

The output of the wp media regenerate command will include two phrases that both indicate success:

  • Success: Regenerated 1 of 1 images.
  • Warning: No images found.

Any output that does not include one of these two messages means that the command likely failed. So let’s modify the code to account for these messages. The new code will look like this:

We also ended up changing the message the user sees if the command is successful.

Now, when you click the button, you should see a success message instead of an error message.

Cleaning Up The Plugin

Now that you’ve got the command working, here are some things you might want to do:

  • Change the text of the button – you can do that in the get_server_fields_sample function.
  • Remove extra screen objects – you can do that from the same function.
  • Change the name, description and author of the plugin.
  • Change the main filename of the plugin
  • Change the text domain (currently set to ‘wpcd’)

If you’re reading this article and using the plugin you already know how to do these things so we aren’t going to make this article any longer and bore you with those details here. We just wanted to get you to the core of the sample plugin so you can quickly create your own simple custom functions.

Wrapup

As you can see, it’s very easy to create a custom plugin to add new functions to the WPCD screens. With this method, you are limited to commands that can run within the PHP Execution Limit. We cover longer running commands in part 2 of this series.

Even with the time limitations you can still run any bash command or group of chained bash commands!

Can you think of the possibilities that this opens for offering custom unique server and app functionality to your customers?

Or maybe you can stop logging into servers on a regular basis to run the same command over and over again. Link them all to buttons and execute them with a click on each site!

Sweet, huh?

View all articles in this series.

Follow us on Twitter!  We post a lot of cool things there first.  Click the bird below!

Posted in

Purchase WPCloud Deploy

30 Day Money Back Guarantee