Home / Admin / Enhanced Force Author Plugin
Duplicate Snippet

Embed Snippet on Your Site

Enhanced Force Author Plugin

Forces a specific user to be the author of all posts and pages while maintaining the original creator in post meta for reference.

<10
Code Preview
php
<?php
<?php
/**
 * Enhanced Force Author Plugin
 * 
 * Forces a specific user to be the author of all posts and pages while maintaining
 * the original creator in post meta for reference.
 */
class Force_Author_Handler {
    private $forced_author_id;
    private $post_types = ['post', 'page'];
    private $options_name = 'force_author_settings';
    
    public function __construct() {
        // Get settings
        $options = get_option($this->options_name);
        $this->forced_author_id = isset($options['forced_author_id']) ? intval($options['forced_author_id']) : 29;
        
        // Initialize filters and actions
        add_filter('wp_insert_post_data', [$this, 'force_author_on_save'], 99, 2);
        add_action('post_updated', [$this, 'store_original_author'], 10, 3);
        add_filter('manage_posts_columns', [$this, 'add_original_author_column']);
        add_filter('manage_pages_columns', [$this, 'add_original_author_column']);
        add_action('manage_posts_custom_column', [$this, 'show_original_author_column'], 10, 2);
        add_action('manage_pages_custom_column', [$this, 'show_original_author_column'], 10, 2);
        
        // Add settings
        add_action('admin_menu', [$this, 'add_settings_page']);
        add_action('admin_init', [$this, 'register_settings']);
    }
    /**
     * Force the author when saving/updating content
     */
    public function force_author_on_save($data, $postarr) {
        if (!in_array($data['post_type'], $this->post_types)) {
            return $data;
        }
        $author = get_user_by('id', $this->forced_author_id);
        if (!$author) {
            error_log('Forced author ID ' . $this->forced_author_id . ' does not exist');
            return $data;
        }
        if (empty($postarr['ID']) && !empty($data['post_author'])) {
            $this->store_original_author($postarr['ID'], null, $data);
        }
        $data['post_author'] = $this->forced_author_id;
        return $data;
    }
    /**
     * Store the original author in post meta
     */
    public function store_original_author($post_id, $post_before, $post_after) {
        if (!in_array(get_post_type($post_id), $this->post_types)) {
            return;
        }
        if (!get_post_meta($post_id, '_original_author', true)) {
            $original_author = $post_before ? $post_before->post_author : get_current_user_id();
            update_post_meta($post_id, '_original_author', $original_author);
            update_post_meta($post_id, '_original_author_date', current_time('mysql'));
        }
    }
    /**
     * Add original author column to admin
     */
    public function add_original_author_column($columns) {
        $columns['original_author'] = 'Original Author';
        return $columns;
    }
    /**
     * Display original author in admin column
     */
    public function show_original_author_column($column_name, $post_id) {
        if ($column_name !== 'original_author') {
            return;
        }
        $original_author_id = get_post_meta($post_id, '_original_author', true);
        if ($original_author_id) {
            $original_author = get_user_by('id', $original_author_id);
            if ($original_author) {
                echo esc_html($original_author->display_name);
                $date = get_post_meta($post_id, '_original_author_date', true);
                if ($date) {
                    echo '<br><small>' . esc_html(date('M j, Y', strtotime($date))) . '</small>';
                }
            }
        } else {
            echo '—';
        }
    }
    /**
     * Register plugin settings
     */
    public function register_settings() {
        register_setting(
            $this->options_name,
            $this->options_name,
            [$this, 'sanitize_settings']
        );
        add_settings_section(
            'force_author_main_section',
            'Main Settings',
            function() { echo '<p>Configure the forced author settings below:</p>'; },
            'force-author-settings'
        );
        add_settings_field(
            'forced_author_id',
            'Forced Author',
            [$this, 'author_dropdown_field'],
            'force-author-settings',
            'force_author_main_section'
        );
        add_settings_field(
            'post_types',
            'Apply to Post Types',
            [$this, 'post_types_field'],
            'force-author-settings',
            'force_author_main_section'
        );
    }
    /**
     * Create author dropdown field
     */
    public function author_dropdown_field() {
        $options = get_option($this->options_name);
        $current_author = isset($options['forced_author_id']) ? $options['forced_author_id'] : $this->forced_author_id;
        
        // Get all users who can publish posts
        $users = get_users([
            'who' => 'authors',
            'orderby' => 'display_name',
            'order' => 'ASC'
        ]);
        echo '<select name="' . esc_attr($this->options_name) . '[forced_author_id]" class="regular-text">';
        foreach ($users as $user) {
            echo '<option value="' . esc_attr($user->ID) . '" ' . selected($current_author, $user->ID, false) . '>';
            echo esc_html($user->display_name) . ' (' . esc_html($user->user_login) . ')';
            echo '</option>';
        }
        echo '</select>';
    }
    /**
     * Create post types field
     */
    public function post_types_field() {
        $options = get_option($this->options_name);
        $selected_types = isset($options['post_types']) ? $options['post_types'] : $this->post_types;
        
        // Get all public post types
        $post_types = get_post_types(['public' => true], 'objects');
        
        foreach ($post_types as $post_type) {
            echo '<label style="display: block; margin-bottom: 5px;">';
            echo '<input type="checkbox" name="' . esc_attr($this->options_name) . '[post_types][]" value="' . 
                 esc_attr($post_type->name) . '" ' . 
                 checked(in_array($post_type->name, $selected_types), true, false) . '>';
            echo ' ' . esc_html($post_type->labels->name);
            echo '</label>';
        }
    }
    /**
     * Sanitize settings
     */
    public function sanitize_settings($input) {
        $sanitized = [];
        
        // Sanitize forced author ID
        $sanitized['forced_author_id'] = isset($input['forced_author_id']) ? 
            intval($input['forced_author_id']) : $this->forced_author_id;
        // Sanitize post types
        $sanitized['post_types'] = isset($input['post_types']) && is_array($input['post_types']) ? 
            array_map('sanitize_key', $input['post_types']) : $this->post_types;
        return $sanitized;
    }
    /**
     * Add settings page to admin menu
     */
    public function add_settings_page() {
        add_options_page(
            'Force Author Settings',
            'Force Author',
            'manage_options',
            'force-author-settings',
            [$this, 'render_settings_page']
        );
    }
    /**
     * Render settings page
     */
    public function render_settings_page() {
        if (!current_user_can('manage_options')) {
            return;
        }
        ?>
        <div class="wrap">
            <h1><?php echo esc_html(get_admin_page_title()); ?></h1>
            
            <?php
            if (isset($_GET['settings-updated'])) {
                add_settings_error(
                    'force_author_messages',
                    'force_author_message',
                    'Settings Saved',
                    'updated'
                );
            }
            
            settings_errors('force_author_messages');
            ?>
            <form action="options.php" method="post">
                <?php
                settings_fields($this->options_name);
                do_settings_sections('force-author-settings');
                submit_button('Save Settings');
                ?>
            </form>
            <div class="card" style="max-width: 800px; margin-top: 20px; padding: 10px 20px;">
                <h3>About This Plugin</h3>
                <p>This plugin forces all selected content types to show a specific author, while maintaining the original author information in the database.</p>
                <p>The original author information is visible in the posts/pages list under the "Original Author" column.</p>
            </div>
        </div>
        <?php
    }
}
// Initialize the handler
add_action('init', function() {
    new Force_Author_Handler();
});

Comments

Add a Comment