Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

260
Views
¿Cómo muestro el título de la publicación personalizada como "first_name last_name"?

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 ); } }
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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 ); } }
over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report

0

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 ); } }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!