Ask your WordPress questions! Pay money and get answers fast! (more info)

Get this function to work in WPUF Pro WordPress

  • SOLVED

I had this function that was edited to work with WPUF pro...
// auto post title by date,author name and post id
add_action( 'wpuf_add_post_after_insert', 'wpuf_alter_title' );
add_action( 'wpuf_edit_post_after_update', 'wpuf_alter_title' );

function wpuf_alter_title ( $post_id){
$post_object=get_post($post_id);
$post_date = $post_object->post_date;
$format_date = DateTime::createFromFormat( 'Y-m-d H:i:s', $post_date );
$date_formatted = $format_date->format( 'Y-m-d' ); // Set correct to display here

$post_author = $post_object->post_author;
$author_name = get_the_author_meta( 'display_name', $post_author ); // Adjust as needed

$my_post = array(
'ID' => $post_id,
'post_title' => '#' . $post_id . ' ' . $author_name . ' ' . $date_formatted // Change as needed
);
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'wpuf_alter_title');

wp_update_post( $my_post );

// re-hook this function
add_action('save_post', 'wpuf_alter_title');

}


Now I want to use this function, but it hasn't been edited to work with WPUF Pro yet...
add_action( 'save_post', 'wpse_214927_alter_title', 10, 2 );
function wpse_214927_alter_title ( $post_id, $post_object )
{
// Target only specific post type
if ( 'my_specific_post_type' !== $post_object->post_type
&& 'my_other_specific_post_type' !== $post_object->post_type
)
return;

// Remove the current action
remove_action( current_filter(), __FUNCTION__ );

$post_date = $post_object->post_date;
$format_date = DateTime::createFromFormat( 'Y-m-d H:i:s', $post_date );
$date_formatted = $format_date->format( 'Y-m-d' ); // Set correct to display here
$post_author = $post_object->post_author;
$author_name = get_the_author_meta( 'display_name', $post_author ); // Adjust as needed

$my_post = [
'ID' => $post_id,
'post_title' => '#' . $post_id . ' ' . $author_name . ' ' . $date_formatted // Change as needed
];
wp_update_post( $my_post );
}

Answers (1)

2016-04-18

Reigel Gallarde answers:

you have to add this

add_action( 'wpuf_add_post_after_insert', 'wpse_214927_alter_title', 10, 2 );
add_action( 'wpuf_edit_post_after_update', 'wpse_214927_alter_title', 10, 2 );


then on function wpse_214927_alter_title,

change

wp_update_post( $my_post );

to

// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'wpse_214927_alter_title');

wp_update_post( $my_post );

// re-hook this function
add_action('save_post', 'wpse_214927_alter_title');



remove

remove_action( current_filter(), __FUNCTION__ );


should then be like this..


add_action( 'save_post', 'wpse_214927_alter_title', 10, 2 );
add_action( 'wpuf_add_post_after_insert', 'wpse_214927_alter_title', 10, 2 );
add_action( 'wpuf_edit_post_after_update', 'wpse_214927_alter_title', 10, 2 );

function wpse_214927_alter_title ( $post_id, $post_object )
{
// Target only specific post type
if ( 'my_specific_post_type' !== $post_object->post_type
&& 'my_other_specific_post_type' !== $post_object->post_type
)
return;


$post_date = $post_object->post_date;
$format_date = DateTime::createFromFormat( 'Y-m-d H:i:s', $post_date );
$date_formatted = $format_date->format( 'Y-m-d' ); // Set correct to display here
$post_author = $post_object->post_author;
$author_name = get_the_author_meta( 'display_name', $post_author ); // Adjust as needed
$my_post = [
'ID' => $post_id,
'post_title' => '#' . $post_id . ' ' . $author_name . ' ' . $date_formatted // Change as needed
];

// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'wpse_214927_alter_title');

wp_update_post( $my_post );

// re-hook this function
add_action('save_post', 'wpse_214927_alter_title');

}


pjeaje comments:

Thanks, i'll test it now


pjeaje comments:

great thanks :)