WordPress.org

Ready to get started?Download WordPress

Forums

Simple Facebook Connect
Simple Facebook Connect running shortcodes in the head (6 posts)

  1. blogjunkie
    Member
    Posted 2 years ago #

    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/

  2. Samuel Wood (Otto)
    Tech Ninja
    Plugin Author

    Posted 2 years ago #

    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.

  3. blogjunkie
    Member
    Posted 2 years ago #

    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 :)

  4. Samuel Wood (Otto)
    Tech Ninja
    Plugin Author

    Posted 2 years ago #

    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.

  5. Samuel Wood (Otto)
    Tech Ninja
    Plugin Author

    Posted 2 years ago #

    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.

  6. blogjunkie
    Member
    Posted 2 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic