Support

Account

Home Forums General Issues Subheadline Text Delayed when Publishing?

Solving

Subheadline Text Delayed when Publishing?

  • Hey @elliot,

    We’ve been working on a function that combines the default WordPress title with a custom sub heading in the post/page’s permalink URL. For example:

    Post Title (h1): Title
    ACF Custom Text Field (h2): Sub heading
    URL: site.com/title-sub-heading

    This feature works perfectly when using the standard WordPress custom fields and using the following code in our functions.php file:

    function custom_post_title($data , $postarr) {
    	if ( !in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
    		$custom_field_title = get_post_meta($postarr['post_ID'], 'heading2', true);
    		if(isset($custom_field_title) && $custom_field_title != '')
    			$data['post_name'] = sanitize_title($postarr['post_title'].'-'.$custom_field_title);
        }
        return $data;
    }
    add_action ('wp_insert_post_data','custom_post_title', 99, 2 );

    However, when using a ACF text field to add the sub heading instead of the default WordPress custom field, this function doesn’t work after publishing the page. I need to click the Update (Publish) button a second time in order for it to update correctly.

    I thought this might be our code, but without question it only requires the double page update when using the ACF field.

    Is there anything you can suggestion that might solve this issue we’re having with ACF’s text field that seems to be delayed?

  • Hi @nitruc

    Quite simply, the ‘wp_insert_post_data’ action is run BEFORE ACF save’s its data. This is why you get a ‘page load delay’.

    Your best bet is to use the acf/update_value action to update the post title.
    You can read about this action on the ACF docs page + there are already a few topics regarding updating a post’s title via an ACF field!

    Thanks
    E

  • @elliot

    Looking in the docs I found to use this:

    <?php
     
    function my_acf_update_value( $value, $post_id, $field  )
    {
        $value = "Custom value";
     
        // do something else to the $post object via the $post_id
     
        return $value;
    }
     
    // acf/update_value - filter for every field
    add_filter('acf/update_value', 'my_acf_load_field', 10, 3);
     
    // acf/update_value/type={$field_type} - filter for a specific field based on it's type
    add_filter('acf/update_value/type=select', 'my_acf_load_field', 10, 3);
     
    // acf/update_value/name={$field_name} - filter for a specific field based on it's name
    add_filter('acf/update_value/name=my_select', 'my_acf_load_field', 10, 3);
     
    // acf/update_value/key={$field_key} - filter for a specific field based on it's name
    add_filter('acf/update_value/key=field_508a263b40457', 'my_acf_load_field', 10, 3);
     
    ?>

    However, adding just that to my functions.php causes a bunch of errors when trying to publish a post:

    Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'heading2' not found or invalid function name in /home/movies9/public_html/msv6/wp-includes/plugin.php on line 199
    
    Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'heading2' not found or invalid function name in /home/movies9/public_html/msv6/wp-includes/plugin.php on line 199
    
    Warning: Cannot modify header information - headers already sent by (output started at /home/movies9/public_html/msv6/wp-includes/plugin.php:199) in /home/movies9/public_html/msv6/wp-admin/post.php on line 222
    
    Warning: Cannot modify header information - headers already sent by (output started at /home/movies9/public_html/msv6/wp-includes/plugin.php:199) in /home/movies9/public_html/msv6/wp-includes/pluggable.php on line 899

    If I just want this to effect the “heading2” custom field. How would I edit it?

  • Hi @nitruc

    The code above is an example, to demonstrate how a filter can be added. It is not to be taken literally as you have done.

    Your errors say that you are trying to call a function ‘heading2’, but your above code does not show any reference to this.

    What you want to do is something like this:

    
    <?php
     
    function custom_post_title( $value, $post_id, $field  )
    {
    	// update the $post via the wp_update_post function
        
        
        
        
        return $value;
    }
     
    // acf/update_value/name={$field_name} - filter for a specific field based on it's name
    add_filter('acf/update_value/name=heading2', 'custom_post_title', 10, 3);
    
     
    ?>
    

    Does that help?

    Thanks
    E

  • Hmm… When I add the code you just wrote, I get the error:

    Fatal error: Cannot redeclare custom_post_title() (previously declared in /home/movies9/public_html/msv6/wp-content/themes/Media/functions.php:1824) in /home/movies9/public_html/msv6/wp-content/themes/Media/functions.php on line 1884

    When I remove my old code (mentioned above) that seems to be causing the conflict in the functions.php file, the permalink+heading2 no longer works and I’m left with the heading2 being left out of the URL again with nothing changing no matter how many times I hit the Update post button.

  • Hi @nitruc

    Your error says that you have created a function called ‘custom_post_title’. Is this true?

    The error says that WP has already defined this function, and that you can’t override it. Just rename your function to something unique. Perhaps use a prefix like ‘my_’:

    my_custom_post_title

    Thanks
    E

  • Ok, so the below code no longer causes the conflict, but is still requiring me to do the double update which is back to where we started:

    
    // Custom URL Post Title
    
    function custom_url_post_title($data , $postarr) {
    	if ( !in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
    		$custom_field_title = get_post_meta($postarr['post_ID'], 'heading2', true);
    		if(isset($custom_field_title) && $custom_field_title != '')
    			$data['post_name'] = sanitize_title($postarr['post_title'].'-'.$custom_field_title);
        }
        return $data;
    }
    add_action ('wp_insert_post_data','custom_url_post_title', 99, 2 );
    
    // End Custom URL Post Title
    
    // Custom ACF
     
    function custom_post_title( $value, $post_id, $field  )
    {
    	// update the $post via the wp_update_post function
        
        
        
        
        return $value;
    }
     
    // acf/update_value/name={$field_name} - filter for a specific field based on it's name
    add_filter('acf/update_value/name=heading2', 'custom_post_title', 10, 3);
    
    // End Custom ACF
    
  • Hi @nitruc

    I think you have miss-understood my advice with this one.

    Looking at your code, I can see that your function ‘custom_post_title’ is not actually doing anything, yest you are still using the ‘wp_insert_post_data’ action to do your logic.

    What I have tried to explain is that you SHOULDNT use the wp_insert_post_data action, but instead use the update_value filter instead.

    An action or a filter is a point in time where you can run some custom functionality. As stated previously, the wp_insert_post_data action is TOO EARLY to work correctly. You need to use the acf filter instead to get the latest ACF values and then use a wp_update_post function to modify the $post object.

    I hope you understand this.

    Thanks
    E

  • So it looks like the OP figured it out over at: https://wordpress.org/support/topic/adding-a-custom-field-in-addition-to-post-name-to-permalinks?replies=22

    However, I have a similar situation. If I should open a completely new thread then please let me know but I’d like to use an ACF Pro Relationship field to prepend to my CPT. Here’s my situation:

    I have a CPT “Team” with a relationship field that allows you to select multiple values for the CPT “Player”. The URL structure of the “Team” CPTs looks like “/teams/darkside/12-darkside” for the team named “12 Darkside” and the category “Darkside”. I currently am using code that I posted over at wordpress.org.
    The code updates the permalink of the “Team” to use the structure “/teams/[team-category]/[team-name]”.

    So…is there a way to use either Bidirectional Relationship code or just use the Relationship field on the “Team” CPT to prepend “/teams/darkside/12-darkside/” before the “Player” CPT’s permalink? The end result would look like “/teams/darkside/12-darkside/[player-name]”.

    Sources:

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Subheadline Text Delayed when Publishing?’ is closed to new replies.