I am getting an undefined array error. The goal here is to override the second link to simply go back one page. An example is
Custom Templates > Category > Template
I added the following code to hijack the breadcrumb structure. Yoast kept adding /blog into the url, and this is a custom post type. My hack is working, however I am getting an error on the server:
PHP Warning: Undefined array key "HTTP_REFERER"
add_filter('wpseo_breadcrumb_links', 'wpse_332125_breadcrumbs');
function wpse_332125_breadcrumbs($links) {
if(is_singular('custom-template')) {
$links[2] = array(
'text' => 'Category',
'url' => $_SERVER['HTTP_REFERER'],
'allow_html' => 2
);
}
return $links;
}
The
RefererHTTP request header is supplied by browser. It's trivial to forge, but it can also point to a third-party web site that links to yours. Some tools (browser extensions, firewalls...) strip it altogether for security/privacy reasons. And you can't always trust on intermediate proxies to pass on the header.Said that... If you want to use it, you need to cover several cases:
These points aren't hard to deal with from a technical standpoint (you have isset(), parse_url(), str_starts_with()...), but some of them they require making some decisions regarding functionality and security.
Possible alternatives to replace or complement this: