Support

Account

Home Forums Front-end Issues Yoast Schema Person ID and ACF issue

Solved

Yoast Schema Person ID and ACF issue

  • I am making a function that change the author of a post with a User custom field from ACF and I need the the Schema ID to change as well.

    add_filter( 'wpseo_schema_person', 'example_change_person' );
    
    function example_change_person( $data ) {
        $altAuthorName = get_field('alt_author_name');
        $altAuthorNameDisplay = $altAuthorName['display_name'];
        $altAuthorEmail = $altAuthorName['user_email'];
    
        if($altAuthorNameDisplay){
        // data
        $author_avatar = get_avatar_url($altAuthorEmail);
        $data['name'] = $altAuthorNameDisplay;
        $data['author']['name'] = $altAuthorNameDisplay;
        $data['image']['caption'] = $altAuthorNameDisplay;
        $data['image']['url'] = $author_avatar;
        $data['@id'] = $string;
        return $data;
        }
    }

    I have to problems:

    1.
    I cannot find the Yoast Schema ID for a given WordPress user. Which is not really a ACF issue.

    2.
    The value of the field return null when used in the in if-statement.

    https://developer.yoast.com/features/schema/api/
    https://developer.yoast.com/features/schema/pieces/person#required-properties

  • I cannot find any documentation on the hook wpseo_schema_person or what is passed to your filter.

    Searching the code I can also not find where this hook is called specifically. I can only find 2 places where filtering is done that may be where this is happening.

    
    // the first one
    $graph_piece = \apply_filters( 'wpseo_schema_' . $identifier, $graph_piece, $context );
    
    // the second one
    $graph_piece = \apply_filters( 'wpseo_schema_' . $type, $graph_piece, $context );
    

    Your issue is that in order to get the values from the ACF fields for a user you need to supply the correct $post_id

    
    $altAuthorName = get_field('alt_author_name', 'user_'.$user_id);
    

    If the user ID is not supplied in one of the arguments when the filter is applied there is likely no possibility of you being able to alter this. What you need to do is to output all of what is being supplied and see if the user ID is in there.

    You may need to adjust the priority up or down in this.

    
    add_filter( 'wpseo_schema_person', 'example_change_person', 10, 2 );
    
    function example_change_person( $data, $context ) {
      echo '<pre>'; var_dump($data); echo '</pre>';
      echo '<pre'>; var_dump($context); echo '</pre>';
      die;
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.