• Resolved tjohnston

    (@tjohnston)


    I’m trying to add a custom field after the page title so that everything will display like this:

    [page title] [custom field] | [site title]

    I could do this previously hooking into ‘document_title_parts’, is there any equivalent for SEO framework?

    ‘the_seo_framework_pro_add_title’ only seems to allow me to insert stuff before or after the entire string.

    Thanks!

Viewing 1 replies (of 1 total)
  • Plugin Author Sybre Waaijer

    (@cybr)

    Hi @tjohnston,

    I believe this is possible by using the pre filter, as shown in the snippet below.
    Do note that the snippet below is hypothetical and you’ll need to adjust the $custom_field variable.

    add_filter( 'the_seo_framework_pre_add_title', function( $title, $args ) {
    	if ( is_singular() ) {
    		//= term_id is also the post ID.
    		$custom_field = get_post_meta( $args['term_id'], 'my_custom_field', true );
    
    		if ( $custom_field ) {
    			$tsf = the_seo_framework();
    
    			//= Whether internal custom fields are enabled, fetch that.
    			if ( $args['get_custom_field'] )
    				$title = $tsf->get_custom_field_title( $title, $args['term_id'], $args['taxonomy'] );
    
    			//= When no custom field is found, generate a title.
    			if ( empty( $title ) )
    				$title = (string) $tsf->generate_title( $args, false );
    
    			if ( $title ) {
    				$title = $title . ' ' . $custom_field;
    			} else {
    				$title = $custom_field;
    			}
    		}
    	}
    
    	return $title;
    }, 10, 2 );

    Also, note that this is a bit of a hack. To make this less of a hack, I’m planning to restructure the title generation for 3.1, which might/should result in your original method working again:
    https://github.com/sybrew/the-seo-framework/issues/202

    I hope this helps you get you on your path ?? Happy holidays!

Viewing 1 replies (of 1 total)
  • The topic ‘Hooking into Title parts’ is closed to new replies.