Filter uncanny-learndash-toolkit

uo_build_anchor_links

Filters anchor link generation, allowing modification of generated links, permalinks, titles, and types.

add_filter( 'uo_build_anchor_links', $callback, 10, 4 );

Description

Filters the generated HTML anchor link for breadcrumbs. Developers can modify the link's structure, attributes, or content before it's displayed. This hook is applied after the initial link is constructed and before it is returned by the `uo_build_anchor_links` function.


Usage

add_filter( 'uo_build_anchor_links', 'your_function_name', 10, 4 );

Parameters

$link (mixed)
This parameter is the HTML anchor link that is being built.
$permalink (mixed)
This parameter is the generated anchor link HTML for the breadcrumb item.
$title (mixed)
This parameter contains the title of the breadcrumb link, used for its display text and as an attribute.
$type (mixed)
This parameter represents the sanitized version of the link's title, often used for creating URL slugs or internal link relationships.

Return Value

The filtered value.


Examples

// Add a specific class to the anchor link when the type is 'page'
add_filter( 'uo_build_anchor_links', function( $link, $permalink, $title, $type ) {
    if ( 'page' === $type ) {
        // Assuming $link is a string containing the HTML.
        // We'll add a class to the <a> tag.
        $link = str_replace( 'class="trail-begin"', 'class="trail-begin page-link"', $link );
    }
    return $link;
}, 10, 4 );

// Remove the 'rel="nofollow"' attribute if the permalink is internal
add_filter( 'uo_build_anchor_links', function( $link, $permalink, $title, $type ) {
    // A simplified check for internal permalink. In a real scenario,
    // you might use parse_url and compare with site_url().
    if ( strpos( $permalink, site_url() ) !== false ) {
        $link = str_replace( 'rel="nofollow"', '', $link );
    }
    return $link;
}, 10, 4 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/classes/breadcrumbs.php:416

public static function uo_build_anchor_links( $permalink, $title, $type = '' ) {

		$link = sprintf(
			'<span itemscope="" itemtype="http://schema.org/Breadcrumb"><a href="%1$s" title="%2$s" rel="%3$s" class="trail-begin"><span itemprop="%2$s">%4$s</span></a></span>',
			esc_url( $permalink ),
			esc_attr( $title ),
			sanitize_title( $title ),
			esc_html( $title )
		);

		return apply_filters( 'uo_build_anchor_links', $link, $permalink, $title, $type );

	}

Scroll to Top