• Home
  • Categories
    • WordPress Troubleshooting
    • WordPress Tips and Tricks
    • SSH (Secure Shell)
    • WordPress Insights
    • WordPress Common Facts
    • Securing WordPress
  • Series Tutorials
    • Advanced WordPress Topic
  • Privacy Policy
  • Terms of Use
  • Contact Me
  • About WP Tuts Hub
Home  /  Advanced WordPress Topic • Series Tutorials • WordPress Insights • WordPress Tips and Tricks  /  Using Cron Job in WordPress – Schedule Events With WP-Cron: Part 2
Advanced WordPress Topic
July 10, 2017

Using Cron Job in WordPress – Schedule Events With WP-Cron: Part 2

zubaer Using Cron Job, WordPress Cron Job, WP-Cron Leave a Comment
Share on Facebook Share on Twitter Share on Pinterest Share on StumbleUpon Share on Reddit Share on LinkedIn Share on tumblr
Email this article!
WP Cron - All scheduled cron jobs

In the previous part of this series, we saw how to create schedule a cron, create a custom cron and unschedule it. If you missed it, I highly recommend reading that first as these two tutorials are connected. That was Using Cron Job in WordPress – Schedule Events With WP-Cron: Part 1

In this part of this Using Cron Job in WordPress series, we will see how to create an options page to show all cron jobs available in WordPress, all scheduled cron events and how to schedule a single event (instead of a recurring cron job).

Show all Cron events available in WordPress:

/**
 * Create an options page
 */
add_action('admin_menu', function() {
	add_options_page('WP Cron Settings', 'WP Cron Settings', 'manage_options', 'wptutshub-cron', function() {
		$cron = _get_cron_array();
		?>
			<div class="wrap">
				<h2>All Crons</h2>
				<?php 
					foreach ($cron as $time => $hook) {
						echo "<h3>$time</h3>";
						print_r($hook);
					}
				?>
			</div>
		<?php
	});
});

Cron Job in WordPress - See all cron events

Explanations:

Here I have registered an options page using add_options_page function which takes a good number of parameters. You can see more about this function at WordPress Codex (add_options_page). To get an array of all available cron events in WordPress, we have used a function named _get_cron_array. Anyway, this function is only for private use or only for our understanding.

Show all Scheduled Cron Events:

Now, we will enlist all of the scheduled cron events.

/**
 * Create an options page
 */
add_action('admin_menu', function() {
	add_options_page('WP Cron Settings', 'WP Cron Settings', 'manage_options', 'wptutshub-cron', function() {
		$schedules = wp_get_schedules();
		?>
			<div class="wrap">
				<h2>Scheduled Cron Events</h2>
				<pre><?php print_r($schedules); ?></pre>
			</div>
		<?php
	});
});

Show all scheduled cron jobs or events in WordPress

Explanations:

In this part, we have removed the previous _get_cron_array function and added a new function named wp_get_schedules which will return all scheduled cron jobs or events.

If we now want to make it a little bit readable by showing display name and interval aside, it will look like below:

/**
 * Create an options page
 */
add_action('admin_menu', function() {
	add_options_page('WP Cron Settings', 'WP Cron Settings', 'manage_options', 'wptutshub-cron', function() {
		$schedules = wp_get_schedules();
		?>
			<div class="wrap">
				<h2>Scheduled Cron Events</h2>
				<?php 
					foreach ($schedules as $name) {
						echo "<h3>". $name['display']. ': '. $name['interval']. "</h3>";
					}
				?>
			</div>
		<?php
	});
});

WP Cron - All scheduled cron jobs

Scheduling a Single Cron (instead of a recurring cron job):

You may find the necessity of scheduling a single event which will run only once instead of running repeatedly or recurrently, unlike a conventional cron job (like sending an email in 1 hour or make a query to a specific API). Well, in that case, you need to use wp_schedule_single_event() function which accepts three parameters (timestamps, hook and options arguments to pass to the hook functions.

Example:

wp_schedule_single_event( time() + 3600, 'wptutshub_cron_hook', array() );

I hope this article series has helped you to get started with WP-Cron event/job scheduling and how to use it in your plugin or themes. If you have any questions at all, feel free to comment below.

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)

Related

Previous Article Using Cron Job in WordPress – Schedule Events With WP-Cron: Part 1
Next Article C Program to Store Multiple Student Records Using Array of Structures

About Author

zubaer

Hi, My name is Zubaer and I am a Web Devloper. Besides, I am Hybrid Mobile App Developer. My fields of proficiency are WordPress, Laravel, HTML, CSS, JavaScript, AngularJS and PHP. You can visit www.zubaer.com to know more about me.

Related Posts

  • Using Cron Job in WordPress - - Schedule Events With WP-Cron

    Using Cron Job in WordPress – Schedule Events With WP-Cron: Part 1

  • Secure WordPress site from hacking, DDoS and Brute force attack

    How to Secure WordPress Site From Hacking, DDoS and Brute Force Attack

Leave a Reply

Cancel reply




Popular Posts

  • Using Cron Job in WordPress - - Schedule Events With WP-Cron
    Using Cron Job in WordPress – Schedule Events With WP-Cron: Part 1 December 20, 2016
  • C Program to Store Multiple Student Records Using Structure and Pointer May 27, 2023
  • SSH Secure Shell Commands
    Frequently Used SSH (Secure Shell) Commands for a Web Developer September 7, 2016
  • Update WordPress site URL via PHPmyadmin
    WordPress Replace Old URL After Domain Change September 27, 2016

Categories

  • Advanced WordPress Topic2
  • C Programming2
  • Computer Programming2
  • Securing WordPress2
  • Series Tutorials2
  • SSH (Secure Shell1
  • WordPress Common Facts1
  • WordPress Insights2
  • WordPress Tips and Tricks4
  • WordPress Troubleshooting3

Calorie Calculator Pro

  • US Units
  • Metric Units
Please enter your age
Please enter your gender
Please enter your height
Please enter your weight (Pounds)
Please select an activity level
Your body fat percentage (optional)
Please enter your First Name
Please enter your Last Name
Please enter your best email address
Please enter your age
Please enter your gender
Please enter your height
Please enter your weight (Kg)
Please select an activity level
Your body fat percentage (optional)
Please enter your First Name
Please enter your Last Name
Please enter your best email address
Please enter your best email address
Problem? Try Another

Archives

  • May 20233
  • July 20171
  • December 20162
  • September 20162
  • July 20162

Tags

File Upload Error HTTP Error Prevent DDoS and Brute Force Attack Schedule Events Secure Shell Securing WordPress SSH SSH Commands Structure and Pointers in C Structures in C Programming Using Cron Job WordPress Administrator WordPress Common Facts WordPress Cron Job WordPress Error WordPress Insights WordPress Tips WordPress Tricks WP-Cron

My Services and Products

  • Hire Me or Contact Me
  • My WordPress Plugins and Themes
  • WeboCoder
  • Privacy Policy
  • Terms of Use
  • About Us

Subscribe

    Your Name (required)

    Your Email (required)

    Random Posts

    • SSH Secure Shell Commands
      Frequently Used SSH (Secure Shell) Commands for a Web Developer September 7, 2016
    • Update WordPress site URL via PHPmyadmin
      WordPress Replace Old URL After Domain Change September 27, 2016
    • Top WordPress Plugins to Secure Your Website in 2023 May 27, 2023
    © WP Tuts Hub 2023.
     

    Loading Comments...