• One of my sites had a weird side effect after implementing the Simple Facebook Connect plugin – my custom shortcode was being output in the head of the page as well as in the content.

    The page in question is here: http://www.dubrovnikrestaurantkl.com/about/group-bookings. I’ve since disabled SFC so the problem has went away. The image rotator was being outputted on both the head and in the content.

    Here is my custom shortcode – perhaps I didn’t code it correctly, which caused the problem?

    function dubrovnik_attachment_slider( $atts ) {
    
    	// get shortcode attributes
    	extract( shortcode_atts( array(
    		'width' => '460',
    		'height' => '300',
    	), $atts ) );
    
    	// print images
    	global $post;
    	$args = array(
    		'order'          => 'ASC',
    		'post_type'      => 'attachment',
    		'post_parent'    => $post->ID,
    		'post_mime_type' => 'image',
    		'post_status'    => null,
    		'numberposts'    => -1,
    	);
    	$attachments = get_posts($args);
    	if ($attachments) {
    		echo '<div class="attachment-slider" ';
    	//	if ( ( $width !== '') && ( $height !== '' ) ) {
    			echo 'style="width:' . $width . 'px; height:' . $height . 'px;"';
    	//	}
    		echo '>';
    		foreach ($attachments as $attachment) {
    			echo '<img src="' . wp_get_attachment_url($attachment->ID) . '" alt="' . apply_filters('the_title', $attachment->post_title) . '" />';
    		}
    		echo '</div>';
    	}
    
    	// output JS in wp_footer
    	add_action('wp_footer', 'dubrovnik_attachment_slider_js');
    	function dubrovnik_attachment_slider_js() {
    		?>
    			<script type="text/javascript">
    			jQuery(document).ready(function($) {
    				$('.attachment-slider img:gt(0)').hide();
    					setInterval(function(){
    						$('.attachment-slider :first-child').fadeOut()
    						.next('img').fadeIn()
    					.end().appendTo('.attachment-slider');
    					}, 5000
    				);
    			});
    			</script>
    		<?php
    	}
    
    }
    
    add_shortcode( 'attachment_slider', 'dubrovnik_attachment_slider' );

    http://wordpress.org/extend/plugins/simple-facebook-connect/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Shortcodes should never “echo” anything. Shortcode functions are supposed to *return* the text that replaces them. If your code was returning the HTML instead of echo’ing it, then it would work better.

    SFC does run shortcodes in the head, because it needs to get the content, with the shortcodes processed, in order to do things like find images and video and create excerpts for Facebook. This isn’t going to change.

    Thread Starter David Wang

    (@blogjunkie)

    Hi Otto, thanks for swinging by.

    SFC does run shortcodes in the head, because it needs to get the content, with the shortcodes processed, in order to do things like find images and video and create excerpts for Facebook. This isn’t going to change.

    Oh no, I’m not asking you to change your plugin to accommodate me. Thanks for pointing out the error, I’m now going to figure out the difference between echo and return 🙂

    Plugin Author Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Basically, instead of echoing your HTML code, build it into a string.

    echo '<div class="attachment-slider" ';
    	//	if ( ( $width !== '') && ( $height !== '' ) ) {
    			echo 'style="width:' . $width . 'px; height:' . $height . 'px;"';
    	//	}

    becomes:

    $output = '<div class="attachment-slider" ';
    	//	if ( ( $width !== '') && ( $height !== '' ) ) {
    			$output .= 'style="width:' . $width . 'px; height:' . $height . 'px;"';
    	//	}

    Then return $output; when you’re done making the string.

    Plugin Author Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Also, your add_action('wp_footer', 'dubrovnik_attachment_slider_js'); and the associated function should be outside the shortcode function, in the main code of the plugin itself.

    Thread Starter David Wang

    (@blogjunkie)

    Hi again. Thanks for the impromptu code review! I appreciate your help 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Simple Facebook Connect running shortcodes in the head’ is closed to new replies.