Below is the updated full main plugin file (nunsa-vote.php) for your NUNSA Vote WordPress plugin. This version incorporates all the changes and improvements from our previous discussions, including:
Proper enqueuing of admin styles (using a separate enqueue_admin_assets method hooked to admin_enqueue_scripts).
All dependencies, hooks, and initializations are included for a complete, functional plugin.
Based on the latest structure, this file ensures the plugin works seamlessly with the other components (e.g., frontend, admin pages, AJAX handling).
Key Updates in This File:
Added enqueue_admin_assets method to load admin-style.css only in the admin area.
Ensured all hooks and actions are correctly defined.
Maintained the navy blue and gold color scheme in the design.
Included session start for voter authentication.
Here's the complete code for nunsa-vote.php:
php
<?php
/*
Plugin Name: NUNSA Vote
Description: A custom voting plugin for NUNSA, allowing class-based voting, unique codes, and categorized contestants.
Version: 1.0
Author: Your Name
Text Domain: nunsa-vote
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly.
}
define('NV_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('NV_PLUGIN_URL', plugin_dir_url(__FILE__));
class NUNSAVote {
public function __construct() {
$this->load_dependencies();
$this->define_hooks();
}
private function load_dependencies() {
require_once NV_PLUGIN_DIR . 'includes/class-nv-database.php';
require_once NV_PLUGIN_DIR . 'includes/class-nv-cpt.php';
require_once NV_PLUGIN_DIR . 'includes/class-nv-voters.php';
require_once NV_PLUGIN_DIR . 'includes/class-nv-frontend.php';
require_once NV_PLUGIN_DIR . 'includes/class-nv-voting-ajax.php';
require_once NV_PLUGIN_DIR . 'includes/class-nv-settings.php';
}
private function define_hooks() {
register_activation_hook(__FILE__, array('NV_Database', 'install'));
add_action('init', array('NV_CPT', 'register_post_types_and_taxonomies'));
add_action('add_meta_boxes', array('NV_CPT', 'add_election_metaboxes'));
add_action('save_post_nunsa_election', array('NV_CPT', 'save_election_meta'));
add_action('admin_menu', array($this, 'add_admin_menus'));
add_action('wp_enqueue_scripts', array($this, 'enqueue_frontend_assets'));
add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_assets')); // Hook for admin-specific assets
add_action('init', 'start_session'); // Start PHP session for voter authentication
}
public function add_admin_menus() {
add_menu_page('NUNSA Vote', 'NUNSA Vote', 'manage_options', 'nunsa-vote', array('NV_Settings', 'display_dashboard'), 'dashicons-megaphone', 6);
add_submenu_page('nunsa-vote', 'Manage Elections', 'Elections', 'manage_options', 'edit.php?post_type=nunsa_election');
add_submenu_page('nunsa-vote', 'Manage Contestants', 'Contestants', 'manage_options', 'edit.php?post_type=nunsa_contestant');
add_submenu_page('nunsa-vote', 'Manage Categories', 'Categories', 'manage_options', 'edit-tags.php?taxonomy=nunsa_category');
add_submenu_page('nunsa-vote', 'Manage Classes', 'Classes', 'manage_options', 'edit-tags.php?taxonomy=nunsa_class');
add_submenu_page('nunsa-vote', 'Manage Voters', 'Voters', 'manage_options', 'nv-voters', array('NV_Voters', 'voters_page'));
add_submenu_page('nunsa-vote', 'Settings', 'Settings', 'manage_options', 'nv-settings', array('NV_Settings', 'display_settings_page'));
}
public function enqueue_frontend_assets() {
// Enqueue frontend styles and scripts
wp_enqueue_style('nunsa-vote-frontend', NV_PLUGIN_URL . 'assets/css/frontend-style.css', array(), '1.0');
wp_enqueue_script('nunsa-vote-js', NV_PLUGIN_URL . 'assets/js/voting-frontend.js', array('jquery'), '1.0', true);
// Localize script for AJAX calls
wp_localize_script('nunsa-vote-js', 'nv_ajax_object', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('nv_voting_nonce')
));
}
public function enqueue_admin_assets() {
// Enqueue admin-specific styles
wp_enqueue_style('nunsa-vote-admin', NV_PLUGIN_URL . 'assets/css/admin-style.css', array(), '1.0');
}
}
// Function to start PHP session
function start_session() {
if (!session_id()) {
session_start();
}
}
// Initialize the plugin
if (class_exists('NUNSAVote')) {
new NUNSAVote();
}
How to Use This File:
Replace the Existing File: Save this code into your nunsa-vote.php file in the root of your plugin directory (wp-content/plugins/nunsa-vote/).
Ensure Dependencies Exist: Make sure all the included files (e.g., includes/class-nv-*.php, assets/css/admin-style.css, etc.) are present in their respective directories, as provided in earlier responses.
Test the Plugin: After updating, activate the plugin in WordPress and verify that:
Admin pages load with the custom styles.
Frontend voting works as expected.
Uninstallation cleans up data (test by deactivating and deleting the plugin).
This file is now complete and should work perfectly with the rest of the plugin. If you need any other files or further modifications, just let me know! 😊