
內容簡介
WP-Polls 是一款能夠在您的網站上添加投票功能的外掛,使用者可透過簡碼或小工具輕鬆嵌入投票,並在不重新載入頁面的情況下查看結果。此外掛提供多種自訂選項,讓您能夠靈活地設計投票樣式。
【主要功能】
• 支援單選或多選投票
• 可設定投票截止日期
• 投票結果即時顯示
• 支援簡碼和小工具嵌入
• 自訂樣式可由佈景主題覆蓋
外掛標籤
開發者團隊
📦 歷史版本下載
原文外掛簡介
WP-Polls adds a poll to your site: a question, its answers, and a result bar that replaces the voting form once a visitor has voted, without a page reload. A poll can go in a post with a shortcode or a block, in a sidebar with the widget, or anywhere in your theme with a template tag.
A poll can accept one answer or several, close on a date you set, and be shown as a form or as its result. The markup of every part of it is a template you can edit, and the bars are styled with CSS custom properties your theme can override.
Donations
I spent most of my free time creating, updating, maintaining and supporting these plugins, if you really love my plugins and could spare me a couple of bucks, I will really appreciate it. If not feel free to use it without any obligations.
Usage
Showing A Poll From A Theme
Polls
To show specific poll, use where 2 is your poll id.
To show random poll, use
To embed a specific poll in your post, use [poll id="2"] where 2 is your poll id.
To embed a random poll in your post, use [poll id="-2"]
To embed a specific poll’s result in your post, use [poll id="2" type="result"] where 2 is your poll id.
Showing A Poll In A Block
Two blocks are available in the editor, under Widgets:
Poll — one poll, as its voting form or as its result. Set Poll ID in the sidebar, or leave it at zero for the current poll, which is what an empty [poll] does. Show chooses between the voting form and the result.
Polls Archive — every poll with its results, the same listing [page_polls] produces.
Both render on the server, so the block preview in the editor is the real poll rather than an approximation, and changing a poll updates every post showing it without re-saving anything.
The shortcodes still work and are not going anywhere. [poll], [poll=2] and [page_polls] behave exactly as they always have, and a post already containing one needs no change. The blocks call the same code the shortcodes call, so the two render identically — use whichever suits the post.
Showing A Poll In A Widget
Go to WP-Admin -> Appearance -> Widgets.
Add the Polls widget to a widget area. On block themes the widget is under the Legacy Widget block, or in Appearance -> Editor if your theme has no widget areas at all.
Set its title and which poll it shows, then save.
Scroll down for instructions on how to create a Polls Archive.
How To Add A Polls Archive?
Go to WP-Admin -> Pages -> Add New.
Type any title you like in the post’s title area.
If you ARE using nice permalinks, after typing the title, WordPress will generate the permalink to the page. You will see an ‘Edit’ link just beside the permalink.
Click ‘Edit’ and type in pollsarchive in the text field and click ‘Save’.
Type [page_polls] in the post’s content area.
Click ‘Publish’.
If you ARE NOT using nice permalinks, go to WP-Admin -> Polls -> Settings and fill in Poll Archive URL, under the Archive heading, with the URL of the Polls Archive page you created above.
To Display Total Polls
To Display Total Poll Answers
To Display Total Poll Votes
To Display Poll Votes by ID
To Display Total Poll Voters
To Display Poll Time by ID and date format
WP-CLI
wp polls list
wp polls list --status=open
wp polls get 3
wp polls open 3
wp polls close 3
wp polls delete 3 --yes
wp polls list --status=open --format=ids | xargs -n1 wp polls close closes every open poll one at a time.
REST API
GET /wp-json/polls/v1/poll/
GET /wp-json/polls/v1/poll/
POST /wp-json/polls/v1/poll/
Reading is public, because a poll is public. Voting takes the same poll_
Each response carries the rendered markup as well as the numbers, because your templates decide what a poll looks like and a client that rebuilt the markup itself would ignore them.
A refusal answers 403, not 400 — a closed poll, a vote already cast, a bad nonce, nothing selected. 400 is kept for a parameter this plugin never had a chance to look at, and a poll that does not exist is 404. So a 403 means the request was understood and the answer is no, and sending it again differently will not help.
These routes are an addition. The admin-ajax.php polls action is unchanged and still supported.
Translating the template
The plugin templates can be translated via template variables.
There are these filters for the custom template variables
wp_polls_template_voteheader_variables
wp_polls_template_votebody_variables
wp_polls_template_votefooter_variables
wp_polls_template_resultheader_variables
wp_polls_template_resultbody_variables
wp_polls_template_resultfooter_variables
The Result Body (Voted) and Result Footer (Voted) templates are filtered by
wp_polls_template_resultbody_variables and wp_polls_template_resultfooter_variables
too – the variables are shared, only the markup filters are separate.
Add filter to your theme and register custom variable where you will add your translation.
Good practice is to name them for example with prefix STR_ in the example STR_TOTAL_VOTERS.
/**
* Localize wp_polls_template_resultfooter_variables.
*
* @param array $variables An array of template variables.
* @return array $variables Modified template variables.
*/
function wp_polls_template_resultfooter_variables( $variables ) {
// Add strings.
$variables['%STR_TOTAL_VOTERS%'] = __( 'Total voters', 'theme-textdomain' );
return $variables;
}
// Trigger the filter
add_filter( 'wp_polls_template_resultfooter_variables', 'wp_polls_template_resultfooter_variables' , 10, 1 );
In the admin side just call the custom variable like so and the variable has been translated in the front-end.
%STR_TOTAL_VOTERS%’
