cron jobs with membership plugin

Hi

I thought i read earlier that cron jobs on wordpress only run when a user logs onto the site.

I am just wondering as I have created some renewal notification emails (under communications in the membership plugin), but none of the emails have been sent. I am able to send myself emails, so emails are working on the site but i am confused why no emails have gone out.

Any ideas? I am working on a site where there are a few hundred members awaiting an annual renewal email. A speedy reply would be much appreciated.

Many thanks

Justin

  • Michelle Shull

    Hey there, Justin!

    You're almost correct, WordPress cron runs when someone visits your page, rather, when a visitor is detected, WP checks to see if there are any waiting cron jobs and fires them as the page loads. Here's a better description: http://code.tutsplus.com/articles/insights-into-wp-cron-an-introduction-to-scheduling-tasks-in-wordpress--wp-23119

    At the bottom, the article also explains how to take advantage of your server's cron to offload the job from WordPress itself.

    His method only works with single site installs, however, so if you're running a network, we need to look for other options.

    Hope this helps!

  • Justin

    Hi Michelle

    Thanks for the quick reply.

    I see what you mean.

    We had some custom development done on the site so that it only shows membership products at certain times of the year. This makes use of the Cronjob Scheduler Plugin: https://wordpress.org/plugins/cronjob-scheduler/.

    I see that this plugin doesn't need a visitor to the site for the jobs to fire. Is there a certain cron job task i should add into this plugin? I have a number of jobs scheduled in the plugin and I'm not sure what each does (the developer set this up). Should i have a certain task set up for membership plugin jobs?

    Thanks again.

    J

  • Michelle Shull

    Hi, Justin!

    Sorry for the delay here, we're a bit understaffed for the holiday.

    I've reached out to one of my colleagues who's better versed in custom crons than I am to help find the best way to schedule your membership renewal emails. I do suspect that some of the other cron jobs the previous dev had initiated may be interfering with this cron firing as it should, but I'd need to take a closer look. Can you grab me a screenshot of the settings for the cron plugin's jobs? Or, if you're comfortable, you can allow support access and I can look directly. If this is ok, just grant me temporary admin access to your site by clicking "Grant Access" button in the WPMU DEV Dashboard Settings from the following path and reply on this thread after granting it?

    Admin -> WPMU DEV -> Support -> Support Access Tab

    If you have not installed WPMU DEV Dashboard plugin yet, kindly do that here : https://premium.wpmudev.org/project/wpmu-dev-dashboard/ and then allow access as per the above process.

    Thanks, Justin!

  • Justin

    Hi Michelle

    Thanks for your reply. Hope you are having a nice Christmas.

    I am using the Cronjob Scheduler plugin (settings>cronjob scheduler). I can see there is a job set up called 'membership_communications_process' and it is scheduled to run every 5 mins, even when i run it manually nothing happens. Maybe this has been set up wrong but i cannot tell. In the cronjobs.php i have taken a guess and added:

    function membership_communications_process () {
    // code to execute on cron run
    } add_action('membership_communications_process', 'membership_communications_process');

    but this has not made a difference.

    I have granted support access so if you are able to have a look it would be greatly appreciated.

    Thanks again.

    Justin

  • Tyler Postle

    Hey Justin,

    Hope you're doing well today and thanks for your reply!

    Do you mind installing this plugin and running a debug on your cron to see what could be happening there: https://wordpress.org/plugins/debug-bar-cron/

    You will need to install this plugin as well since the above one is an add-on for this: https://wordpress.org/plugins/debug-bar/

    You can remove them after you're done debugging :slight_smile:

    Also, it doesn't look like support access has been granted, none of your registered sites reflect support access being available. Could you try granting it again just to be sure.

    Look forward to hearing back Justin!

    All the best,
    Tyler

  • Justin

    Hi @Tyler

    Thanks for your quick reply.

    Apologies, i went onto the support tab but didnt actually click grant support access - i have done this now so you should be able to connect.

    I have installed the two debug plugins on the site but haven't had a chance to run anything yet. I will let you know what happens but in the meantime feel free to have a look if you get a minute, I would be very grateful.

    Many thanks

    Justin

  • aristath

    Hello there @Justin, I hope you're well today!

    Thank you for granting us access to your site!
    I just logged-in there and took a look at your settings.
    From what I see there are 15(!) updates available for your site, including WordPress core and the Membership plugin.
    Could you please make sure everything is up to date?

    From your dashboard I went to WPMUDEV => Support => System Info.
    On the "WordPress" section there, I see that you've set DISABLE_WP_CRON to true (probably in your wp-config.php file) but you have not set an alternative method.

    Could you please re-enable WP Crons on your site?
    Is there a particular reason you've installed the cronjob scheduler plugin and override the default WP Cronjobs?

    Please advise,
    Ari.

  • Justin

    Hi @Ari

    Thanks for your reply.

    The site has had some development to the membership plugin so i have been advised not to update this - the development is that, when a member signs up, admin approve the member by moving their access level from 'unapproved' to 'approved'. When admin do this the development automatically sets the expiry date of the subscription to the 31st December that year.

    Another piece of development we have had done is we have 4 different prices for the membership which, 1 for each quarter of the year. There are 4 different versions of the same membership product and depending on what date it is depends on which products are displayed as public or private. This would be why the wp-cron is switched off.

    The wp-cron has be disabled but we are using the cronjob scheduler plugin to run the cron jobs. I have attached a sceenshot of the job that is set up. I am not sure that this has been set up properly though, can you advise?

    Many thanks again

    Justin

  • Michelle Shull

    Heya Justin!

    This is what my colleague said:

    Also the plugin he is using requires a Unix Crontab schedule. So if any of his corn jobs worked before its an issue with the add action code. Otherwise the plugin may not be configured correcty and you can hook the email sending function to something like wp footer and trigger it once and then immediately unhook it.

    He recommends using a function in functions.php to send the renewal messages if you need an immediate fix, and I've invited him (he's on the second level team) to swing by the thread as well.

    When you get a chance, if you can install and run the two plugins Tyler recommended and let us know the results, that will help a lot, too.

    Thanks, Justin!

  • Justin

    Hi Michelle

    Hope you are well.

    I have installed the 2 debug plugins Tyler mentioned. I have attached a screenshot of what it says for the membership_communications_process job.

    Do you know what sort of thing i would need to write to trigger the emails manually as a temporary fix? Apologies, my code writing isn't great.

    I am not sure on how to set up tasks on the cronjob scheduler plugin so i am not sure that it has been set up properly. In my previous post there is a screenshot of the membership_communications_process cronjob but i have no idea what this references/triggers, i cannot see any reference to the membership_communications_process job anywhere else in the plugin, although when i run the job manually it says action was triggered successfully. It would be great if you colleague could have a look for me.

    Much appreciated,

    Justin

  • Michelle Shull

    Hello, Justin!

    Hope you're doing well, too! Thanks for the nice words. :slight_smile:

    I have installed the 2 debug plugins Tyler mentioned. I have attached a screenshot of what it says for the membership_communications_process job.

    Awesome! This actually looks fine to me, it's not giving any indication of an error, so I'm still not sure why your crons aren't firing.

    Do you know what sort of thing i would need to write to trigger the emails manually as a temporary fix? Apologies, my code writing isn't great.

    I'm personally a pretty green programmer myself - but I put in a flag for second level support, which includes Jude, who's been helping me with the feedback here. Second level are brilliant with code, it will be a five minute task, whereas for me it would probably be next week sometime. :slight_smile:

    I am not sure on how to set up tasks on the cronjob scheduler plugin so i am not sure that it has been set up properly. In my previous post there is a screenshot of the membership_communications_process cronjob but i have no idea what this references/triggers, i cannot see any reference to the membership_communications_process job anywhere else in the plugin, although when i run the job manually it says action was triggered successfully. It would be great if you colleague could have a look for me.

    The membership_communication_process is the file that checks to see if any membership communications need to be sent, and sends them if they do. Your cron is set up to have this file triggered every five minutes (which you could probably tone back to a few times a day, unless you're dealing with an extremely large number of members, with constantly changing subscriptions.) Other than being a bit overzealous, it all looks a-okay to me. We'll let Jude chime in here as well.

    We live on opposite hemispheres, so he'll be in after my shift is over tonight, and it can take a little longer in general to hear back from second level, but he'll check in as soon as he can.

    Thanks, Justin!

  • Jude

    Hey Justin,

    I'll need some details to quickly go in and fix this for you. Im guessing it'll be much faster that way instead of going back and forth on this thread.

    Once done I'll post code snippets here so if you need to do this in the future you need not wait for me.

    I am not able to see the support access enabled for your site, it must have expired. Anyway I will need an admin account to your WordPress install and cPanel details to inspect the server corn details.

    Can you please send in:

    - Mark to my attention - Attn: Jude Rosario
    - Link back to this thread
    - Include admin/network access (WordPress admin username/password)
    - Include cPanel/WHM (I will need to look at the DB so need PHPMyAdmin or similar)
    - Include FTP
    - Include MySQL root username and password

    On the contact form, select "I have a different question", this ensures it comes through and gets assigned to me.

    Also just letting you know as soon as I get this I'll manually debug and trigger the corn, so please check if everything else like the content of the message and the recipient list are in order

    Cheers
    Jude

  • Justin

    Hi Jude

    Many thanks for your message.

    I am gathering what is required and will send across using the contact form shortly. I am just awaiting the host to let me know a URL to use for phpMyAdmin.

    Apart from that, the content on the message is fine and the recipient list is in order. I have set the email to go out 2 days before expiry (everyone expires on the 31st of December 2014).

    Thanks again and i will have the details over to you shortly.

    Justin

  • Jude

    Hey Justin

    I got the details you sent over and am currently working on your site. I was able to test the Unix CornTab schedule and its working like a charm. Also the mail server works and is configured correctly.

    So this is definitely something with the code itself. Can you point me to the exact location of the code which is responsible for sending out the mail ? I am trying to do a search file by file through ftp but that's taking a really long time.

    Cheers
    Jude

  • Justin

    Hi Jude

    I am not sure what file/code is responsible for sending out the mail for the membership plugin.

    I know in the Cronjobs scheduler plugin the membership_communications_process is being called but I'm not sure where the code for this is. The custom cron task code is located in plugins>cronjob scheduler> cron jobs.php, but there is no mention of code for the membership plugin communications. How would wordpress normally call these cronjobs?

    Not sure how much this helps.

    Thanks,
    Justin

  • Jude

    Hey Justin,

    Anyway here are some notes on the site I'd like to share to help you going forward

    1) There is a serious issue with the vantage theme which throws a fatal exception and does not allow you to turn on debugging. You can see what I mean if you try turning this setting on in your wp-config.php file

    define('WP_DEBUG', true);

    2) You have some custom code here which runs a cron job scheduled via a plugin. For this plugin to run the default WordPress cron must be turned off. I can see this in your wp-config.php file

    define('DISABLE_WP_CRON', true);

    What this means is that they both do not play well together. Im not certain but the other cron job toggles memberships from private to public ??

    What (1) and (2) mean is that something is not right but there is no easy way of finding out what. This is why a 15 minute task ended up taking half a day.

    Here are some action points that'll help you going forward

    1) Fix the issues with vantage, it becomes nearly impossible to test or debug anything on your site in its current form.

    2) Setup the custom corn in a non conflicting way. Use one or the other but not both.

    3) Update everything on your site, there are a bunch of updates including a core update

    4) Reduce frequency of the corn jobs, they are really server intensive and overkill the way they are setup now. We see these kind of settings in sites with around a million users concurrently using the site.

    I will post some code and my solution in the next post. Also if you need any help feel free to ask, we'd be more than happy to help.

    Cheers
    Jude

  • Jude

    Here is what I did, I created a custom plugin for your site that manually queries the database and pulls out a list of users expiring in the next 24 hours. Here is the SQL

    SELECT  ID ,  user_email ,  user_login ,display_name
    FROM  wp_m_membership_relationships
    INNER JOIN  wp_users ON wp_m_membership_relationships.user_id = wp_users.ID
    WHERE  expirydate
    BETWEEN  '2014-12-30 23:59:59'
    AND  '2014-12-31 23:59:59'

    Then I created a Communications Object setting the message ID to 2 and looped through each member sending them an email from the template.

    $expiring_users = $wpdb->get_results("query");
    
        foreach ($expiring_users as $user) {
        	$comm = new M_Communication(2);
        	$comm->send_message( $user->ID, $user->sub_id, $user->level_id);
        }

    Finally this is some glue code that warns us if there was an error with the code execution

    add_action('activated_plugin','save_error');
    function save_error(){
       update_option('plugin_error', ob_get_contents());
    }

    What this does is when you activate my plugin it triggers the job which the cron was supposed to do. Its not recommended that you use this. I built it because we were in a hurry with very little time left. It best to fix the site and automate the whole thing.

    I am attaching a screenshot and the plugin code just in case you need to look in. Hope this helps

    Cheers
    Jude

  • Justin

    Thank you Jude.

    You have been very thorough and a great help at this crucial time.

    I will take your tips on board and look at the issues with urgency. In honesty, this site has only just gone live at the start of December and it was rushed due to the members renewing at the end of the year.

    There another couple of emails scheduled to go out in a few days time. Will I need to run this manually?

    Thanks,
    Justin

  • Jude

    Hey Justin,

    The order here is all messed up :smiley: but here goes

    Is there an alternative to adding this code into the cronjob plugin? Possibly adding it into the membership plugin itself?

    The alternative would be to use wp-cron.php instead. Schedule either from a site-specific plugin or a mu-plugin

    The other alternative would be further modify the communication logic in membership and integrate with the cron plugin you are using. (not recommended)

    Excuse my ignorance I am not the best cider.

    Not at all, I think you are more than well informed to be able to follow along on this thread and get this far in a month on your site.

    I think anyone reading this will be super confused, its best that we start a new thread for the next batch of mail, if you have any issues

    Glad I could be of help

    Cheers and wish you a Happy New Year :slight_smile:
    Jude

  • Jude

    Hey Justin,

    Just did a bit of research into ways of fixing the root causes of this issue.

    Take a look at this thread

    The issue is very similar and the update was to specifically fix a similar problem. I know you cannot upgrade because of the custom work you put in but maybe your developer can cherry pick and port the changes into your current install.

    Also take a look at this post . It lets you take the cron job out of a plugin and into the OS.

    And this one, specifically the issue with scheduled posts not publishing.

    Hope it helps

    Cheers
    Jude

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.