Change frequency of digest received from Subscribe by Email?

Where is the code located in the plugin that specifies the weekly digest option as 7 days? I'd like the option of sending the digest every two weeks instead of every week. Ideally, there would be an option to designate number of days, in addition to the current options -- but for now I can live with hacking the plugin or adding some code to my theme's function.php file. Can you help me out with either identifying the file and location where I can change the 7 days to 14 days, or in writing the code I could use in the functions.php file to make the change? Thanks!

  • Adam Czajczyk

    Hello xbladerunner,

    I hope you're well today and thank you for your question!

    I agree that more option to choose from would be handy so I have moved this post to our "Features and Feedback" forum where other members of WPMU DEV community may give it a vote. With more support from members it's possible that our developers will consider adding more "frequency features" to the plugin settings in future. I'm not able to guarantee that though.

    As for a "quick hack". I reviewed plugin's code briefly but wasn't able to come up with a solution myself so I asked one of the developers to take a look at it and I'll let you know once I get a response from him. That said, please keep an eye on this thread and I'll keep you informed.

    Best regards,
    Adam

  • Adam Czajczyk

    Hello xbladerunner!

    I'm sorry I kept you waiting that long. I got a "hot patch" from the developer that should work just as you need.

    Here's the code:

    <?php
    add_action( 'pre_update_option_next_week_scheduled', function( $value ) {
        // Change the weekly setting to be sent twice in a week
    
        // Example: We want to send on Mon and Thu
        $send_on = array( 'monday', 'thursday' );
    
        // Let's check which one is closer and compare:
        $next_times = array();
        foreach ( $send_on as $day_of_week ) {
            $next_times[] = strtotime( 'next ' . $day_of_week, current_time( 'timestamp' ) );
        }
    
        // We should keep the min value as is the closest one
        $digest_should_be_send_on = min( $next_times );
    
        // At what time the digest should be sent (set in settings page when
        // weekly option is selected)
        $settings = incsub_sbe_get_settings();
        $time = $settings['time'];
        $digest_should_be_send_on = date( 'Y-m-d', $digest_should_be_send_on ) . ' ' . $time . ':00:00';
        return strtotime( $digest_should_be_send_on );
    });

    In this line you set on which days the digest should go out:

    $send_on = array( 'monday', 'thursday' );

    In order to make use of that patch create an empty file with a .php extension, paste the code inside and upload the file to the "/wp-content/mu-plugins" folder of your site. If there's no "mu-plugins" folder inside the "wp-content" folder, simply create it.

    Once the code is added, make sure that you set the "weekly" option in plugin settings and saved it.

    Best regards,
    Adam

  • xbladerunner

    Thanks for your help with this, Adam. Unfortunately, I think you may have misread my request. What I am looking for is the ability to send the digest every 2 weeks, not twice a week... I have a situation where content isn't posted every week, but rather at least once every two weeks.

    I presumed that subscribing to the digest presently means that every other week the subscribers are getting an empty digest -- unless there is a piece of code included that prevents the digest from being sent if no new posts exist (I didn't think of that possibility, so asked for a way to just send the digest every two weeks). Do you know if the digest is sent out when there are no new posts? If the digest is sent with no posts, is there a hack to prevent that (rather than changing the frequency, per se)?

  • Adam Czajczyk

    Hello xbladerunner!

    I'm terribly sorry for that. I just passed the link to your post to the developer and since it was already some time ago I did not check the code I got against your question again. I apologize for that.

    I have already asked the developer for update and I double-checked the way I described the issue to him. I hope though that since we got this code it should be relatively easy to tweak it to work on a "two weeks" basis instead of "every week".

    I'll update this thread with a new code as soon as possible.

    Best regards,
    Adam

  • Adam Czajczyk

    Hello xbladerunner!

    I just got another piece of code that should work for you this time:

    add_action( 'pre_update_option_next_week_scheduled', function( $value ) {
        $settings = incsub_sbe_get_settings();
        $day = false;
        switch ( $settings['day_of_week'] ) {
            case 0:
                $day = 'sunday';
                break;
            case 1:
                $day = 'monday';
                break;
            case 2:
                $day = 'tuesday';
                break;
            case 3:
                $day = 'wednesday';
                break;
            case 4:
                $day = 'thursday';
                break;
            case 5:
                $day = 'friday';
                break;
            case 6:
                $day = 'saturday';
                break;
        }
    
        if ( ! $day ) {
            return false;
        }
    
        // At what time the digest should be sent (set in settings page when
        // weekly option is selected)
        $next_time = strtotime( 'next ' . $day, current_time( 'timestamp' ) + 604800 ); // Current time + 1 week
        $next_time_date = date( 'Y-m-d', $next_time ) . ' ' . str_pad( $settings['time'], 2, 0, STR_PAD_LEFT ) . ':00:00';
        $next_time_timestamp = strtotime( $next_time_date );
        return $next_time_timestamp;
    });

    Just like previously, you will want to select "weekly" schedule and select days and time the digest should go out and it should then be send on "bi-weekly" base (every two weeks) on a selected date.

    Let me know if that works for you please.

    Best regards,
    Adam

  • xbladerunner

    Thanks Adam!

    I presume it will take a couple weeks to test :slight_smile:
    I'll post back here and close the ticket if it looks like it is working as needed.

    Quick question: within a multisite environment, do you know if it will interfere with an existing weekly email digest on other subsites? If it is expected to interfere, are there any changes that can be made to allow for standard operation of the other subsite digests?

  • Adam Czajczyk

    Hello xbladerunner!

    The code applies a custom function to the scheduling option filter and it doesn't know what site you are currently editing. It would be applied to all schedules the same way. Therefore you may wan't to try this slightly changed code:

    $selected_blog = 1;
    $the_blog = get_current_blog_id(); // get an ID of current blog
    
    if ($the_blog == $selected_blog) {
    
    add_action( 'pre_update_option_next_week_scheduled', function( $value ) {
    
    	$settings = incsub_sbe_get_settings();
        $day = false;
        switch ( $settings['day_of_week'] ) {
            case 0:
                $day = 'sunday';
                break;
            case 1:
                $day = 'monday';
                break;
            case 2:
                $day = 'tuesday';
                break;
            case 3:
                $day = 'wednesday';
                break;
            case 4:
                $day = 'thursday';
                break;
            case 5:
                $day = 'friday';
                break;
            case 6:
                $day = 'saturday';
                break;
        }
    
        if ( ! $day ) {
            return false;
        }
    
        // At what time the digest should be sent (set in settings page when
        // weekly option is selected)
        $next_time = strtotime( 'next ' . $day, current_time( 'timestamp' ) + 604800 ); // Current time + 1 week
        $next_time_date = date( 'Y-m-d', $next_time ) . ' ' . str_pad( $settings['time'], 2, 0, STR_PAD_LEFT ) . ':00:00';
        $next_time_timestamp = strtotime( $next_time_date );
        return $next_time_timestamp;
    
    });
    
    }

    It's basically the same code but wrapped up in an "if" conditional statement so it's executed only if an ID of current blog equals a specified ID. It wouldn't be triggered on any other blog so other blogs' behavior should stay intact.

    To specify the blog, find it's ID (by looking at "Sites -> All sites" page and hovering over blog name; URL will include ID) and change the value (currently "1") of the very first line of the code.

    Best regards,
    Adam