WordPress.org

Ready to get started?Download WordPress

Forums

Events Manager
[resolved] Addition of hook to modify post status before wp_insert_post ? (7 posts)

  1. leoloso
    Member
    Posted 1 year ago #

    Hi Marcus,

    I have a requirement for my website, for which I had to modify the EM code. I'll explain, if you agree you can add it to the official code.

    I'm adding the feature to allow registered users post Events. I want them to not be able to publish straight when creating the Event, but once it is Published by the editors, then the users can update the Event directly (so the status is 'pending' only when creating, but for updating it is 'published')

    This is not supported right now. My strategy to cope with this is:
    - Grant capability 'publish_events' to my users
    - When creating an Event in frontend, change the post_status to 'pending' through a hook

    For this: I changed in em-event.php, where it says:
    if( count($this->errors) == 0 ){
    $post_array['post_status'] = ( $this->can_manage('publish_events','publish_events') ) ? 'publish':'pending';
    }else{
    $post_array['post_status'] = 'draft';
    }
    ...
    $post_id = wp_insert_post($post_array);

    I have added a hook before wp_insert_post:

    ...
    $post_array = apply_filters('em_event_save_post_array', $post_array, $this);
    $post_id = wp_insert_post($post_array);

    Then, in the hook, I can check if it is a new post or an update, and then change the status:

    add_filter('em_event_save_post_array', function($post_array, $event) {

    // If it is an update, exit
    if (!empty($event->post_id)) return $post_array;

    // If there were errors, exit
    if( count($event->errors) > 0 ) return $post_array;

    // Check if it was created in frontend
    if (!created_in_frontend()) return $post_array;

    // Created in Frontend => status always pending
    $post_array['post_status'] = 'pending';

    return $post_array;
    }, 10, 2);

    As you can see, I have a function created_in_frontend() that allows me to do this only when creating an Event in the frontend (I haven't seen any property like this, so I'm just adding a hidden input in the Event form)

    I want to mention another issue: if the user does not have capability 'publish_events', then they also need an approval from the editors to have the Event updated. However, this process does not behave like a normal post in WP, because:
    - In WP Backend, when editing that Event that needs approval, the button does not say 'Update' but 'Publish'
    - After submitting the update, the page goes offline, in such a way that it will not be accessible anymore in the website until the editors approve the update (my expectation is that the page is still online without the new update)

    Thanks,
    Leo

    http://wordpress.org/extend/plugins/events-manager/

  2. leoloso
    Member
    Posted 1 year ago #

    I wonder if it makes sense, otherwise, to add capability update_event, so it is possible to grant update_event but not publish_event to frontend users

  3. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 1 year ago #

    coincidentally, I've just added something locally that may fix the problem for you without needing a hook:

    if( empty($this->force_status) ){
    			if( count($this->errors) == 0 ){
    				$post_array['post_status'] = ( $this->can_manage('publish_events','publish_events') ) ? 'publish':'pending';
    			}else{
    				$post_array['post_status'] = 'draft';
    			}
    		}else{
    		    $post_array['post_status'] = $this->force_status;
    		}

    now, you can hook into em_event_save_pre and modify $EM_Event->force_status instead. Will that do?

    Not that I mind adding more hooks, but I think in any event you could probably hook into somethign form WP instead like wp_insert_post_data, which is run just before saving within wp_insert_post

  4. leoloso
    Member
    Posted 1 year ago #

    yes, that will do :)

    As a sidenote, with my solution I'm creating a bug: When creating an event, it is not published immediately, however that user has publish_event capabilities, so if they create an Event and duplicate it, that duplication is published immediately. I think with your solution of force_status I can fix this bug, since now I will not assign publish_event capabilities to the user, but force status to Publish for Updates...

    When will this be released? Do you want me to download the dev version and test it?

    Thanks!

  5. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 1 year ago #

    actually, this is more or less why I'm creating this new line :)

    next update/dev (next dev should be out by tomorrow) will not publish duplicates immediately, they'll be drafts see this

    http://wordpress.org/support/topic/issue-between-events-manager-wp-to-twitter?replies=13#post-3973496

    regarding the publication issue, I don't get this... if the user can publish events, it gets published when they submit an event

  6. leoloso
    Member
    Posted 1 year ago #

    yes yes they'll get published immediately, I'm talking about the solution I implemented in my website to allow immediate update but not immediate publishing: they have publish_event capability, but I intercept it if it's created in the frontend and assign status "pending"

    But after you implement the force_status, I'll use this as it's a much cleaner solution :)

  7. Marcus
    NetWebLogic Support
    Plugin Author

    Posted 1 year ago #

    aha i see now, thx for clearing that up!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.