WP-Cron: Scheduling Tasks in WordPress

WP-Cron: Scheduling Tasks in WordPress

SNAP is using the built-in WordPress scheduling service called WP Cron for autoposts. When it set this way, all time consuming autoposting tasks are done at the background. This increases the performance and makes some advanced functionality possible.  The default setup of WordPress Cron Jobs can cause duplicate autopostings, especially on popular websites.

setup-cron-001

What is Cron Job?

Cron is a UNIX directive that used for scheduling jobs to be executed at some time in the future. A cron could be used to schedule a job that is executed periodically or just once. Cron is a daemon process that runs continuously, waiting for specific times to trigger the scheduled events to occur. Windows also has a similar scheduler service.

However WP Cron is not a real “Cron Job”. Unlike regular Cron Jobs, which run at certain times or dates based on crontab (cron table) configuration file, the WP-Cron service runs every time visitor comes to the WordPress based website. This helps WordPress based websites to run properly on different kinds of server environments because it eliminates a lot of server-specific requirements. WordPress checks if there is a need for WP-Cron to run on every page load. If there is such need, then it tries to make an ajax request to the wp-cron.php file.

Issues with WP-Cron

This way of things has two potential problems.
1. WP-Cron can fail to complete all or some its jobs or it might not work at all on some environments that restrict background jobs or have short execution timeouts.
2. WP-Cron can run the same tasks several times. This is especially the case for high traffic websites or websites with specific configurations. This is what could produce duplicate posts made by SNAP.

Solution

To avoid multiple instances of WP-Cron running on your server and making duplicate posts, you can try to use the default way of setting up cron jobs on a Linux server.

Please make sure that your web host allows you to setup Linux cron jobs. The way this is setup would be different based on the control panel offered by your host.

There are two steps involeved:
1. Disable internal WP-Cron execution on page load.
2. Setup a real cron job function that makes a request to the wp-cron.php file at regular intervals based on your preference.

1. Disable internal WP-Cron function

Open the wp-config.php file in the text editor of your choice and add this to the bottom of it (just before the require_once line)

//Disable internal Wp-Cron function  
define('DISABLE_WP_CRON', true);

This will stop the internal WP-Cron function from executing on every page load.

Please note:You are not disabling the WP Cron itself. You just disabling it’s execution by WordPress on every pageload and replacing it with execution by regular cronjob. cronjob will execute all WP Cron tasks.

2. Setup a real cron job

You will need to execute one the following commands by cron job

cURL (Preferable)

curl -s -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64)" http://YourWebsiteURL.com/wp-cron.php?doing_wp_cron

OR

wget

wget -O /dev/null http://www.YourWebsiteURL.com/wp-cron.php?doing_wp_cron > /dev/null 2>&1


*** Replace http://www.YourWebsiteURL.com with your real website URL. This URL must be the full URL to your WordPress installation.

cPanel

To configure a real cron job, you will need access to your cPanel

1. Log into your cPanel.

2. Scroll down the list of applications until you see the “cron jobs” link. Click on it.

setup-cron-001

3. Under the Add New Cron Job section, choose the interval that you want it to run the cron job. We recommend to set it to run every 1 minute. Enter the command mentioned above to the “Command” field.

setup-cron-002

4. Click “Add new Cron Job”. You should see it in the “Current Cron Jobs” list now.

setup-cron-003

Important: If you choose WGET command, please make sure wget is not blocked!

Some configurations and overprotective plugins restrict an access of your site by wget.

The good example would be popular “Better WP Security” WordPress plugin. It adds the following line to the .htaccess file:
RewriteCond %{HTTP_USER_AGENT} ^Wget [NC,OR]

You need to remove this line, or the whole setup won’t work.

Plesk

If you are subscribed to several hosting packages and have access to several webspaces associated with your account, in the Subscription menu at the top of the screen, select the required webspace.

1. Go to the Websites & Domains tab > Scheduled Tasks (in the Advanced Operations group).

setup-cron-004

2. Click Schedule New Task.
3. Leave the Switched on checkbox selected.
4. Specify when to run your command:
Minute – enter 1 or * (values from 0 to 59)
Hour – enter * (values from 0 to 23)
Day of the month – enter * (values from 1 to 31)
Month – enter * (values from 1 to 12, or select the month from a drop-down box)
Day of the week – enter * (value from 0 to 6 (0 for Sunday), or select the day of the week from a menu).

5. Specify which command to run. Type it into the Command input box. You need to enter the command from above in the Command input box.

setup-cron-005

6. Click OK.

Important: Make sure wget is not blocked!

Some configurations and overprotective plugins restrict an access of your site by wget.

The good example would be popular “Better WP Security” WordPress plugin. It adds the following line to the .htaccess file:
RewriteCond %{HTTP_USER_AGENT} ^Wget [NC,OR]

You need to remove this line, or the whole setup won’t work.

External WP Cron services

Alternately (if this looks too complicated or your hosting providers does not allow cron jobs ) you can use external cron services like http://www.easycron.com/

Update: EasyCron published a special tutorial of How to Setup EasyCron for NextScritps SNAP

Tutorials

How SNAP Sets Images

How SNAP Sets Images

All types of posts for Facebook, Google+, XING

All types of posts for Facebook, Google+, XING

How to format Facebook post with attached link

How to format Facebook post with attached link

All types of Facebook posts

How to format Facebook post with attached link

All types of posts to Facebook with video

All types of posts to Facebook with video

Why we don’t have a Twitter characters counter

Why we don’t have a Twitter characters counter.

Plugin Setup/Installation

All types of posts to Facebook with video

SNAP PRO Plugin Upgrade/Activation

All types of posts to Facebook with video

WP-Cron: Scheduling Tasks in WordPress

WP-Cron: Scheduling Tasks in WordPress

All Supported Networks

All types of posts to Facebook with video

How to check if curlSSL is working

WP-Cron: Scheduling Tasks in WordPress

How to completely remove SNAP

WP-Cron: Scheduling Tasks in WordPress

SNAP Features

Accounts

Most Major Social Networks are supported.

Settings

SNAP has a lot of settings to configure.

Powerful Filters

Set what posts go to each network based on categories, tags, type, format and many more criteria.

Limit Autoposting Speed

Importing many posts at once? Not a problem. SNAP can query your autoposts and publish them one by one in defined time intervals.

Custom URLs for AutoPosts

You can set a custom URL for the link shared by SNAP
l

Autopost New Blogposts

SNAP will automatically post your newly published blogposts (pages, events, products, custom post types) to all your defined social media accounts.

Quick Post

Send a quick post to all configured social networks without creating a WordPress blogpost.

"Spin" message post templates

Automatically change/alter the words or sentences in your social media posts 

Emoji Support

Emoji Support
+

Export/Import Plugin settings

You Export and Import all plugin settings as backup or to move it to another site

Format your posts

Format your posts using flexible replacement tags. make text posts, share links, or post images.

Hashtags

SNAP can post tags, categories and any other custom WordPress taxonomies as Hashtags

Additional URL Parameters

Add additional parameters to the links posted by SNAP.

URL Shorteners

URL Shorteners: bit.ly, is.gd, Rebrandly, YOURLS and built in WordPress URL Shortener

See what was posted

You can see the direct links to the posts published by SNAP on the "Post Edit" page

SNAP Pro for Wordpress

Pro version upgrade adds the ability to configure more than one account for each social network and some additional features.

Get SNAP Pro

Latest Blogposts

Corrupted cache issue with SNAP Pro.

Corrupted cache issue with SNAP Pro.

What happened? SNAP Pro is checking for API update every 6 hours. Today (May 22, 2019) around 6:30PM EST Google Cloud messed our update server for about 7 minutes. A bunch of "lucky" sites that were checking for update during those 7 minutes got their plugin cache...

Support for “Google My Business” is coming soon…

Support for “Google My Business” is coming soon…

Support for "Google My Business" is coming this fall. Google recently made some kind of blog-a-like functionality available for local business listings. As a local business owner you can add posts, events, offers as well as products to your business listing. Although...

Facebook app review

Facebook app review

We are getting a lot of questions about upcoming deadline for Facebook app review. People are asking what should we do. The honest answer would be: "No one has any idea". We submitted several apps for Facebook review using different ways of describing and explaining...

What happened with Facebook

What happened with Facebook

What happened with Facebook? Facebook made changes to it's API access policy on May 1st, 2018. As the result we introduced our own Premium API for Facebook.  We feel that we need to explain how exactly those changes affected SNAP. Since the beginning Facebook native...

Instagram issues

Instagram issues

Instagram made some very big changes to authentication process. About 70% of our users are affected by them. Before the changes the process was quite simple: Sometimes Instagram decided that login from SNAP is "unusual" and asked for confirmation. You just had to open...

SNAP for Wordpress

SNAP for Wordpress

SNAP for Wordpress MultiUser

SNAP for Wordpress MultiUser

SNAP API

SNAP API