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

>本教程演示了構建一個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
您可以使用WordPress構建會員網站嗎?您可以使用WordPress構建會員網站嗎?May 01, 2025 am 12:08 AM

是的,您可以使用Plypluginslikememberpress,PayMembersubScriptions,OrwooCommerceForuserManagemention,ContentAccesControl,andPaymentMenthandling.2)

WordPress是否需要編碼知識作為CMS?WordPress是否需要編碼知識作為CMS?Apr 30, 2025 am 12:03 AM

你不需要編程知識就能使用WordPress,但掌握編程可以提升體驗。 1)使用CSS和HTML可以調整主題樣式。 2)PHP知識能編輯主題文件,添加功能。 3)自定義插件和元標籤可優化SEO。 4)注意備份和使用子主題以防更新問題。

使用WordPress時的安全考慮是什麼?使用WordPress時的安全考慮是什麼?Apr 29, 2025 am 12:01 AM

TosecureaWordPresssite,followthesesteps:1)RegularlyupdateWordPresscore,themes,andpluginstopatchvulnerabilities.2)Usestrong,uniquepasswordsandenabletwo-factorauthentication.3)OptformanagedWordPresshostingorsecuresharedhostingwithawebapplicationfirewal

WordPress與其他網站構建者相比如何?WordPress與其他網站構建者相比如何?Apr 28, 2025 am 12:04 AM

WordPressExcelSoverotherWeberSiteBuilderSduetoItsflexible,可伸縮性,andopen-sourcenature.1)它'saversatilecmswithExtEnsextEnsexenSiveCustomizedOptionsVIATHEMESANDPLUGINS.2)它的alllearbutoffersbutoffersbutoffersbutoffersbutefersbuterbutfulcontrololoncemastered.3)

5個WordPress插件,供開發人員在2025年使用5個WordPress插件,供開發人員在2025年使用Apr 27, 2025 am 08:25 AM

2025年網站開發的七個必備WordPress插件 在2025年建立頂級WordPress網站需要速度,響應能力和可擴展性。 實現這種有效的實現通常取決於戰略插件的選擇。 這篇文章Highlig

您將使用WordPress做什麼?您將使用WordPress做什麼?Apr 27, 2025 am 12:14 AM

WordPresscanbeusedforvariouspurposesbeyondblogging.1)E-commerce:WithWooCommerce,itcanbecomeafullonlinestore.2)Membershipsites:PluginslikeMemberPressenableexclusivecontentareas.3)Portfoliosites:ThemeslikeAstraallowstunninglayouts.Ensuretomanageplugins

WordPress適合創建投資組合網站嗎?WordPress適合創建投資組合網站嗎?Apr 26, 2025 am 12:05 AM

是的,wordpressisisexcellentforcortingaportfoliowebsite.1)itoffersnumeroversnumeroverportfolio-spificthemeslike'astra'astra'astra'astra'astra'astra'astra'astra'astra'elementor'Enelementor'enableIntiviveSiveSign,Thoughtemanycanslowthesite.3)

使用WordPress而不是從頭開始編碼網站的優點是什麼?使用WordPress而不是從頭開始編碼網站的優點是什麼?Apr 25, 2025 am 12:16 AM

WordPressisadvantageousovercodingawebsitefromscratchdueto:1)easeofuseandfasterdevelopment,2)flexibilityandscalability,3)strongcommunitysupport,4)built-inSEOandmarketingtools,5)cost-effectiveness,and6)regularsecurityupdates.Thesefeaturesallowforquicke

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。