WP-Polls Shortcodes

Below, you’ll find a detailed guide on how to add the WP-Polls Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the WP-Polls Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the WP-Polls Plugin and the shortcodes it provides:

Plugin Icon
WP-Polls

"WP-Polls is a dynamic WordPress plugin that allows you to easily create and manage polls on your website. With its user-friendly interface, engaging with your audience has never been easier."

★★★★✩ (134) Active Installs: 60000+ Tested with: 6.3.2 PHP Version: false
Included Shortcodes:
  • [page_polls]
  • [poll]

WP-Polls [page_polls] Shortcode

The ‘page_polls’ shortcode from the wp-polls plugin is designed to display a poll archive on your page. This shortcode calls the ‘polls_archive()’ function, which retrieves and displays all previous polls.

Shortcode: [page_polls]

Examples and Usage

Basic example – Displays the polls archive on a page using the shortcode.

[page_polls]

PHP Function Code

In case you have difficulties debugging what causing issues with [page_polls] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('page_polls', 'poll_page_shortcode');

Shortcode PHP function:

function poll_page_shortcode($atts) {
	return polls_archive();
}

Code file location:

wp-polls/wp-polls/wp-polls.php

WP-Polls [poll] Shortcode

The WP-Polls shortcode is a versatile tool that allows users to embed polls in their posts. The ‘poll’ shortcode takes two parameters: ‘id’ and ‘type’. The ‘id’ parameter specifies the poll to be displayed while the ‘type’ parameter determines whether the poll is for voting or displaying results. If ‘type’ is set to ‘vote’, the poll with the specified ‘id’ is rendered for voting. If ‘type’ is set to ‘result’, the results of the poll are displayed. In case the shortcode is used in a feed, a message is displayed asking users to visit the site to participate in the poll.

Shortcode: [poll]

Parameters

Here is a list of all possible poll shortcode parameters and attributes:

  • id – Unique identifier of the specific poll.
  • type – Determines the poll’s display style: ‘vote’ for voting interface, ‘result’ for showing results.

Examples and Usage

Basic example – Display a voting poll with the ID of 1.

[poll id=1 type=vote /]

Advanced examples

Display a poll result by referencing the poll’s ID. If the poll with the specified ID can’t be found, the shortcode will return a note indicating that there’s a poll embedded within the post and the user needs to visit the site to participate.

[poll id=2 type=result /]

Display a voting poll by referencing the poll’s ID. If the poll with the specified ID can’t be found, the shortcode will return a note indicating that there’s a poll embedded within the post and the user needs to visit the site to participate. In this example, the ID is wrapped in quotes, but the function will trim these off and convert the ID to an integer.

[poll "3" type=vote /]

PHP Function Code

In case you have difficulties debugging what causing issues with [poll] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'poll', 'poll_shortcode' );

Shortcode PHP function:

function poll_shortcode( $atts ) {
	$attributes = shortcode_atts( array( 'id' => 0, 'type' => 'vote' ), $atts );
	if( ! is_feed() ) {
		$id = (int) $attributes['id'];

		// To maintain backward compatibility with [poll=1]. Props @tz-ua
		if( ! $id && isset( $atts[0] ) ) {
			$id = (int) trim( $atts[0], '="\'' );
		}

		if( $attributes['type'] === 'vote' ) {
			return get_poll( $id, false );
		} elseif( $attributes['type'] === 'result' ) {
			return display_pollresult( $id );
		}
	} else {
		return __( 'Note: There is a poll embedded within this post, please visit the site to participate in this post\'s poll.', 'wp-polls' );
	}
}

Code file location:

wp-polls/wp-polls/wp-polls.php

Conclusion

Now that you’ve learned how to embed the WP-Polls Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *