• I’m trying to create a custom scope to show the events from THIS WEEK in a widget. This Week is not currently an option.

    I’ve tried the tutorial at:
    https://wp-events-plugin.com/tutorials/create-your-own-event-scope/
    but I couldn’t seem to get it to work.

    Here is the code I altered from that page and put into the functions.php file of my custom theme:

    add_filter( 'em_events_build_sql_conditions', 'my_em_scope_conditions',1,2);
    function my_em_scope_conditions($conditions, $args){
        if( !empty($args['scope']) && $args['scope']=='this-week' ){
            $start_date = date('Y-m-d',current_time('timestamp'));
            $end_date = date('Y-m-d',strtotime("+6 day", current_time('timestamp')));
            $conditions['scope'] = " (event_start_date BETWEEN CAST('$start_date' AS DATE) AND CAST('$end_date' AS DATE)) OR (event_end_date BETWEEN CAST('$end_date' AS DATE) AND CAST('$start_date' AS DATE))";
        }
        return $conditions;
    }
    
    add_filter( 'em_get_scopes','my_em_scopes',1,1);
    function my_em_scopes($scopes){
        $my_scopes = array(
            'this-week' => 'This Week'
        );
        return $scopes + $my_scopes;
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • what doesn’t work, does the scope actually show up as an option?

    Thread Starter clickxclick

    (@clickxclick)

    Correct.

    This Week is not showing up as a scope drop down option for the widget.

    Here’s what my sidebar scope drop down looks like:
    https://lostsoundtapes.com/sidebar.jpg

    you must not be including this code in the right place. this code looks ok to me

    if you just provide a blank array of scopes, are there no scopes to choose from?

    Thread Starter clickxclick

    (@clickxclick)

    Super stupid mistake on my part. I put it in the wrong functions.php file because I was working on sites for two clients at the same time.

    Thank you very much for verifying my code!

    Is there a way to sort the new scope into the position of the list that I would like? (Between “Tomorrow’s events” and “Events this month”) If not, that’s totally fine.

    if you rearrange the $scopes array yourself, yes.

    Thread Starter clickxclick

    (@clickxclick)

    For anyone who might read this later and wonder abour my last question:

    I used array_splice() to insert a new value to the $scopes array at a specified index.

    add_filter( 'em_get_scopes','my_em_scopes',1,1);
    function my_em_scopes($scopes){
        $my_scopes = array(
            'this-week' => 'This Week'
        );
        array_splice($scopes, 5, 0, $my_scopes);
        return $scopes;
    }

    It adds the array $my_scopes to the $scopes array at position 5 with a length of 0.

    Thanks again for your help Marcus and for writing such a great Event Manager. I have definitely been spreading the word.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘[Plugin: Events Manager] Creating custom scope for Events widgets’ is closed to new replies.