Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function sd_set_title_slug_from_headline( $post_id ) {
- // bail if no ACF data
- if ( empty( $_POST[ 'acf' ] ) ) {
- // return;
- }
- $ad_title = 'field_57e3ed6c92ea0';
- // if sd_ad_title is entered
- if ( ! empty( $_POST[ 'acf' ][ $ad_title ] ) ) {
- $entered_title = $_POST[ 'acf' ][ $ad_title ];
- $cleaned_title = preg_replace( '/[^A-Za-z0-9\-\s]/', '', $entered_title ); // Removes special chars.
- $post_name = sanitize_title( $cleaned_title );
- $city_name = wp_specialchars_decode( get_field( 'sd_city_search_value', $post_id ), ENT_QUOTES );
- $search_city = str_replace( '\'', '', $city_name ); // strip '
- $search_city = str_replace( ' ', '-', $search_city ); // change space
- $new_slug = strtolower( $search_city ) . '-' . $post_name . '-' . $post_id;
- // update acf field
- update_field( 'sd_ad_title', $cleaned_title, $post_id ); // update acf field
- // update post status + post title (if needed)
- global $wpdb;
- $wpdb->update(
- $wpdb->posts,
- array(
- 'post_title' => $cleaned_title,
- 'post_name' => $new_slug
- ),
- array(
- 'ID' => $post_id
- )
- );
- clean_post_cache( $post_id );
- }
- }
- add_action( 'acf/save_post', 'sd_set_title_slug_from_headline', 20 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement