Skip Navigation

[Resuelto] The supplied code fails, Field «Title» is required..

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:

Create dynamic post title from custom fields.

Solution:

An example of how to achieve this can be seen below.

function gg_fill_full_name( $post_id, $form_data ){
	if ( $form_data['id'] == 126) {
		$firstname = get_post_meta( $post_id, 'wpcf-first-name', true );
		$middlename = get_post_meta( $post_id, 'wpcf-middle-name', true );
		$lastname = get_post_meta( $post_id, 'wpcf-last-name', true );
		$suffix = get_post_meta( $post_id, 'wpcf-name-suffix', true );
 	    
		if ($suffix == "None") {
			$suffix = '';
		}              
 
        $result = $lastname . $suffix . ", " . $firstname . " " . $middlename;
		
		
		
//		$result = $lastname . " " . $suffix . ", " . $firstname . " " . $middlename;

		if ( !empty($result) ) {
   			$my_post = array(
  				'ID' => $post_id,
   				'post_title' => $result,
			);

		// Update the post into the database
		wp_update_post( $my_post, $wp_error );
		}
	}
}
 
add_action('cred_submit_complete', 'gg_fill_full_name',10,2);
This support ticket is created hace 6 años, 2 meses. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Este tema contiene 24 respuestas, tiene 4 mensajes.

Última actualización por Gerard hace 6 años, 2 meses.

Asistido por: Shane.

Autor
Mensajes
#1113679

Hi,

Sorry I missed the auto resolve on post https://toolset.com/forums/topic/trying-to-populate-the-wp-title-field-with-a-few-other-fields/

I'm trying it now, and it's failing, saying that the field Title is required, not letting me save..

#1113680

I get this: enlace oculto

Is it possible to make Title "NOT required"?

#1113914

Hi, can you delete the title field from this Form? Edit the form and remove the input field. Then the error will not be displayed on the front-end of the site, and the title will be set automatically.

#1113916

Yes!!! getting closer.. Thanks

I see the logic I think, it was blocked since the field was on the form AND "required"?

So, the Title field now gets filled with only the comma, but the field values did not copy over..

#1113923

I see the logic I think, it was blocked since the field was on the form AND "required"?
Yes, that's right. If you remove the title field from the Form, the error will not be triggered.

So, the Title field now gets filled with only the comma, but the field values did not copy over..
For reference, from the other ticket:

function gg_fill_full_name( $post_id, $post ){
 
global $wpdb;
     
    if ( $post_id == null || empty($_POST) )
        return;
 
    if ( wp_is_post_revision( $post_id ) )
        $post_id = wp_is_post_revision( $post_id );
 
    if ( empty( $post ) )
        $post = get_post($post_id);
 
     
    if ( 'application' == $post->post_type ) {
         
                $firstname = get_post_meta( $post_id, 'wpcf-first-name', true );
                               $middlename = get_post_meta( $post_id, 'wpcf-middle-name', true );
                               $lastname = get_post_meta( $post_id, 'wpcf-last-name', true );
                               $suffix = get_post_meta( $post_id, 'wpcf-name-suffix', true );
 
            $result = $lastname . " " . $suffix . ", " . $firstname . " " . $middlename;
 
            if ( !empty($result) ) {
                $where = array( 'ID' => $post_id );
                $wpdb->update( $wpdb->posts, array( 'post_title' => $result ), $where );
            }
        }
 
 
}
 
add_action( 'save_post', 'gg_fill_full_name', 30, 2 );

One thing I see is here:

            $result = $lastname . " " . $suffix . ", " . $firstname . " " . $middlename;
            if ( !empty($result) ) {

The $result variable will never be empty, because there are spaces and a comma in the concatenated string. I'm not sure why the !empty check is there, but this could be the problem.

Then you could also try bumping up the priority to 100:

add_action( 'save_post', 'gg_fill_full_name', 100, 2 );

If neither of these solves the problem, I'll need to take a closer look.

#1113924

I bumped the priority to 100, and removed the "IF !empty" check, same result, only the "," made it..

if I edit directly from the WP backend, then it works, even with prior settings.

#1113951
photo2.jpg

Systems Test, please ignore this reply.

#1114352

O.K.

#1114399

follow up:

Hoping it was a priority issue, I've now tested with 10, with 100, with 999, and with 1500. no difference.

#1114500

Hi Shane,

You can find site access in this other ticket: https://toolset.com/forums/topic/can-post-forms-be-used-by-guests-to-create-posts/

#1114501

Shane
Supporter

Idiomas: Inglés (English )

Zona horaria: America/Jamaica (GMT-05:00)

Hi Gerard,

As christian is currently on vacation, I will be handling this issue.
Could you let me know if this is for a Toolset form on the frontend ?

Thanks,
Shane

#1114505
#1114514

Shane
Supporter

Idiomas: Inglés (English )

Zona horaria: America/Jamaica (GMT-05:00)

Hi Gerard,

Could you change this line.

add_action( 'save_post', 'gg_fill_full_name', 30, 2 );

To

    add_action('cred_save_data', 'gg_fill_full_name',10,2);

Since it is a CRED form you should use this instead.

Thanks,
Shane

#1114523

Hi, I tried it with both 10, and 100, and the result is that instead of filling the Title field with the other field values, it writes:

CRED Auto Draft 3519dcec72882683e20445031bfdd404

#1114529

Shane
Supporter

Idiomas: Inglés (English )

Zona horaria: America/Jamaica (GMT-05:00)

Hi Gerard,

I thinks a rewritten solution would be needed for you.

As you had the code in the original solution please try it again.

function gg_fill_full_name( $post_id, $post ){
if ( 'application' == $post->post_type ) {
$firstname = get_post_meta( $post_id, 'wpcf-first-name', true );
$middlename = get_post_meta( $post_id, 'wpcf-middle-name', true );
$lastname = get_post_meta( $post_id, 'wpcf-last-name', true );
$suffix = get_post_meta( $post_id, 'wpcf-name-suffix', true );

$result = $lastname . " " . $suffix . ", " . $firstname . " " . $middlename;
if ( !empty($result) ) {
   $my_post = array(
  'ID' => $post_id,
   'post_title' => $result,
);
// Update the post into the database
wp_update_post( $my_post, $wp_error );
}
}
}

add_action('cred_save_data', 'gg_fill_full_name',10,2);

As you mentioned with minesh you were getting an error 500. Could you enable the wordpress debugging and then let me know the error ?

Thanks,
Shane