Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active April 3, 2024 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/f567c630f58fe93970fbb8c85fdef121 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/f567c630f58fe93970fbb8c85fdef121 to your computer and use it in GitHub Desktop.
[SmartCrawl Pro] Custom breadcrumb Home item
<?php
/**
* Plugin Name: [SmartCrawl Pro] Custom breadcrumb Home item
* Description: Changes the Home link and title from the breadcrumbs, based on the current CPT
* Author: Anderson Salas @ WPMUDEV
* Task: SLS-5976
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
add_action( 'plugins_loaded', function() {
if ( ! defined( 'SMARTCRAWL_VERSION' ) ) {
return; // SmartCrawl is not installed/enabled.
}
add_filter( 'smartcrawl_breadcrumbs_render_output', function( $output ) {
$cpt = get_post_type( get_the_ID() );
if ( empty( $cpt ) ) {
return $output;
}
// Change this:
$names = [
// (CPT SLUG) => [ (NEW HOME URL), (NEW HOME TEXT) ]
'training' => [
site_url( '/formations/' ),
'Formations'
],
'support' => [
site_url( '/accompagnements/' ),
'Accompagnements'
],
'resource' => [
site_url( '/ressources/' ),
'Ressource'
],
];
$regex = '/<span class="smartcrawl-breadcrumb"><a href="(.*?)" title="(.*?)">(.*?)<\/a><\/span>/m';
$home = _x( 'Home', 'breadcrumb', 'wds' );
foreach ( $names as $slug => [ $url, $name ] ) {
if ( $slug === $cpt ) {
if ( preg_match(
$regex,
$output,
$matches
) ) {
$search = '<span class="smartcrawl-breadcrumb">';
$search .= '<a href="' . $matches[1] . '" title="' . $home . '">';
$search .= $home;
$search .= '</a></span>';
$replace = '<span class="smartcrawl-breadcrumb">';
$replace .= '<a href="' . $url . '" title="' . $name . '">';
$replace .= $name;
$replace .= '</a></span>';
return str_ireplace( $search, $replace, $output );
}
}
}
return $output;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment