首頁 > CMS教程 > &#&按 > 為WordPress創建投票插件

為WordPress創建投票插件

William Shakespeare
發布: 2025-02-21 09:34:11
原創
592 人瀏覽過

>本教程演示了構建一個WordPress插件,“投票我”,以在帖子中添加投票功能並顯示最佳的內容。

密鑰功能:

  • >>自定義投票插件:avoteme.php插件文件處理核心功能,包括通過voteme.js>。
  • 帖子投票:每個帖子下面的投票鏈接使用ajax動態更新投票。
  • 管理面板集成: WordPress管理員顯示和按票數進行分類。
  • 註冊的用戶限制:
  • 投票僅限於註冊,已登錄用戶以防止垃圾郵件。
  • 頂級投票的帖子窗口小部件:
  • 可自定義的小部件展示最受歡迎的帖子。
  • 插件創建:

>創建在您的

目錄中。 插件標題應為:

voteme.php wp-content/plugins/voteme

中創建
<?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__ ) ) );
登入後複製
文件夾,然後添加

。 插件結構應類似於以下方式:js votemevoteme.js

加入腳本:Create a Voting Plugin for WordPress

激活WordPress Admin面板中的插件。

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');
登入後複製

Create a Voting Plugin for WordPress 添加投票鏈接:

>向帖子添加投票鏈接:

這添加了投票數,並在每個帖子下面鏈接。

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');
登入後複製

Create a Voting Plugin for WordPress ajax投票:

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);
        }
    });
}
登入後複製

這將處理AJAX請求以增加投票數。 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' );
登入後複製

(其餘部分詳細詳細介紹管理員自定義,分類,用戶限制和窗口小部件的創建太多了,以至於在此處包含。提供的文本為每個步驟提供完整的代碼。請參閱完整代碼的原始輸入摘要。)

Create a Voting Plugin for WordPress

>最終部分涵蓋了在管理員帖子列表中添加投票計數列,使其可排序,通過設置頁面將投票限制為註冊用戶,並創建一個小部件以顯示頂級投票的帖子。 原始輸入中存在所有必要的代碼。 切記用自己的佔位符烏里斯替換佔位符。

以上是為WordPress創建投票插件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板