このチュートリアルでは、WordPressプラグインの構築「投票」を示しています。
主要な機能:
voteme.php
voteme.js
投票の投稿:内で
フォルダーを作成し、を追加します。 プラグイン構造はこれに似ている必要があります: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
voteme
WordPress管理パネルのプラグインをアクティブにします
voteme.js
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');
これにより、各投稿の下に投票数とリンクが追加されます。
ajax投票:
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');
:
これにより、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); } }); }
voteme.php
以上がWordPressの投票プラグインを作成しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。