Skip to content

Instantly share code, notes, and snippets.

@vovafeldman
Last active April 20, 2023 14:54
Show Gist options
  • Save vovafeldman/60532ce0b7efb565162208eeff755b16 to your computer and use it in GitHub Desktop.
Save vovafeldman/60532ce0b7efb565162208eeff755b16 to your computer and use it in GitHub Desktop.
Freemius - weDocs KB Breadcrumbs Rich-Snipets
<?php
/**
* Override weDocs breadcrumb to add schema.org rich snippets metadata.
*
* @author Vova Feldman (@svovaf)
*
* @return void
*/
function freemius_wedocs_breadcrumbs() {
global $post;
$args = apply_filters( 'wedocs_breadcrumbs', array(
'delimiter' => '<li class="delimiter">&rarr;</li>',
'home' => __( 'Home', 'wedocs' ),
'before' => '<li><span class="current">',
'after' => '</span></li>'
) );
$breadcrumb_position = 1;
echo '<ol class="wedocs-breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList">';
echo freemius_wedocs_get_breadcrumb( $args['home'], home_url( '/' ), $breadcrumb_position );
echo $args['delimiter'];
$breadcrumb_position ++;
if ( $post->post_type == 'docs' && $post->post_parent ) {
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ( $parent_id ) {
$page = get_post( $parent_id );
$breadcrumbs[] = freemius_wedocs_get_breadcrumb( get_the_title( $page->ID ), get_permalink( $page->ID ), $breadcrumb_position );
$parent_id = $page->post_parent;
$breadcrumb_position ++;
}
$breadcrumbs = array_reverse( $breadcrumbs );
for ( $i = 0; $i < count( $breadcrumbs ); $i ++ ) {
echo $breadcrumbs[ $i ];
if ( $i != count( $breadcrumbs ) - 1 ) {
echo $args['delimiter'];
}
}
echo ' ' . $args['delimiter'] . ' ' . $args['before'] . get_the_title() . $args['after'];
}
echo '</ol>';
}
/**
* @author Vova Feldman (@svovaf)
*
* @param string $label
* @param string $permalink
* @param int $position
*
* @return string
*/
function freemius_wedocs_get_breadcrumb( $label, $permalink, $position = 1 ) {
return '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
<a itemprop="item" href="' . esc_attr( $permalink ) . '">
<span itemprop="name">' . esc_html( $label ) . '</span></a>
<meta itemprop="position" content="' . $position . '" />
</li>';
}
@karpalig
Copy link

IIt turns out very gracefully.

The biggest complaint about this plugin is that the developers refer to "home" as the main page of the website, but users want "home" to be the main page of the documents.

If you close this, there will definitely be more than one comment here.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment