Skip to content

Instantly share code, notes, and snippets.

@danielbachhuber
Created June 1, 2012 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielbachhuber/2847390 to your computer and use it in GitHub Desktop.
Save danielbachhuber/2847390 to your computer and use it in GitHub Desktop.
Automatically assign a co-author based on current user
<?php
/**
* Automatically assign a co-author based on current user's usermeta value
* This code is untested and for demonstration purposes
*
* @see http://wordpress.org/support/topic/plugin-co-authors-plus-dynamically-assign-co-authors?replies=1
*/
add_action( 'save_post', 'capx_auto_assign_author' );
function capx_auto_assign_author( $post_id ) {
global $coauthors_plus;
if ( !is_object( $coauthors_plus ) )
return;
// Get the current user, see if they have a parent_user,
// and automatically assign the parent user if it exists
$current_user = wp_get_current_user();
if ( $parent_user_login = get_user_meta( $current_user->ID, 'parent_user', true ) )
$coauthors_plus->add_coauthors( $post_id, array( $parent_user_login ), true );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment