Creé un sitio en el que los atletas pueden registrarse y crear su propia página de perfil con plantilla. Al registrarme, creo automáticamente una publicación personalizada (su perfil) y configuro al usuario como el autor de esa publicación.
Sin embargo, por mi vida no puedo establecer el título de la publicación como el nombre y apellido del usuario (ahora autor). Por ejemplo, si John Smith se registra, me gustaría que el título de la publicación diga John Smith y que el slug se convierta en atleta/john.smith .
He usado $user_info->nickname por ahora, pero esto hace que tanto el título como el slug se lea como john.smith
Este es el código que estoy usando, cualquier sugerencia sería muy apreciada:
add_action( 'user_register', 'wpse_216921_company_cpt', 10, 1 ); function wpse_216921_company_cpt( $user_id ) { // Get user info $user_info = get_userdata( $user_id ); $user_roles = $user_info->roles; // New code added $this_user_role = implode(', ', $user_roles ); if ($this_user_role == 'author') { // Create a new post $user_post = array( 'post_title' => $user_info->nickname, 'post_status' => 'publish', // <- here is to publish 'post_type' => 'athlete', // <- change to your cpt 'post_author' => $user_info->ID ); // Insert the post into the database $post_id = wp_insert_post( $user_post ); } }También debe usar post_name para slug (john.smith) y first_name, last name para el título de la publicación como este:
add_action( 'user_register', 'wpse_216921_company_cpt', 20, 1 ); function wpse_216921_company_cpt( $user_id ) { // Get user info $user_info = get_userdata( $user_id ); $user_roles = $user_info->roles; // New code added $this_user_role = implode(', ', $user_roles ); if ($this_user_role == 'author') { $post_title = $user_info->first_name.' '.$user_info->last_name; $post_title = trim(ucwords($post_title)); $post_slug = preg_replace('/\s+/', '.', $post_title); // Create a new post $user_post = array( 'post_title' => $post_title, //$user_info->display_name, 'post_name' => $post_slug, 'post_status' => 'publish', // <- here is to publish 'post_type' => 'athlete', // <- change to your cpt 'post_author' => $user_info->ID ); // Insert the post into the database $post_id = wp_insert_post( $user_post ); } }Puede usar el nombre y el apellido como se muestra a continuación:
add_action( 'user_register', 'wpse_216921_company_cpt', 10, 1 ); function wpse_216921_company_cpt( $user_id ) { // Get user info $user_info = get_userdata( $user_id ); $user_roles = $user_info->roles; // New code added $this_user_role = implode(', ', $user_roles ); $first_name = $user_info->first_name; $last_name = $user_info->last_name; if ($this_user_role == 'author') { // Create a new post $user_post = array( 'post_title' => $first_name . ' ' . $last_name, 'post_status' => 'publish', // <- here is to publish 'post_type' => 'athlete', // <- change to your cpt 'post_author' => $user_info->ID ); // Insert the post into the database $post_id = wp_insert_post( $user_post ); } }No probado, pero debería funcionar.
Gracias por la ayuda chicos. Resultó ser un problema de Ultimate Member y necesitaba usar el Hook de UM um_registration_complete.
La solución final que funcionó es la siguiente:
//Create Custom Post Type on registration add_action( 'um_registration_complete', 'wpse_216921_company_cpt', 10, 1 ); function wpse_216921_company_cpt( $user_id ) { // Get user info $user_info = get_userdata( $user_id ); $user_roles = $user_info->roles; // New code added $this_user_role = implode(', ', $user_roles ); if ($this_user_role == 'author') { $post_title = $user_info->first_name.' '.$user_info->last_name; $post_title = trim(ucwords($post_title)); $post_slug = preg_replace('/\s+/', '.', $post_title); // Create a new post $user_post = array( 'post_title' => $post_title, //$user_info->display_name, 'post_name' => $post_slug, 'post_status' => 'publish', // <- here is to publish 'post_type' => 'athlete', // <- change to your cpt 'post_author' => $user_info->ID ); // Insert the post into the database $post_id = wp_insert_post( $user_post ); } }