Skip to main content

Exclude domains, post types or categories from Link Health from code

Premium Intermediate

GET / PATCH /benecaster-outlinks/v1/link-health/exclusions is the route behind Link Health’s exclusions form. It is install-wide: it needs manage_options (an X-WP-Nonce for wp_rest) and Outlinks on for at least one show, else 403 benecaster_outlinks_not_active. Use the route rather than writing the setting yourself: it validates the lists and tidies Link Health’s list of items to match.

Both methods answer { domains, post_types, categories }, with categories as { id, name, taxonomy } and a term deleted since left out. GET adds category_choices, every post category and Episode Category the form can offer (empty ones included), in the same shape; PATCH adds removed. PATCH { domains?, post_types?, categories? } replaces each list it carries and keeps the others; null or [] empties one; each list holds at most 100 entries.

  • domains: a domain, or a pasted URL (its host is used), stored lower case without a leading www.. It covers its subdomains: amazon.com excludes www.amazon.com and smile.amazon.com, never amazon.co.uk or notamazon.com. It applies to tracked links as well as content: an excluded URL is not checked and does not count toward a tracked link’s status, and a tracked link whose every URL is excluded reads excluded.
  • post_types: any of episode, page and post. Content only.
  • categories: term ids of WordPress’s post categories or Benecaster’s Episode Categories. A post in any of them is not read. Content only.

Code

<?php
wp_set_current_user( 1 );
$request = new WP_REST_Request( 'PATCH', '/benecaster-outlinks/v1/link-health/exclusions' );
$request->set_header( 'X-WP-Nonce', wp_create_nonce( 'wp_rest' ) );
$request->set_body_params( [ 'domains' => [ 'amazon.com', 'youtube.com' ] ] ); // post_types and categories are kept
$saved = rest_do_request( $request )->get_data(); // [ 'domains' => [ 'amazon.com', 'youtube.com' ], …, 'removed' => 12 ]

View on GitHub →

Need this built rather than just documented? See our services →