This tutorial demonstrates building a WordPress plugin, "Vote Me," to add voting functionality to posts and display top-voted content.
Key Features:
voteme.php
plugin file handles core functionality, including AJAX integration via voteme.js
.Plugin Creation:
Create voteme.php
within your wp-content/plugins/voteme
directory. The plugin header should be:
<?php /* Plugin Name: Vote Me Plugin URI: [Your Plugin URI] Description: Adds voting to posts. Author: Abbas Version: 0.1 Author URI: [Your Author URI] */ define('VOTEMESURL', WP_PLUGIN_URL."/".dirname( plugin_basename( __FILE__ ) ) ); define('VOTEMEPATH', WP_PLUGIN_DIR."/".dirname( plugin_basename( __FILE__ ) ) );
Create a js
folder within voteme
and add voteme.js
. The plugin structure should resemble this:
Enqueue the scripts:
function voteme_enqueuescripts() { wp_enqueue_script('voteme', VOTEMESURL.'/js/voteme.js', array('jquery')); wp_localize_script( 'voteme', 'votemeajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); } add_action('wp_enqueue_scripts', 'voteme_enqueuescripts');
Activate the plugin in the WordPress admin panel.
Adding Vote Links:
Add a vote link to posts:
function voteme_getvotelink() { $votemelink = ""; if( get_option('votemelogincompulsory') != 'yes' || is_user_logged_in() ) { $post_ID = get_the_ID(); $votemecount = get_post_meta($post_ID, '_votemecount', true) != '' ? get_post_meta($post_ID, '_votemecount', true) : '0'; $link = $votemecount.' <a onclick="votemeaddvote('.$post_ID.');">Vote</a>'; $votemelink = '<div>' . $link . '</div>'; } else { $register_link = site_url('wp-login.php'); $votemelink = '<div><a href="' . $register_link . '">Vote</a></div>'; } return $votemelink; } function voteme_printvotelink($content) { return $content . voteme_getvotelink(); } add_filter('the_content', 'voteme_printvotelink');
This adds the vote count and link below each post.
AJAX Voting:
voteme.js
:
function votemeaddvote(postId) { jQuery.ajax({ type: 'POST', url: votemeajax.ajaxurl, data: { action: 'voteme_addvote', postid: postId }, success: function(data, textStatus, XMLHttpRequest) { var linkid = '#voteme-' + postId; jQuery(linkid).html(''); jQuery(linkid).append(data); }, error: function(MLHttpRequest, textStatus, errorThrown) { alert(errorThrown); } }); }
voteme.php
:
function voteme_addvote() { $results = ''; global $wpdb; $post_ID = $_POST['postid']; $votemecount = get_post_meta($post_ID, '_votemecount', true) != '' ? get_post_meta($post_ID, '_votemecount', true) : '0'; $votemecountNew = $votemecount + 1; update_post_meta($post_ID, '_votemecount', $votemecountNew); $results .= '<div>' . $votemecountNew . '</div>'; die($results); } add_action( 'wp_ajax_nopriv_voteme_addvote', 'voteme_addvote' ); add_action( 'wp_ajax_voteme_addvote', 'voteme_addvote' );
This handles the AJAX request to increment the vote count.
(The remaining sections detailing admin customization, sorting, user restriction, and widget creation are too extensive to include here. The provided text gives the complete code for each step. Please refer to the original input for the complete code snippets.)
The final sections cover adding a vote count column to the admin posts list, making it sortable, restricting voting to registered users via a settings page, and creating a widget to display the top-voted posts. All the necessary code is present in the original input. Remember to replace placeholder URIs with your own.
The above is the detailed content of Create a Voting Plugin for WordPress. For more information, please follow other related articles on the PHP Chinese website!