Code for new button: ‘Check Links Missing TAGS’
-
I wanted a new button to ‘Check Links Missing TAGS’, same as ‘Check Links Missing Categories’.
I am not a dev or a coder, so I asked Claude. Code provided below, as-is. Feel free to audit or edit as required.
To enable/manage, best to add to your Code-Snippets plugin.
/**
* Snippet Name: LL Check Links Missing Tags
* Version: 1.1.0 (2026-04-15)
* Description: Adds a 'Check Links Missing Tags' button to the Link Library
* link-checking tools page (link-library-reciprocal), mirroring
* the 'Check Links Missing Categories' feature.
* Uses message=9 on the reciprocal page (unused by core as of v7.8.9).
*/
// ── 1. Inject the button after the emptycatcheck button row ──────────────────
add_action( 'admin_footer', 'll_missing_tags_inject_button' );
function ll_missing_tags_inject_button() {
$screen = get_current_screen();
if ( ! $screen || $screen->id !== 'link_library_links_page_link-library-reciprocal' ) {
return;
}
?>
<script type="text/javascript">
jQuery(document).ready(function($){
var $catBtn = $('input#emptycatcheck').closest('tr');
if ( $catBtn.length ) {
$catBtn.after(
'<tr><td>' +
'<input class="button" type="submit" id="emptytagcheck" ' +
'name="emptytagcheck" value="<?php echo esc_js( __( 'Check Links Missing Tags', 'link-library' ) ); ?>" />' +
'</td></tr>'
);
}
});
</script>
<?php
}
// ── 2. Intercept the form post BEFORE the plugin handler (priority 1 vs 10) ──
// The plugin's nonce action is 'link-library' (wp_nonce_field( 'link-library' )).
// The plugin uses $_POST['_wp_http_referer'] for the redirect — we must do the same.
add_action( 'admin_post_save_link_library_reciprocal', 'll_missing_tags_intercept_save', 1 );
function ll_missing_tags_intercept_save() {
if ( ! isset( $_POST['emptytagcheck'] ) ) {
return; // Not our button — let the plugin handle it at priority 10
}
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( __( 'Not allowed', 'link-library' ) );
}
// Match the exact nonce the plugin generates
check_admin_referer( 'link-library' );
// Mirror the plugin's own redirect logic exactly
$redirect_url = remove_query_arg( array( 'message' ), $_POST['_wp_http_referer'] );
$redirect_url = add_query_arg( 'message', 9, $redirect_url );
wp_redirect( $redirect_url );
exit;
}
// ── 3. Display report when message=9 on the reciprocal page ──────────────────
add_action( 'admin_notices', 'll_missing_tags_display_report' );
function ll_missing_tags_display_report() {
if (
! isset( $_GET['page'] ) || $_GET['page'] !== 'link-library-reciprocal' ||
! isset( $_GET['message'] ) || intval( $_GET['message'] ) !== 9
) {
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$genoptions = get_option( 'LinkLibraryGeneral' );
$genoptions = wp_parse_args( $genoptions, array( 'tagtaxonomy' => 'link_library_tags' ) );
$tag_tax = sanitize_key( $genoptions['tagtaxonomy'] );
global $wpdb;
// SQL mirrors link_library_empty_cat_link_checker() with tag taxonomy
$linkquery = "SELECT p.ID, p.post_title, pm.meta_value FROM {$wpdb->posts} p ";
$linkquery .= "JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id ";
$linkquery .= "WHERE p.post_type = 'link_library_links' ";
$linkquery .= "AND p.post_status IN ('publish','pending','draft','future','private') ";
$linkquery .= "AND pm.meta_key = 'link_url' ";
$linkquery .= "AND NOT EXISTS ( ";
$linkquery .= "SELECT 1 FROM {$wpdb->term_relationships} rel ";
$linkquery .= "JOIN {$wpdb->term_taxonomy} tax ON tax.term_taxonomy_id = rel.term_taxonomy_id ";
$linkquery .= "AND tax.taxonomy = %s ";
$linkquery .= "WHERE p.ID = rel.object_id ";
$linkquery .= ")";
$links = $wpdb->get_results( $wpdb->prepare( $linkquery, $tag_tax ) );
echo "<div id='message' class='updated fade'><p>";
echo "<strong>" . __( 'Links Missing Tags Report', 'link-library' ) . "</strong><br /><br />";
if ( $links ) {
foreach ( $links as $link ) {
$edit_url = esc_url( add_query_arg( array( 'action' => 'edit', 'post' => $link->ID ), admin_url( 'post.php' ) ) );
echo esc_html( $link->ID ) . ' - ' . esc_html( $link->post_title ) . ': ';
echo esc_html( $link->meta_value );
echo ' <a href="' . $edit_url . '">Edit</a><br /><br />';
}
} else {
echo __( 'No links found without tags.', 'link-library' );
}
echo '</p></div>';
}
You must be logged in to reply to this topic.