Polldaddy Shortcode

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

Before starting, here is an overview of the Polldaddy Plugin and the shortcodes it provides:

Plugin Icon
Crowdsignal Dashboard – Polls, Surveys & more

"Crowdsignal Dashboard – Polls, Surveys & more" is a versatile WordPress plugin that empowers users to create, manage, and analyze polls, surveys, and more directly from their WordPress dashboard.

★★☆✩✩ (34) Active Installs: 100000+ Tested with: 6.2.3 PHP Version: 5.6
Included Shortcodes:
  • [crowdsignal]

Polldaddy [crowdsignal] Shortcode

The Crowdsignal (formerly Polldaddy) shortcode is used to embed polls, surveys, and ratings into WordPress posts or pages. This PHP function extracts attributes, sets defaults, and handles various display types.

Shortcode: [crowdsignal]

Parameters

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

  • survey – Identifier of the survey to be displayed
  • link_text – Text for the link to the survey
  • poll – Identifier of the poll to be displayed
  • rating – Rating value to be displayed
  • unique_id – Unique ID for the rating or poll
  • item_id – ID of the item being rated or polled
  • title – Title of the poll or survey
  • permalink – Permalink URL for the poll or survey
  • cb – Cache buster value to prevent caching of data
  • type – Type of the display, such as button, slider, etc.
  • body – Body text for the survey or poll
  • button – Button text for the survey or poll
  • text_color – Text color for the survey or poll
  • back_color – Background color for the survey or poll
  • align – Alignment of the survey or poll
  • style – Style of the survey or poll
  • width – Width of the survey or poll
  • height – Height of the survey or poll
  • delay – Delay before displaying the survey or poll
  • visit – Defines when the survey or poll should be displayed
  • domain – Domain of the survey or poll
  • id – Identifier of the survey or poll

Examples and Usage

Basic example – Displaying a simple poll with the ID of 5 on your webpage

[crowdsignal poll=5 /]

Advanced examples

Creating a survey with a unique ID, customized link text, and a specific width and height.

[crowdsignal survey="123456" link_text="Please take our survey!" width="500" height="300" /]

Embedding a rating system with a unique ID and a specific post ID as the item ID.

[crowdsignal rating="5" unique_id="rating123" item_id="post7" /]

Displaying a poll with a specific ID, customized link text, and set to display as a button.

[crowdsignal poll="789" link_text="Vote now!" type="button" /]

Using the shortcode to display a poll with a specific ID, and setting the color of the text and background.

[crowdsignal poll="321" text_color="FF0000" back_color="0000FF" /]

PHP Function Code

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

Shortcode line:

add_shortcode( 'crowdsignal', array( $this, 'polldaddy_shortcode' ) );

Shortcode PHP function:

function polldaddy_shortcode( $atts ) {
		global $post;
		global $content_width;

		extract( shortcode_atts( array(
			'survey'     => null,
			'link_text'  => 'Take Our Survey',
			'poll'       => 'empty',
			'rating'     => 'empty',
			'unique_id'  => null,
			'item_id'    => null,
			'title'      => null,
			'permalink'  => null,
			'cb'         => 0,
			'type'       => 'button',
			'body'       => '',
			'button'     => '',
			'text_color' => '000000',
			'back_color' => 'FFFFFF',
			'align'      => '',
			'style'      => '',
			'width'      => $content_width,
			'height'     => floor( $content_width * 3 / 4 ),
			'delay'      => 100,
			'visit'      => 'single',
			'domain'     => '',
			'id'         => ''
		), $atts, 'crowdsignal' ) );

		if ( ! is_array( $atts ) ) {
			return '<!-- Crowdsignal shortcode passed invalid attributes -->';
		}

		$inline          = !in_the_loop();
		$no_script       = false;
		$infinite_scroll = false;

		if ( is_home() && current_theme_supports( 'infinite-scroll' ) )
			$infinite_scroll = true;

		if ( defined( 'PADPRESS_LOADED' ) )
			$inline = true;

		if ( function_exists( 'get_option' ) && get_option( 'polldaddy_load_poll_inline' ) )
			$inline = true;

		if ( is_feed() || ( defined( 'DOING_AJAX' ) && !$infinite_scroll ) )
			$no_script = false;

		self::$add_script = $infinite_scroll;

		if ( intval( $rating ) > 0 && !$no_script ) { //rating embed

			if ( empty( $unique_id ) )
				$unique_id = is_page() ? 'wp-page-'.$post->ID : 'wp-post-'.$post->ID;

			if ( empty( $item_id ) )
				$item_id = is_page() ? '_page_'.$post->ID : '_post_'.$post->ID;

			if ( empty( $title ) )
				$title = apply_filters( 'the_title', $post->post_title );

			if ( empty( $permalink ) )
				$permalink = get_permalink( $post->ID );

			$rating    = intval( $rating );
			$unique_id = preg_replace( '/[^\-_a-z0-9]/i', '', wp_strip_all_tags( $unique_id ) );
			$item_id   = wp_strip_all_tags( $item_id );
			$item_id   = preg_replace( '/[^_a-z0-9]/i', '', $item_id );

			$settings = json_encode( array(
				'id'        => $rating,
				'unique_id' => $unique_id,
				'title'     => rawurlencode( trim( $title ) ),
				'permalink' => esc_url( $permalink ),
				'item_id'   => $item_id
			) );

			$item_id = esc_js( $item_id );

			if ( $inline ) {
				return <<<SCRIPT
<div class="cs-rating pd-rating" id="pd_rating_holder_{$rating}{$item_id}"></div>
<script type="text/javascript" charset="UTF-8"><!--//--><![CDATA[//><!--
PDRTJS_settings_{$rating}{$item_id}={$settings};
//--><!]]></script>
<script type="text/javascript" charset="UTF-8" src="https://polldaddy.com/js/rating/rating.js"></script>
SCRIPT;
			} else {
				if ( self::$scripts === false )
					self::$scripts = array();

				$data = array( 'id' => $rating, 'item_id' => $item_id, 'settings' => $settings );

				self::$scripts['rating'][] = $data;

				add_action( 'wp_footer', array( $this, 'generate_scripts' ) );

				$data = esc_attr( json_encode( $data ) );

				if ( $infinite_scroll )
					return <<<CONTAINER
<div class="cs-rating pd-rating" id="pd_rating_holder_{$rating}{$item_id}" data-settings="{$data}"></div>
CONTAINER;
				else
					return <<<CONTAINER
<div class="cs-rating pd-rating" id="pd_rating_holder_{$rating}{$item_id}"></div>
CONTAINER;
			}
		} elseif ( intval( $poll ) > 0 ) { //poll embed

			$poll      = intval( $poll );
			$poll_url  = sprintf( 'https://poll.fm/%d', $poll );
			$poll_js   = sprintf( '%s.polldaddy.com/p/%d.js', '//static', $poll );
			$poll_link = sprintf( '<a href="%s">Take Our Poll</a>', $poll_url );

			if ( $no_script ) {
				return $poll_link;
			} else {
				if ( $type == 'slider' && !$inline ) {

					if( !in_array( $visit, array( 'single', 'multiple' ) ) )
						$visit = 'single';

					$settings = array(
						'type'  => 'slider',
						'embed' => 'poll',
						'delay' => intval( $delay ),
						'visit' => $visit,
						'id'    => intval( $poll )
					);

					return $this->get_async_code( $settings, $poll_link );
				} else {
					$cb      = ( $cb == 1 ? '?cb='. time() : false );
					$margins = '';
					$float   = '';

					if ( in_array( $align, array( 'right', 'left' ) ) ) {
						$float = sprintf( 'float: %s;', $align );

						if ( $align == 'left')
							$margins = 'margin: 0px 10px 0px 0px;';
						elseif ( $align == 'right' )
							$margins = 'margin: 0px 0px 0px 10px';
					}

					// Force the normal style embed on single posts/pages otherwise it's not rendered on infinite scroll themed blogs ('infinite_scroll_render' isn't fired)
					if ( is_singular() )
						$inline = true;

					if ( $cb === false && !$inline ) {
						if ( self::$scripts === false )
							self::$scripts = array();

						$data = array( 'url' => $poll_js );

						self::$scripts['poll'][] = $data;

						add_action( 'wp_footer', array( $this, 'generate_scripts' ) );

						$data = esc_attr( json_encode( $data ) );

						$script_url = esc_url_raw( plugins_url( 'js/polldaddy-shortcode.js', __FILE__ ) );

						$str = <<<CONTAINER
<a name="pd_a_{$poll}"></a>
<div class="CSS_Poll PDS_Poll" id="PDI_container{$poll}" data-settings="{$data}" style="display:inline-block;{$float}{$margins}"></div>
<div id="PD_superContainer"></div>
<noscript>{$poll_link}</noscript>
CONTAINER;

$loader = <<<SCRIPT
( function( d, c, j ) {
  if ( !d.getElementById( j ) ) {
    var pd = d.createElement( c ), s;
    pd.id = j;
    pd.src = '{$script_url}';
    s = d.getElementsByTagName( c )[0];
    s.parentNode.insertBefore( pd, s );
  }
  else if ( typeof jQuery !== 'undefined' )
  	jQuery( d.body ).trigger( 'pd-script-load' );
}( document, 'script', 'pd-polldaddy-loader' ) );
SCRIPT;

						$loader = $this->compress_it( $loader );
						$loader = "<script type='text/javascript'>\n".$loader."\n</script>";

						return $str.$loader;
					} else {
						if ( $inline )
							$cb = '';

						return <<<CONTAINER
<a name="pd_a_{$poll}"></a>
<div class="CSS_Poll PDS_Poll" id="PDI_container{$poll}" style="display:inline-block;{$float}{$margins}"></div>
<div id="PD_superContainer"></div>
<script type="text/javascript" charset="UTF-8" src="{$poll_js}{$cb}"></script>
<noscript>{$poll_link}</noscript>
CONTAINER;
					}
				}
			}
		} elseif ( !empty( $survey ) ) { //survey embed

			if ( in_array( $type, array( 'iframe', 'button', 'banner', 'slider' ) ) ) {

				if ( empty( $title ) ) {
					$title = __( 'Take Our Survey!', 'polldaddy' );
					if( !empty( $link_text ) )
						$title = $link_text;
				}

				if ( $type == 'banner' || $type == 'slider' )
					$inline = false;

				$survey      = preg_replace( '/[^a-f0-9]/i', '', $survey );
				$survey_url  = esc_url( "https://survey.fm/{$survey}" );
				$survey_link = sprintf( '<a href="%s">%s</a>', $survey_url, esc_html( $title ) );

				if ( $no_script || $inline || $infinite_scroll )
					return $survey_link;

				if ( $type == 'iframe' ) {
					if ( $height != 'auto' ) {
						if ( isset( $content_width ) && is_numeric( $width ) && $width > $content_width )
							$width = $content_width;

						if ( !$width )
							$width = '100%';
						else
							$width = (int) $width;

						if ( !$height )
							$height = '600';
						else
							$height = (int) $height;

						return <<<CONTAINER
<iframe src="{$survey_url}?iframe=1" frameborder="0" width="{$width}" height="{$height}" scrolling="auto" allowtransparency="true" marginheight="0" marginwidth="0">{$survey_link}</iframe>
CONTAINER;
					} elseif ( !empty( $domain ) && !empty( $id ) ) {

						$domain = preg_replace( '/[^a-z0-9\-]/i', '', $domain );
						$id = preg_replace( '/[\/\?&\{\}]/', '', $id );

						$auto_src = esc_url( "https://{$domain}.survey.fm/{$id}" );
						$auto_src = parse_url( $auto_src );

						if ( !is_array( $auto_src ) || count( $auto_src ) == 0 )
							return '<!-- no crowdsignal output -->';

						if ( !isset( $auto_src['host'] ) || !isset( $auto_src['path'] ) )
							return '<!-- no crowdsignal output -->';

						$domain   = $auto_src['host'] . '/';
						$id       = ltrim( $auto_src['path'], '/' );

						$settings = array(
							'type'       => $type,
							'auto'       => true,
							'domain'     => $domain,
							'id'         => $id
						);
					}
				} else {
					$text_color = preg_replace( '/[^a-f0-9]/i', '', $text_color );
					$back_color = preg_replace( '/[^a-f0-9]/i', '', $back_color );

					if ( !in_array( $align, array( 'right', 'left', 'top-left', 'top-right', 'middle-left', 'middle-right', 'bottom-left', 'bottom-right' ) ) )
						$align = '';

					if ( !in_array( $style, array( 'inline', 'side', 'corner', 'rounded', 'square' ) ) )
						$style = '';

					$title  = wp_strip_all_tags( $title );
					$body   = wp_strip_all_tags( $body );
					$button = wp_strip_all_tags( $button );

					$settings = array_filter( array(
						'title'      => $title,
						'type'       => $type,
						'body'       => $body,
						'button'     => $button,
						'text_color' => $text_color,
						'back_color' => $back_color,
						'align'      => $align,
						'style'      => $style,
						'id'         => $survey
					) );
				}

				if ( empty( $settings ) )
					return '<!-- no crowdsignal output -->';

				return $this->get_async_code( $settings, $survey_link );
			}
		} else {
			return '<!-- no crowdsignal output -->';
		}
	}

Code file location:

polldaddy/polldaddy/polldaddy-shortcode.php

Conclusion

Now that you’ve learned how to embed the Polldaddy Plugin shortcode, 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 *