在WordPress中添加在线投票功能可以通过安装插件或使用内置功能实现,可以通过WordPress插件市场搜索并安装支持投票功能的插件,投票插件”或“节日投票插件”,安装后,通常会在WordPress后台管理界面上提供简单的设置选项,用户可以轻松创建和管理投票活动,WordPress也支持使用 shortcode 功能在文章或页面中添加投票链接,只需在输入框中插入相应的代码即可,这些插件和短代码提供了丰富的投票选项和互动性,使用户能够方便地参与到在线调查、问卷或任何需要集体决策的活动中。
随着互联网的快速发展,越来越多的网站和博客开始使用WordPress作为其内容管理系统,而在这些网站上,为用户提供互动性的元素变得尤为重要,在线投票功能不仅可以增加用户参与度,还有助于收集意见、调查结果等,本文将详细介绍如何在WordPress中添加在线投票功能。
为什么要在WordPress中添加在线投票功能?
在线投票功能能够带来诸多好处,它可以让你的网站内容更加丰富多样,提高用户的参与度,通过投票收集到的数据可以为你的网站运营者提供宝贵的参考信息,如受众喜好、热门话题等,在线投票功能还具有很高的互动性,能够让用户在享受投票乐趣的同时,加深对网站内容的了解。
在WordPress中添加在线投票功能的步骤
使用插件
WordPress插件市场中有许多优质的在线投票插件,如VotePress、Polls and Surveys等,以下以VotePress为例,介绍如何在WordPress中添加在线投票功能:
-
登录到你的WordPress管理后台;
-
在左侧菜单栏中找到“插件”,点击“添加新”;
-
在搜索框中输入“VotePress”,找到相关插件并点击“安装现在”;
-
安装完成后,点击“激活”按钮启用插件;
-
在WordPress后台的“投票Press”选项卡下,你可以看到投票创建的界面,根据提示填写投票题目、选项等内容;
-
创建完成后,你可以将投票链接分享到社交媒体或者直接嵌入到你的网站内容中。
自定义代码实现
如果你对PHP编程有一定了解,还可以选择自定义代码来实现在线投票功能,以下是一个简单的示例:
-
在WordPress主题文件夹中创建一个新的PHP文件,如
投票插件.php; -
复制以下代码粘贴到新创建的文件中,并根据自己的需求进行修改:
function create_poll($title, $poll_items, $poll_interval = 60) {
?>
<form method="post" action="<?php echo esc_url( home_url() ); ?>":
?>
<?php wp_setnonce_field('poll nonsion'); ?>
<label for="poll_title"><?php _e( 'Poll Title' ); ?></label>
<input type="text" name="poll_title" id="poll_title" value="<?php the_field( 'poll_title' ); ?>" required />
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<p>
<?php the_title( '<h4 class="poll-title">' , '</h4>' ); ?>
<?php the_field( 'poll_options[]' ); ?>
<?php
$poll_items[] = array(
'label' => get_the_title(),
'url' => the_field( 'poll_link' ),
'poll_option' => get_the_field( 'poll_option' )
);
?>
</p>
<?php endwhile; endif;
?>
<?php
foreach ($poll_items as $item):
$url = esc_url( $item['url'] );
$option = esc_attr( $item['poll_option'] );
$class = in_array( 'poll-item-' . $poll_item['poll_option'], get_field( 'poll_options[]' ) ) ? 'poll-item active' : '';
?>
<div id="poll-container-<?php the_hash_code( $poll_item['poll_item_id'] ) ?>" class="poll-container <?php echo esc_attr( $class ); ?>">
<input type="radio" name="polls[]" value="<?php echo esc_attr( $poll_item['poll_item_id'] ); ?>" <?php checked( true, in_array( $poll_item['poll_item_id'], get_field( 'poll_items' ) ) ); ?>><?php the_field( 'poll_option' ); ?></input>
<a href="<?php echo esc_url( $url ); ?>"><?php the_field( 'poll_option' ); ?></a>
</div>
<?php
foreach ($poll_items as $item):
$url = esc_url( $item['url'] );
$option = esc_attr( $item['poll_option'] );
$class = in_array( 'poll-item-' . $poll_item['poll_item_id'], get_field( 'poll_items' ) ) ? 'poll-item active' : '';
?>
<div id="poll-container-<?php the_hash_code( $poll_item['poll_item_id'] ) ?>" class="poll-container <?php echo esc_attr( $class ); ?>">
<input type="radio" name="polls[]" value="<?php echo esc_attr( $poll_item['poll_item_id'] ); ?>" <?php checked( true, in_array( $poll_item['poll_item_id'], get_field( 'poll_items' ) ) ); ?>><?php the_field( 'poll_title' ); ?></input>
<a href="<?php echo esc_url( $url ); ?>"><?php the_field( 'poll_title' ); ?></a>
</div>
<?php
foreach ($poll_items as $item):
$url = esc_url( $item['url'] );
$option = esc_attr( $item['poll_option'] );
$class = in_array( 'poll-item-' . $poll_item['poll_item_id'], get_field( 'poll_items' ) ) ? 'poll-item active' : '';
?>
<div id="poll-container-<?php the_hash_code( $poll_item['poll_item_id'] ) ?>" class="poll-container <?php echo esc_attr( $class ); ?>">
<input type="radio" name="polls[]" value="<?php echo esc_attr( $poll_item['poll_item_id'] ); ?>" <?php checked( true, in_array( $poll_item['poll_item_id'], get_field( 'poll_items' ) ) ); ?>><?php the_field( 'poll_title' ); ?></input>
<a href="<?php echo esc_url( $url ); ?>"><?php the_field( 'poll_title' ); ?></a>
</div>
<?php
wp_reset_postdata();
?>
<input type="submit" value="<?php _e( 'Submit' ); ?>" />
</form>
<?php
}
add_action( 'admin_menu', 'create_poll' );
add_action( 'wp_ajax_start_poll', 'start_poll' );
add_action( 'wp_ajax_get_polls', 'get_polls' );
function start_poll() {
global $wpdb;
$id = intval($_POST['poll_id']);
if (!$id) wp_die('Invalid poll ID.');
$poll = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}polls WHERE id={$id}");
wp_remote_post('https://www.google.com/poll/submit.php', array(
'method' => 'POST',
'body' => array(
'kiosk' => $poll[0]->title,
'question' => $poll[0]->question,
'poll' => $id,
'choices[]' => join(', ', $poll[0]->options),
'custom_key' => uniqid(),
'action' => 'polls/create'
)
));
return new WP_Error('Poll started successfully.');
}
function get_polls() {
$polls = array();
$polls[] = array('poll_id' => 1);
return $polls;
}
将上述代码添加到你的主题的functions.php文件中,并在WordPress后台的“投票插件”页面中激活该插件即可。
在WordPress中添加在线投票功能,可以通过安装插件或自定义代码两种方法实现,无论是使用现有的插件还是编写自定义代码,都能让你的网站具备更强的互动性和用户参与度,希望本文能对你有所帮助,让你的WordPress网站焕发出新的活力!


还没有评论,来说两句吧...