From 7cf38f23927e6238b3251a733905c3c47146b5d1 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Mon, 30 Mar 2026 20:44:17 +0200 Subject: [PATCH] Add support for `nomention` CSS class to exclude links from Webmentions Links with a `nomention` class will be skipped when extracting URLs for sending Webmentions. A `webmention_nomention_class` filter allows developers to customize the class name. Closes #166 --- includes/functions.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index 6e539fbd..29a43b40 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -343,12 +343,27 @@ function webmention_extract_urls( $content, $support_media_urls = false ) { $attributes = array_merge( $attributes, $media_attributes ); } + /** + * Filters the CSS class name used to prevent sending Webmentions for a link. + * + * @param string $nomention_class The CSS class name. Default 'nomention'. + */ + $nomention_class = apply_filters( 'webmention_nomention_class', 'nomention' ); + $urls = array(); foreach ( $attributes as $attribute => $elements ) { foreach ( $elements as $element ) { - foreach ( $xpath->query( sprintf( '//%1$s[@%2$s]', $element, $attribute ) ) as $url ) { - $urls[] = $url->getAttribute( $attribute ); + foreach ( $xpath->query( sprintf( '//%1$s[@%2$s]', $element, $attribute ) ) as $node ) { + // Skip elements that have the nomention class. + if ( $nomention_class && $node->hasAttribute( 'class' ) ) { + $classes = preg_split( '/\s+/', $node->getAttribute( 'class' ) ); + if ( in_array( $nomention_class, $classes, true ) ) { + continue; + } + } + + $urls[] = $node->getAttribute( $attribute ); } } }