WordPress.org

Support

Support » Plugins and Hacks » Hacks » Adding a custom field in addition to Post Name to Permalinks?

Adding a custom field in addition to Post Name to Permalinks?

  • 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.

Viewing 15 replies - 1 through 15 (of 21 total)
  • 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,

    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()

    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,

    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.

    Moderator bcworkz

    @bcworkz

    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.

    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,

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

    Hi @pgrizz,

    Your most welcome, if I can be of any further assistance please don’t hesitate to ask 🙂

    Cheers.

    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?

    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,

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

    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,

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

    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.

Viewing 15 replies - 1 through 15 (of 21 total)
  • The topic ‘Adding a custom field in addition to Post Name to Permalinks?’ is closed to new replies.
Skip to toolbar