Skip to content

Commit

Permalink
Use an option "archive_author" to specify the author
Browse files Browse the repository at this point in the history
  • Loading branch information
leedxw committed Dec 16, 2024
1 parent d5733b4 commit 1d79929
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
16 changes: 14 additions & 2 deletions lib/set-archive-author.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

// Set the post_author to be "2"
// Find a username specified in the option "archive_author"
// Set the post_author (in $data) to be the id corresponding to the user

function set_archive_author($data, $postarr)
{
Expand All @@ -10,7 +11,18 @@ function set_archive_author($data, $postarr)
return $data;
}

$data['post_author'] = 2;
$archive_username = get_option('archive_author');

if ( !empty($archive_author) ) {
if (is_int($archive_author)) {
$archive_author_id = $archive_author;
} else {
$archive_author_id = get_user_by('id', $archive_author);
}
if ( !empty($archive_author_id) ) {
$data['post_author'] = $archive_author_id;
}
}

return $data;
}
15 changes: 7 additions & 8 deletions templates/entry-meta.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<div class="govuk-body-s"><?php

// If the post is owned by a deleted user, set it to the archive user
$tmp_author_id = get_post_field('post_author', $post->ID);
if ($tmp_author_id > 2) {
$tmp_user = get_user_by('id', $tmp_author_id);
if (empty($tmp_user->user_login)) {
error_log("post {$post->ID} has deleted user $tmp_author_id", 0);
add_filter('wp_insert_post_data', 'set_archive_author', '99', 2);
wp_update_post($post);
// If the post is owned by a deleted user, patch it to the archive user
$check_author_id = get_post_field('post_author', $post->ID);
if ($check_author_id > 1) {
$check_user = get_user_by('id', $check_author_id);
if (empty($check_user->user_login)) {
error_log("author of post {$post->ID} is deleted user $check_author_id", 0);
add_filter('wp_insert_post_data', 'set_archive_author', 99, 2);
}
}

Expand Down

0 comments on commit 1d79929

Please sign in to comment.