WordPress.org

Ready to get started?Download WordPress

Forums

Adding a custom field in addition to Post Name to Permalinks? (22 posts)

  1. PGrizz
    Member
    Posted 8 months ago #

    By default WordPress takes the page title you enter in the editor and makes it into a permalink. This title is usually also the h1 of the post.

    If I keep the default title in the editor as my h1, but then add another h2 that's added by an Advanced Custom Field, how would I be able to combine the custom h2 field into my page's permalink to help my SEO?

    For Example: My post's title would be "WordPress Title" and my custom h2 field added to the post would be "Example for Forum" so the url would end up being "site.com/wordpress-title-example-for-forum"

    Doesn't seem like it's as easy as just appending the custom field on to the end of /%postname%/ in the Custom Structure of the Permalink settings.

  2. WPMU DEV
    Member
    Posted 8 months ago #

    Hi @PGrizz,

    I hope you are well today and thank you for your question.

    To add a custom field in addition to Post Name to Permalinks, just add following code in functions.php file of your theme.

    function custom_post_title($title) {
        global $post;
        $type = get_post_type($post->ID);
        if ($type== 'post') {
    		$title = $post->post_title;
    		$custom_field_title = get_post_meta($post->ID, 'custom_title', true);
    		if(isset($custom_field_title))
    			return $title.'-'.$custom_field_title;
    		else
    			return $title;
    
        }
    	 return $title;
    }
    add_filter ('title_save_pre','custom_post_title');

    Please advise if you have more questions.

    Best Regards,

  3. PGrizz
    Member
    Posted 8 months ago #

    Thanks for the quick reply and detailed example WPMU DEV! However, it doesn't seem like that's working... :(

    When I add that to the end of my functions.php file within the wp-included folder (even before editing anything to match my custom "headline2" field) I get the following error when trying to view the site: Fatal error: Call to undefined function add_filter()

  4. WPMU DEV
    Member
    Posted 8 months ago #

    Hi @PGrizz,

    Thanks for the reply.

    When I add that to the end of my functions.php file within the wp-included folder (even before editing anything to match my custom "headline2" field) I get the following error when trying to view the site: Fatal error: Call to undefined function add_filter()

    You should add provided code in functions.php file of your theme and not functions.php file within the wp-includes folder. You should also create custom field with the name "custom_title".

    Kind Regards,

  5. PGrizz
    Member
    Posted 8 months ago #

    Ah, ok, figured a change like this would require hacking of WordPress and not the theme! I made the change to the theme's functions.php, but it's still acting very weird and not doing what we want.

    Now every time I update the post with the "custom_title" added as a custom field, it adds the custom_title on to the post's title in the editor and on the actual page. Sorry for any confusion, but I want the custom_title to be added on to the post's URL, not the default headline. The whole reason I'm doing this is to try to separate the default headline and a new custom h2 line, yet still keep both in the URL.

    It also seems to be glitching where every time the post is updated it adds the custom_title again, so if you update twice it will say Title-custom_title-custom_title, three updates will cause it to say Title-custom_title-custom_title-custom_title, and so on.

    Even after deleting the custom_title field from the post it keeps adding more custom_titles on to the title after every update made to the post.

  6. bcworkz
    Member
    Posted 8 months ago #

    The slug name for a newly edited post is created by the javascript function autosave_update_slug() in wp-includes/js/autosave.min.js. The non-minified version is autosave.js. The proposed slug is sent to the server via AJAX request to ensure it is not already used, the server returns the full HTML of the complete permalink and edit button, with the slug adjusted as necessary.

    I'm not sure of the proper way to override this, or if it is even possible. This is where you would need to focus your attentions to get a slug composed the way you want.

  7. WPMU DEV
    Member
    Posted 8 months ago #

    Hi @PGrizz,

    Thanks for the reply.

    You don't need to hack WordPress core. you have to just add provided code in the bottom of functions.php file of your theme.

    Now i properly understood what you want to achieve, just add following code in theme's functions.php file and remove the old provided code from the 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'], 'custom_title', 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 );

    Best Regards,

  8. PGrizz
    Member
    Posted 8 months ago #

    Works like a charm! Thank you so much WPMU DEV! And props on making it look so easy. :)

  9. WPMU DEV
    Member
    Posted 8 months ago #

    Hi @PGrizz,

    Your most welcome, if I can be of any further assistance please don't hesitate to ask :)

    Cheers.

  10. PGrizz
    Member
    Posted 8 months ago #

    Hey @WPMU DEV,

    There's actually one slight problem I just noticed. :(

    When I add the title, WordPress usually automatically creates the URL underneath faster than I can add the subheadline as a custom field.

    So when I click Publish or Update, it creates the page using just the normal permalink structure, even after I've added both the headline and subheadline.

    I then need to click the Update button a second time immediately after the page is published/updated in order to get the page to update a second time with the headline+subheadline in the permalink.

    Is there any way you can suggest to get this to work the first time so we don't need to double submit each time we want the subheadline added to the permalink?

  11. WPMU DEV
    Member
    Posted 8 months ago #

    Hi @PGrizz,

    I could not reproduce the same problem you are facing on my test site.
    Can you tell me how you are adding subheadline, using default custom field or creating new subheadline meta box for posts?
    You can try changing priority of hook in the code which is currently 99

    Kind Regards,

  12. PGrizz
    Member
    Posted 8 months ago #

    I'm using a basic text field in Advanced Custom Fields with metabox turned off.

  13. WPMU DEV
    Member
    Posted 8 months ago #

    Hi @PGrizz,

    Please reply a bit in more detail. Are you referring Advanced Custom Fields as a this plugin http://wordpress.org/plugins/advanced-custom-fields/ ?

    Best Regards,

  14. PGrizz
    Member
    Posted 8 months ago #

    Yes, sorry, that exact plugin using the basic text custom field added right under the title in the top position.

  15. PGrizz
    Member
    Posted 8 months ago #

    Looks like it's the ACF. :(

    Like you said, if I just add the sub heading as a normal custom field it works perfect. But when it's added via ACF, it requires the double update. Changing the priority of the hook from 99 to 1 doesn't seem to do anything.

  16. PGrizz
    Member
    Posted 8 months ago #

  17. WPMU DEV
    Member
    Posted 8 months ago #

    Hi @PGrizz,

    Please check the answer posted on the ACF forum post that you have linked in last reply.

    Cheers.

  18. PGrizz
    Member
    Posted 8 months ago #

    @WPMU DEV

    Yeah... I saw that. Just trying to figure out how to do it now since I've never done it before. ;)

  19. PGrizz
    Member
    Posted 7 months ago #

    @WPMU DEV

    I still can't seem to get this to work... :(

    The ACF plugin dev has been trying to help, but some of his advice has been going over my head and all the changes I make based off of his suggestions aren't working.

    Could you please have a look? As you would better understand what he's suggesting and how to modify your original code to work with ACF.

    Link top the support thread is again here: http://support.advancedcustomfields.com/forums/topic/subheadline-text-delayed-when-publishing/

  20. WPMU DEV
    Member
    Posted 7 months ago #

    Hi @PGrizz,

    Try removing whole previous code and add the following code in place of it.

    // Custom URL Post Title
    
    function custom_post_title( $value, $post_id, $field  )
    {
    	// update the $post via the wp_update_post function
    
    		if(isset($value) && $value != '') {
    			 $args = array(
    				 'ID'           => $post_id,
    				 'post_name' => sanitize_title(get_the_title($post_id).'-'.$value)
    			 );
    			 wp_update_post( $args );
    		} 
    
        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 URL Post Title

    Best Regards,

  21. PGrizz
    Member
    Posted 7 months ago #

    Yep, that did it! Thanks again! =D

  22. WPMU DEV
    Member
    Posted 7 months ago #

    Hi @PGrizz,

    You are welcome.

    Cheers.

Reply

You must log in to post.

About this Topic