Skip to content

Instantly share code, notes, and snippets.

@svenl77
Last active March 2, 2020 13:17
Show Gist options
  • Save svenl77/6344e902dcad0d25a83f18ef5efa63bb to your computer and use it in GitHub Desktop.
Save svenl77/6344e902dcad0d25a83f18ef5efa63bb to your computer and use it in GitHub Desktop.
BuddyForms filter buddyforms_the_author_id to overwrite the author of a post during submission
<?php
//
// Add the auto_assign_user id as post author if auto_assign_user is set for this form
//
add_filter( 'buddyforms_the_author_id', 'buddyforms_auto_assign_user',10 , 2 );
function buddyforms_auto_assign_user($author_id, $form_slug){
// Check if anonymous author is set
if( ! isset( $_POST['auto_assign_user'] ) ){
return $author_id;
}
// Check if this form does have an auto_assign_user hidden form element and return the auto_assign_user id from the form element.
// We not want to make it possible to manipulate this data and not grab it from the form directly.
$auto_assign_user = buddyforms_get_form_field_by_slug($form_slug, 'auto_assign_user');
if( isset( $auto_assign_user['value'] ) ){
return $auto_assign_user['value'];
}
return $author_id;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment