As mentioned yesterday I was looking to get "nomentions" working without having to edit the webmention plugin itself.

David pointed out that the plugin sets up a filter which enables you to process the target URLs before mentions are sent so I needed to hook into this:

$targets = apply_filters( 'webmention_links', $links, $post_id );

By default nothing happens with this line as no filters are defined leaving you free to add whatever you need. So I moved my code from the webmention plugin to a callback function in a new plugin which hooks in using add_filter() and checks each link in the post for the nomention class, excluding any it finds.

The code is as follows:

function nomention_links( $links, $post_id ) {

  $post = get_post( $post_id );
  preg_match_all('/<a[^>]+>/i',$post->post_content, $results);

  $mentions = '';
  foreach ($results[0] as $link) {
    if (!strpos($link, 'class="nomention')) {
      $mentions .= $link;
    }
  }
  $links = wp_extract_urls($mentions);
  return $links;
}
add_filter( 'webmention_links', 'nomention_links', 10, 2 );

As my posts are written in Markdown (and WordPress uses MultiMarkdown thanks to JetPack) I can easily add the nomention class to any link like this:

[link text here](http://thisisthe.link) {.nomention}

My original use case was to be able to link to a conversation on micro.blog without the post automatically being treated as a reply but there could be other occasions when you don't want to send a mention for whatever reason.

As always, the plugin is listed on GitHub should you have a need and want to try it.

Note: the plugin only handles <a> tags, if you wanted to extend this to other types of link such as <img> tags please see the note in the readme.

Leave a Reply

Your email address will not be published.