Forked from chaance/wp-team-post-save-link-user.php
Last active
April 2, 2018 16:07
-
-
Save philipdowner/4b8ee401bd75f643cd469697d8134fce to your computer and use it in GitHub Desktop.
Function to dynamically sync CPT team posts with WP users. The post type requires custom meta fields for linked_author (user ID) and email.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Create new users to match team members. | |
* | |
* @param integer $post_id Post ID. | |
*/ | |
function xx_team_post_save_link_user( $post_id ) { | |
$user_email = get_post_meta( $post_id, 'email', true ); | |
$author_id = (int)get_post_meta( $post_id, 'linked_author', true ); | |
if( $user_email && !is_email($user_email) ) { | |
$user_email = ''; | |
} | |
//No email or author ID, can't proceed | |
if( !$user_email && !$author_id ) { | |
return; | |
} | |
// Format first and last names. | |
$post_title = get_the_title( $post_id ); | |
$name_parts = explode( ' ', $post_title, 2 ); | |
$first_name = $name_parts[0]; | |
$last_name = $name_parts[1]; | |
//We have both an author ID and an email; ensure fields match | |
if( $user_email && $author_id ) { | |
$user = get_user_by('ID', $author_id); | |
$updated_user = []; | |
if ( $post_title !== $user->display_name ) { | |
$updated_user['display_name'] = $post_title; | |
} | |
if ( $user_email !== $user->user_email ) { | |
$updated_user['user_email'] = $user_email; | |
} | |
if( $user->first_name !== $first_name ) { | |
$updated_user['first_name'] = $first_name; | |
} | |
if( $user->last_name !== $last_name ) { | |
$updated_user['last_name'] = $last_name; | |
} | |
if( !empty($updated_user) ) { | |
$updated_user['ID'] = $author_id; | |
$author_id = wp_update_user($updated_user); | |
} | |
return; | |
} | |
//We have an author ID, but no email; update the post | |
if( $author_id ) { | |
$user = get_user_by('ID', $author_id); | |
$user_email = $user->user_email; | |
update_post_meta($post_id, 'email', $user_email); | |
} | |
//We have no author ID but do have an email; create the user | |
if( !$author_id && is_email($user_email) && !email_exists($user_email) ) { | |
$username = $first_name && $last_name ? "$first_name.$last_name" : get_the_title( $post_id ); | |
$author_id = wp_insert_user( [ | |
'user_login' => subst(strtolower( $username ), 0, 60), | |
'user_pass' => wp_generate_password(), | |
'user_email' => $user_email, | |
'display_name' => get_the_title( $post_id ), | |
'first_name' => $first_name, | |
'last_name' => $last_name, | |
'role' => 'author', | |
] ); | |
if( is_wp_error($author_id) ) { | |
return; | |
} | |
// Link the post with the new author. | |
update_post_meta( $post_id, 'linked_author', $author_id ); | |
return; | |
} | |
} | |
add_action( 'save_post_team', 'xx_team_post_save_link_user' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment