ホームページ >CMS チュートリアル >&#&プレス >WordPressの投票プラグインを作成します
このチュートリアルでは、WordPressプラグインの構築「投票」を示しています。
主要な機能:
voteme.php
voteme.js
投票の投稿:内で
フォルダーを作成し、を追加します。 プラグイン構造はこれに似ている必要があります:voteme.php
wp-content/plugins/voteme
<code class="language-php"><?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__ ) ) );</code>スクリプトをenqueueします:
js
voteme
WordPress管理パネルのプラグインをアクティブにします
voteme.js
<code class="language-php">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');</code>投稿への投票リンクを追加します:
これにより、各投稿の下に投票数とリンクが追加されます。
ajax投票:
<code class="language-php">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="'%20.%20%24register_link%20.%20'">Vote</a></div>'; } return $votemelink; } function voteme_printvotelink($content) { return $content . voteme_getvotelink(); } add_filter('the_content', 'voteme_printvotelink');</code>
:
これにより、ajaxリクエストが処理され、投票数が増えます。
voteme.js
<code class="language-javascript">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); } }); }</code>(管理者のカスタマイズ、並べ替え、ユーザー制限、ウィジェットの作成を詳細に説明する残りのセクションは、ここに含めるには広すぎます。提供されたテキストは、各ステップの完全なコードを提供します。スニペット。)
voteme.php
以上がWordPressの投票プラグインを作成しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。