
Creating a user reputation system in WordPress involves several steps, including creating a custom database table to store the reputation data, creating functions to manage user reputation, and potentially providing a way for users to earn or lose reputation points through interaction (like posting comments, liking content, etc.).
Here’s a simple example to get you started:
Step 1: Create a Custom Database Table
You need to set up a custom table in the WordPress database to store user reputation information. You can do this when your theme or plugin is activated.
Add the following code to your theme’s functions.php
file or within a custom plugin.
function create_user_reputation_table() { global $wpdb; $table_name = $wpdb->prefix . 'user_reputation'; // Custom table name // SQL to create the table $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( user_id BIGINT(20) UNSIGNED NOT NULL, reputation INT(11) NOT NULL DEFAULT 0, PRIMARY KEY (user_id) ) $charset_collate;"; // Include the WordPress file needed to run dbDelta require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); // Create the table dbDelta($sql); } // Hook the function to 'after_switch_theme' or 'plugins_loaded' for plugin add_action('after_switch_theme', 'create_user_reputation_table');
Step 2: Functions to Manage Reputation
Next, you need to create functions to add, subtract, and retrieve user reputation scores.
function get_user_reputation($user_id) { global $wpdb; $table_name = $wpdb->prefix . 'user_reputation'; $reputation = $wpdb->get_var($wpdb->prepare("SELECT reputation FROM $table_name WHERE user_id = %d", $user_id)); return $reputation ? $reputation : 0; // Returns 0 if no reputation found } function add_user_reputation($user_id, $points) { global $wpdb; $table_name = $wpdb->prefix . 'user_reputation'; // Check if user already has a reputation entry $current_reputation = get_user_reputation($user_id); if ($current_reputation === 0) { // If not, insert a new entry $wpdb->insert($table_name, [ 'user_id' => $user_id, 'reputation' => $points, ]); } else { // Update the existing reputation $wpdb->update($table_name, [ 'reputation' => $current_reputation + $points, ], [ 'user_id' => $user_id, ]); } } function subtract_user_reputation($user_id, $points) { global $wpdb; $table_name = $wpdb->prefix . 'user_reputation'; $current_reputation = get_user_reputation($user_id); if ($current_reputation > 0) { // Ensure reputation doesn't go negative $new_reputation = max(0, $current_reputation - $points); $wpdb->update($table_name, ['reputation' => $new_reputation], ['user_id' => $user_id]); } }
Step 3: Integrate Reputation in User Interface
You can display the user’s reputation on their profile or elsewhere on your site. For example, you can add the follower’s reputation to their profile page using a shortcode:
function display_user_reputation($atts) { $atts = shortcode_atts(['id' => get_current_user_id()], $atts); $user_id = intval($atts['id']); $reputation = get_user_reputation($user_id); return "Reputation: " . esc_html($reputation); } add_shortcode('user_reputation', 'display_user_reputation');
Step 4: Using the Reputation System
You can now manage user reputation with the following functions:
- Add Reputation:
add_user_reputation($user_id, $points);
- Subtract Reputation:
subtract_user_reputation($user_id, $points);
- Get Reputation:
get_user_reputation($user_id);
Example Usage:
You can use these functions in other parts of your WordPress theme or plugin as needed. For example, you can call add_user_reputation(1, 10);
to add 10 reputation points to the user with the ID of 1.
Conclusion
This is a basic implementation of a user reputation system in WordPress using PHP and MySQL. You can expand this functionality by connecting it to various user actions, such as liking a post, commenting, or posting, to award or deduct reputation points as appropriate.
Important Notes:
- Always test code changes on a staging site before deploying to a live site.
- Consider implementing additional features, like logging reputation changes or creating an admin interface for managing user reputations.