• Hello,

    I have been running cron from last few years without any issue but after updating the wordpress version 6.0, cron stopped working.

    For this, I have created a file on root and using below code to use the native WordPress Core function.
    require_once( dirname( FILE ) . ‘/wp-load.php’ );

    If I run the cron URL in the browser then it works but with cron it’s not working automatically.

    Nothing works after this code in the cron’s file. Please let me know if you guys have any solution for this. As I knew this is the only way to able to use the native WordPress Core function.

    Thanks!

    • This topic was modified 2 years, 5 months ago by Jan Dembowski.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Are you using wp-cron or server cron jobs? wp-cron callbacks should already be in WP namespace so shouldn’t need wp-load.php.

    In any case, requiring wp-load.php is discouraged due to portability issues between servers. You could instead execute /wp-admin/admin-post.php with an appropriate “action” query var, which then fires a related action hook to execute an action callback you’ve added that runs the PHP you want. But it’d be easier to execute a wp-cron callback instead.

    Thread Starter rooprai

    (@rooprai)

    Hello,

    Thanks for your reply.

    I am using server cron jobs. Actually it is created by an old developer, so we are following same method. Cron was working fine before version 6.0.

    Can you please show me an example for how can I handle with /wp-admin/admin-post.php?

    Moderator bcworkz

    (@bcworkz)

    Sadly, admin-post.php is not well documented within official WP docs. Its use is explained on this 3rd party page. Unfortunately, the page isn’t specifically focused on your usage, so you’ll need to do a little interpretation. On the plus side, it explains why we should not use wp-load.php and what the viable options are.

    The page examples focus on creating a dynamic CSS stylesheet, but it does so via admin-post.php. You can skip over the CSS specific parts. It also assumes HTTP requests, where you’re using terminal commands (I assume). The crux of using admin-post.php is there needs to be a value in $_REQUEST['action']. Simple enough with HTTP. I’m unsure how to accomplish that with a terminal command. I believe it requires using php-cgi instead of just php.

    All the PHP in your current file (except the wp-load.php line) needs to be incorporated into an action callback. (the // do something here... in the page’s example). All related code should be incorporated into a WP plugin. This entails adding a particular comment header to the file and placing it in the right place (/wp-content/plugins/). More on creating a plugin. The linked doc assumes you’d be starting with a new empty file. You of course already have one. It’s just in the wrong place and needs some modification.

    Thread Starter rooprai

    (@rooprai)

    Thanks for your reply.

    I will try your solution but it doesn’t look easy one.

    Actually we are fetching fashion show images from third party FTP and saving those images as a WordPress posts.

    Moderator bcworkz

    (@bcworkz)

    “easy” or not is relative ?? There are a number of elements to address, but each one is relatively simple. (I’ve no experience with php-cgi though) The other alternative is to use WP Cron instead of server Cron.

    You’re still looking at making your code into a plugin. That entails adding a header comment and moving the file. And making your code into an action callback (by turning it into a function declaration). Same things as above. But instead of passing an “action” value to admin-post.php via php-cgi, you schedule an event in WP.

    TBH, server Cron is better, but WP Cron will save you from having to implement php-cgi, which could involve installing another Apache module if it’s not already available. Ease of doing so depends on your hosting situation.

    Yet another way to accomplish command line tasks (avoiding the need for php-cgi) is the WP Command Line app (WP-CLI). I’ve no experience with this either, but it is an option available to us. Much like php-cgi, it’s an app installed on the server. It’s probably the least “easy” option, but I’m a poor judge of what’s easy since I’m lacking necessary experience.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Cron giving issue after update wordpress version to 6.0’ is closed to new replies.