• Resolved daymobrew

    (@daymobrew)


    I have a standalone script (WP_USE_THEMES = false) that adds an event by using EM_Event->save(). Unfortunately, although the event is saved and is in the back end and I can click ‘View’, it does not appear in the location page. If I Quick Edit and Update (no changes) it does appear.

    When I look at the database the ‘event_slug’ column in ${prefix}_em_events table is NULL until I do the Quick Edit/Update.

    My code does ‘wp_update_post()’ because the anonymous user is used to create the event because I am not logged in. That user cannot publish events.

    Should I call EM_Event->save() again to set the slug again? I see that the EM code checks for an existing event and does some different stuff..

    <?php
    define('WP_USE_THEMES', false);
    require( dirname( __FILE__ ) . '/wp-blog-header.php' );
    
    $minute = date('i');  // Change the minute.
    $event_time = '18:'.$minute.':15';
    
    $event_title = 'EM Test';
    $event_end_date = '2017-02-10';
    
                            
    $EM_Event = new EM_Event();
    $EM_Event->event_name = 'EM Test';
    $EM_Event->event_slug = sanitize_title('EM Test 2017-02-10');
    $EM_Event->post_content = "<p>This is the description of EM Test event on $event_date at $event_time.</p>";
    $EM_Event->event_owner = 6;
    $EM_Event->location_id = 30;
                            
    $EM_Event->event_start_date = '2017-02-10';
    $EM_Event->event_end_date = '2017-02-10';
    $EM_Event->event_start_time = '18:10:15';
    $EM_Event->event_end_time = '18:10:15';
    $EM_Event->start = strtotime($EM_Event->event_start_date.' '.$EM_Event->event_start_time);
    $EM_Event->end = strtotime($EM_Event->event_end_date.' '.$EM_Event->event_end_time);
    
    // Disable saving during development.
    $save_return = $EM_Event->save();
    
    echo '<p>save() result: ', var_export($save_return, true), '</p>';
    echo '<p>Event post ID: ', $EM_Event->post_id, '</p>';
    echo '<p>Event event ID: ', $EM_Event->event_id, '</p>';
    echo '<p>Minute: ', $minute, '</p>';
    
    $args = array(
        'ID' => $EM_Event->post_id,
        'post_author' => 6,
        'post_name' => sanitize_title('EM Test 2017-02-10'),
        'post_status' => 'publish',
    );
    wp_update_post( $args );
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support angelo_nwl

    (@angelo_nwl)

    maybe you can try to hook into em_event_save filter instead.

    Thread Starter daymobrew

    (@daymobrew)

    I don’t think that I can do that. My script runs outside of WordPress – I invoke the script directly so if I used add_filter() where would the triggering code be?

    Or are you suggesting that I add_filter() in addition to the above code and use $this to set the $event_slug? That filter seems to be run after all the database updates are complete.

    I am curious why $event_slug is not set during the initial EM_Event->save(). Is it because the event owner is the anonymous user that I have set to not be able to publish events?

    That sounds a likely explanation.

    Thread Starter daymobrew

    (@daymobrew)

    Ok, so it’s workaround time.

    I have it working as desired when I temporarily change the “Guest Default User” (under Anonymous Event Submissions) to a user that has ‘publish_events’ permissions. After calling EM_Event->save() I change back to the use that does not have ‘publish_events’ permissions.

    Here is an excerpt of the modified code:

    // Change anonymous submission user to allow the event be published.
    $anon_user = get_option('dbem_events_anonymous_user');
    update_option('dbem_events_anonymous_user', 1);  // Set to a user that can publish events.
    
    $save_return = $EM_Event->save();
    
    // Set back to originally set anon user.
    update_option('dbem_events_anonymous_user', $anon_user);
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Using EM_Event->save() does not publish event’ is closed to new replies.