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:
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');
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]); } }
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');
You can now manage user reputation with the following functions:
add_user_reputation($user_id, $points);
subtract_user_reputation($user_id, $points);
get_user_reputation($user_id);
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.
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.
WordPress development has evolved significantly, and modern tooling plays a crucial role in creating efficient…
I. Project Overview The goal is to automate the process of notifying search engines (like…
1. Database Structure (MySQL) We'll need a database table to store information about our website's…
This explanation aims to provide a solid foundation for understanding the process and implementing your…
Okay, here's a comprehensive guide on building a real-time website chat script using PHP, HTML,…
Comprehensive guide on creating a simple website analytics system using PHP, HTML, CSS, JavaScript, and…