public
Created

Automatically assign a co-author based on current user

  • Download Gist
gistfile1.aw
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?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 );
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.