Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

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.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
First, thank you for such a detailed answer. I'm interested you say the variables are empty due to a matter of scope. But then you use $output in your last 2 examples of how to solve this? I have tried to var_dump($output) inside the function but nothing is returned. –  showFocus Feb 26 '13 at 22:17
    
Look at the alteration I made to your function definition: ss_breadcrumb_output($output). You have to pass the variable in in order to use it (With filters and actions the variable passing is behind the scene). I think that is where you are going wrong. –  s_ha_dum Feb 26 '13 at 22:23
    
Ok, I think I understand. One thing I don't understand is your use of $output. In the original class file, $output seems to be inside '<' . $wrapper . $id . $class . ' xmlns:v="http://rdf.data-vocabulary.org/#">' I don't understand why we are altering $output? I've already changed what I want in the $output variable in other functions. I'm going away to do some more research, as you can tell this is all new to me! Thanks again. –  showFocus Feb 26 '13 at 22:31
    
Remember about scope. You are altering what comes into the function. Variables, even variables of the same name, that are outside the function are out of scope and don't exist inside the function. But you can use any variable name you want, if it makes you feel better. –  s_ha_dum Feb 26 '13 at 22:36
    
The penny drops! I'd give you a vote up if I could, but it seems I don't have that privilege just yet. So i'll just say thank you. @s_ha_dum –  showFocus Feb 26 '13 at 22:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.