Artikellinks aus WordPress in Linkwarden speichern

Publiziert am:

Zuletzt editiert:

Ein kurzer Snippet zum Ausprobieren. Benötigt WordPress und Linkwarden. Damit werden alle Links, die sich in einem Artikel bestimmter Kategorien befinden, beim Veröffentlichen zum Speichern an Linkwarden geschickt. Linkwarden schickt diese dann auch an archive.org, damit sie auch öffentlich verfügbar bleiben.

Kontext dazu: Mein Mastodon‑Post hier, und dieser Bluesky‑Post.

// Hooks into the WordPress publish_post action.
// When a post is published, it triggers the send_post_links_to_linkwarden function.
add_action('publish_post', 'send_post_links_to_linkwarden', 10, 2);

function send_post_links_to_linkwarden($post_ID, $post) {
// Only proceed if the post is in one of these categories.
$preferred_categories = ['CHANGE_ME', 'CHANGE_ME_TOO'];

// Set post_title
$post_title = sanitize_text_field($post->post_title);


// Get the category slugs of the published post.
$post_categories = wp_get_post_categories($post_ID, ['fields' => 'slugs']);

// Skip processing if the post doesn't belong to one of the preferred categories.
if (empty(array_intersect($preferred_categories, $post_categories))) {
error_log("[Linkwarden] Skipping post ID $post_ID because it is not in preferred categories.");
return;
}

// Define the Linkwarden API base URL and access token.
$api_base_url = ' https://YOUR_LINKWARDEN_URL/api/v1';
$api_token = 'YOUR_API_TOKEN';

// Extract all URLs from the post content.
$content = $post->post_content;
$urls = wp_extract_urls($content);

// Exit if no URLs were found in the post.
if (empty($urls)) {
error_log("[Linkwarden] No URLs found in post ID $post_ID");
return;
}

// Remove duplicate URLs.
$urls = array_unique($urls);

// Send each URL found in the post to the Linkwarden API.
foreach ($urls as $url) {
// Create the body of the request with the URL, post title as title and tag.
$body = [
'url' => $url,
'title' => $post->post_title,
'tags' => [ ['name'=> $post_title] ],
'collectionId' => COLLECTION_ID_NUMBER_NO_QUOTES_FOR_ME_PLEASE,
];

// Make a POST request to the Linkwarden /links endpoint.
$response = wp_remote_post("{$api_base_url}/links", [
'headers' => [
'Authorization' => 'Bearer ' . $api_token,
'Content-Type' => 'application/json',
],
'body' => wp_json_encode($body),
'timeout' => 10,
]);

// Log an error if the HTTP request fails.
if (is_wp_error($response)) {
error_log("[Linkwarden] HTTP request error for URL $url: " . $response->get_error_message());
continue;
}

// Check the response code to confirm successful addition.
$code = wp_remote_retrieve_response_code($response);
if ($code !== 201 && $code !== 200) {
error_log("[Linkwarden] Unexpected response code $code for URL $url. Response: " . wp_remote_retrieve_body($response));
} else {
error_log("[Linkwarden] Successfully added URL $url from post ID $post_ID");
}
}
}

Kommentare

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert