Skip to content

Instantly share code, notes, and snippets.

@remcotolsma
Last active December 17, 2017 20:29
Show Gist options
  • Save remcotolsma/b0289a3c0856d1727f046e3883e4fc2e to your computer and use it in GitHub Desktop.
Save remcotolsma/b0289a3c0856d1727f046e3883e4fc2e to your computer and use it in GitHub Desktop.
WordPress custom endpoints and Breadcrumb NavXT.
<?php
/**
* WordPress custom endpoints breadcrumbs.
*/
class WordPressCustomEndpointsBreadcrumbs {
/**
* Setup.
*/
public function setup() {
// Actions
add_action( 'bcn_before_fill', array( $this, 'bcn_before_fill' ) );
}
/**
* Breadcrumb NavXT before fill.
*
* @param bcn_breadcrumb_trail $trail
*/
public function bcn_before_fill( $trail ) {
if ( ! method_exists( $trail, 'add' ) ) {
return;
}
if ( ! class_exists( 'bcn_breadcrumb' ) ) {
return;
}
$endpoint = _x( 'photos', 'endpoint', 'custom-text-domain' );
$value = get_query_var( $endpoint, null );
if ( null === $value ) {
return;
}
if ( ! empty( $value ) ) {
$trail->add( new bcn_breadcrumb(
$value,
null,
array(
'custom-photo',
),
null,
null
) );
}
$trail->add( new bcn_breadcrumb(
__( 'Photos', 'custom-text-domain' ),
null,
array(
'custom-photos',
),
trailingslashit( get_permalink() . $endpoint ),
null
) );
if ( ! has_action( 'bcn_breadcrumb_url', array( $this, 'bcn_breadcrumb_url' ) ) ) {
add_action( 'bcn_breadcrumb_url', array( $this, 'bcn_breadcrumb_url' ), 10, 3 );
}
}
/**
* Breadcrumb NavXT breadcrumb URL.
*
* @see https://github.com/mtekk/Breadcrumb-NavXT/blob/v5.7.1/class.bcn_breadcrumb.php#L121
* @param string $url
* @param string $type
* @param string $id
* @return string
*/
public function bcn_breadcrumb_url( $url, $type, $id ) {
if ( null !== $url ) {
return $url;
}
$post_type = 'object';
if ( in_array( 'post-' . $post_type, $type ) ) {
return get_permalink( $id );
}
return $url;
}
}
$wp_custom_endpoints_breadcrumbs = new WordPressCustomEndpointsBreadcrumbs();
$wp_custom_endpoints_breadcrumbs->setup();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment