Day 30: WP-CLI and Admin Ajax


WP-CLI, or WordPress Command-Line Interface, is a powerful tool that allows developers and administrators to interact with their WordPress websites through the command line. This enables efficient management, automation of tasks, and streamlined development processes. Instead of relying solely on the WordPress dashboard, users can execute various commands directly from the terminal, enhancing productivity and control.

1. --path

  • Description: The --path parameter specifies the location of the WordPress installation directory. This is particularly useful when you want to run WP-CLI commands from a different directory.
  • Example:
  wp core update --path=/path/to/wordpress

2. --url

  • Description: The --url parameter allows you to define the URL of the WordPress site. This is handy for scenarios like working with multisite installations or when dealing with a remote setup.
  • Example:
  wp plugin install akismet --activate --url=

3. --user

  • Description: --user lets you specify the user for whom the command should run. It ensures that actions are performed with the correct user privileges.
  • Example:
  wp user delete 123 --user=naman

4. --quiet

  • Description: The --quiet parameter suppresses standard output, making command execution more silent. This is useful for scripts or automated tasks where you want minimal output.
  • Example:
  wp theme install twentytwenty --quiet

5. --color

  • Description: --color enables or disables colorized output in the command line. It provides a more visually appealing or minimalist interface, depending on your preference.
  • Example:
  wp core check-update --color=false

Important Commands and Their Functions:

1. wp core install

  • Description: The wp core install command sets up a new WordPress installation from the command line. It configures the database, site details, and creates an initial admin user.
  • Example:
  wp core install --title="Naman's Blog" --admin_user=naman --admin_password=securepassword [email protected]

2. wp plugin install

  • Description: The wp plugin install command installs a WordPress plugin from either the official repository or a specified URL. The --activate flag activates the plugin after installation.
  • Example:
  wp plugin install akismet --activate --path=/path/to/wordpress

3. wp user delete

  • Description: wp user delete removes a user from the WordPress site. The --user parameter specifies the username of the user to be deleted.
  • Example:
  wp user delete 123 --user=naman

4. wp theme install

  • Description: The wp theme install command installs a WordPress theme from either the official repository or a specified ZIP file. The --activate flag activates the theme after installation.
  • Example:
  wp theme install twentytwenty --activate

5. wp core update

  • Description: The wp core update command updates the WordPress core to the latest version available.
  • Example:
  wp core update --path=/path/to/wordpress


  1. Efficiency: WP-CLI commands with global parameters offer an efficient way to manage WordPress installations, especially when handling repetitive tasks.
  2. Automation: The ability to use WP-CLI commands in scripts or automated workflows streamlines development and maintenance processes.
  3. Flexibility: Global parameters provide flexibility by allowing you to customize commands based on specific scenarios or requirements.


  1. Learning Curve: For those new to command-line interfaces, there might be a learning curve in understanding the syntax and usage of global parameters.
  2. Potential for Errors: Incorrect use of global parameters can lead to errors, emphasizing the importance of thorough understanding before implementation.
  3. Command Complexity: Some WP-CLI commands, especially those with multiple global parameters, may seem complex for users unfamiliar with the command structure.

Introduction to admin-ajax.php:

admin-ajax.php is a crucial file that handles asynchronous requests from both the admin and front-end of a WordPress site. These requests, often performed using JavaScript, enable dynamic and interactive features without the need for a full page reload.

Key Concepts:

  1. Asynchronous Requests:
  • Definition: Asynchronous requests allow certain actions to be executed in the background without disrupting the user experience.
  • Usage: Features like live search, dynamic content loading, and real-time updates often rely on asynchronous requests.
  1. WordPress AJAX Actions:
  • Definition: WordPress AJAX actions are functions that can be triggered asynchronously using admin-ajax.php.
  • Usage: Developers define custom AJAX actions to perform specific tasks or retrieve data without reloading the entire page.

Example Scenario:

Suppose we want to create an AJAX action to fetch the latest posts without refreshing the entire page.

  1. Enqueue JavaScript:
   function enqueue_custom_scripts() {
       wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/custom-script.js', array('jquery'), null, true);
       wp_localize_script('custom-script', 'custom_vars', array('custom_nonce' => wp_create_nonce('custom-nonce')));
   add_action('wp_enqueue_scripts', 'enqueue_custom_scripts');
  1. JavaScript (custom-script.js):
   jQuery(document).ready(function($) {
           type: 'POST',
           url: custom_vars.ajaxurl,
           data: {
               action: 'custom_ajax_action',
               nonce: custom_vars.custom_nonce
           success: function(response) {
  1. PHP AJAX Handler:
   function custom_ajax_handler() {
       check_ajax_referer('custom-nonce', 'nonce');

       $latest_posts = get_posts(array('numberposts' => 5));

   add_action('wp_ajax_custom_ajax_action', 'custom_ajax_handler');


  1. Improved User Experience: Asynchronous requests enhance the user experience by enabling dynamic content updates without full page reloads.
  2. Efficiency: AJAX requests are more efficient than traditional page reloads, reducing server load and speeding up interactions.
  3. Real-time Updates: Enables real-time updates, making features like live notifications and dynamic content loading possible.


  1. Potential for Overuse: Overusing AJAX can lead to increased server load, impacting performance. It’s essential to use AJAX judiciously.
  2. Complexity for Beginners: Implementing AJAX requests may be challenging for beginners, especially when dealing with nonce verification and data handling.

Leave a Reply

Your email address will not be published. Required fields are marked *