Created
October 26, 2015 14:11
-
-
Save JayWood/a902d60525e22b6f5add to your computer and use it in GitHub Desktop.
create a page for a user on login
This file contains 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 | |
/** | |
* Creates a page for a user if it doesn't exist when they login | |
* @param string $user_login The login the user used | |
* @param WP_User $user WP_User object | |
*/ | |
function create_my_page( $user_login, $user ) { | |
$has_shop = get_user_meta( $user->ID, 'has_shop', true ); | |
$page_exists = get_page_by_title( $user->tds_shop_name, OBJECT, 'business' ); | |
if ( $page_exists || $has_shop ) { | |
// The page already exists by that title, so we stop here. | |
return; | |
} | |
$post_args = array( | |
'post_title' => $user->tds_shop_name, | |
'post_author' => $user->ID, | |
'post_type' => 'business', | |
'post_content' => '', | |
'post_status' => 'private', | |
); | |
$inserted_post = wp_insert_post( $post_args, true ); | |
if ( ! is_wp_error( $inserted_post ) ) { | |
// Update the user meta with the shop page ID | |
update_user_meta( $user->ID, 'has_shop', $inserted_post ); | |
} | |
} | |
add_action( 'wp_login', 'create_my_page', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment