Edit Yoast SEO breadcrumbs output

I want to customise the output of Yoast SEO breadcrumbs, I have this so far which works great:

add_filter( 'wpseo_breadcrumb_single_link', 'ss_breadcrumb_single_link', 10, 2 );
function ss_breadcrumb_single_link( $link_output, $link ) {
    $element = 'li';
    $element = esc_attr( apply_filters( 'wpseo_breadcrumb_single_link_wrapper', $element ) );
    $link_output = '<' . $element . ' typeof="v:Breadcrumb">';
    if ( isset( $link['url'] ) && ( $i < ( count( $links ) - 1 ) || $paged ) ) {
        $link_output .= '<a href="' . esc_url( $link['url'] ) . '" rel="v:url" property="v:title">' . esc_html( $link['text'] ) . '</a>';
    } else {
        if ( isset( $opt['breadcrumbs-boldlast'] ) && $opt['breadcrumbs-boldlast'] ) {
            $link_output .= '<strong class="breadcrumb_last" property="v:title">' . esc_html( $link['text'] ) . '</strong>';
        } else {
            $link_output .= '<li class="breadcrumb_last" property="v:title">' . esc_html( $link['text'] ) . '</li>';
        }
    }
    $link_output .= '</' . $element . '>';
    return $link_output;
}

add_filter( 'wpseo_breadcrumb_output_wrapper', 'ss_breadcrumb_output_wrapper', 10, 1 );
function ss_breadcrumb_output_wrapper( $wrapper ) {
    $wrapper = 'ol';
    return $wrapper;
}

Next step is to change this line:

Read More
return apply_filters( 'wpseo_breadcrumb_output', '<' . $wrapper . $id . $class . ' xmlns:v="http://rdf.data-vocabulary.org/#">' . $output . '</' . $wrapper . '>' );

I want to add an aria element of aria-labelledby="breadcrumblabel" to that output. So I setup this function:

add_filter( 'wpseo_breadcrumb_output', 'ss_breadcrumb_output' );
function ss_breadcrumb_output() {
    return apply_filters( 'ss_breadcrumb_output', '<' . $wrapper . $id . $class . ' xmlns:v="http://rdf.data-vocabulary.org/#">' . $output . '</' . $wrapper . '>' );
}

Problem I am having is $wrapper $id $class and $output all return null. I think I know why they are null although as a PHP beginner I can’t explain it.

If someone could point me in thr right direction so I can go figure this out, i’d appreciate it.

Thanks.

Related posts

Leave a Reply

2 comments

  1. They are all null. You haven’t set any of those variables. Here is your function.

    function ss_breadcrumb_output() {
        return apply_filters( 'ss_breadcrumb_output', '<' . $wrapper . $id . $class . ' xmlns:v="http://rdf.data-vocabulary.org/#">' . $output . '</' . $wrapper . '>' );
    }
    

    All of the variables in that function are isolated to that function. Variables set outside that function don’t work. Since you haven’t set any variables (in the function), and they aren’t passed into the function, they are empty. It is a matter of scope. There are ways around that, like globalization, but that isn’t critical to this question so I’ll leave you to your own devices.

    But to your problem… Look at the filter where it is applied in the plugin.

    return apply_filters( 'wpseo_breadcrumb_output', '<' . $wrapper . $id . $class . ' xmlns:v="http://rdf.data-vocabulary.org/#">' . $output . '</' . $wrapper . '>' );
    

    I think this may be where you are going wrong. This–'<' . $wrapper . $id . $class . ' xmlns:v="http://rdf.data-vocabulary.org/#">' . $output . '</' . $wrapper . '>'— is all one parameter. That . is string concatenation operator, meaning all of that is merged together into a string. When that is passed to the filter, there are no variables anymore. They are passed to the filter as a simple string.

    Your function could catch and manipulate that string if you added parameter like:

    function ss_breadcrumb_output($output) {
        var_dump($output); // you should see the string referred to above
        return apply_filters( 'ss_breadcrumb_output', '<' . $wrapper . $id . $class . ' xmlns:v="http://rdf.data-vocabulary.org/#">' . $output . '</' . $wrapper . '>' );
    }
    

    I am confused about this part: return apply_filters( 'ss_breadcrumb_output', '<' . $wrapper . $id . $class . ' xmlns:v="http://rdf.data-vocabulary.org/#">' . $output . '</' . $wrapper . '>' );

    If you are trying to pass the breadcrumb output to another filter (of your creation, I assume) you’d want:

    function ss_breadcrumb_output($output) {
        return apply_filters( 'ss_breadcrumb_output', $output );
    }
    

    But why? Anything you could do with that you could do with the wpseo_breadcrumb_output filter.

    I think you just want:

    function ss_breadcrumb_output($output) {
        $output = preg_replace('/([^>]+)>(.*)/','$1 aria-labelledby="breadcrumblabel" >$2',$output);
        return $output;
    }
    

    I should also not that regex on markup is pretty dicey. It is easy to get it wrong. Use with caution.