WordPress.org

Ready to get started?Download WordPress

Forums

Simple Facebook Connect
SFC conflicting with shortcode which loads custom loop (11 posts)

  1. Josh Leuze
    Member
    Posted 2 years ago #

    Hi Otto, a user of my plugin Meteor Slides reported to me that they were having a conflict with Simple Facebook Connect.

    The issue is that when you try to edit a page or post that has the slideshow shortcode in it, WordPress loads a slide post from the slideshow in the editor instead. Deactivating SFC or my plugin resolves this issue and you can edit pages with the shortcode normally.

    I was able to replicate this issue on a fresh install with WP 3.3.1, MS 1.4, and SFC 1.2 with just the Publisher module activated. I saw that there were some other forum posts with shortcode issues so I thought maybe I was having a similar problem. Possibly due to including a template file using the shortcode, or a problem with the contents of that template file, so I tested this out with a stripped down, self-contained shortcode plugin:

    <?php /*
    	Plugin Name: JL Test Shortcode */
    	function jltest_shortcode() {
    		$jltest_output = '';
    		$jltest_loop = new WP_Query( array( 'posts_per_page' => 3 ) );
    		while ( $jltest_loop->have_posts() ) : $jltest_loop->the_post();
    			$jltest_output .= '<li><a href="' . get_permalink() . '">' . the_title( '', '', false ) . '</a></li>';
    		endwhile;
    		wp_reset_query();
    		return '<ul>' . $jltest_output . '</ul>';
    	}
    	add_shortcode( 'jltest', 'jltest_shortcode' ); ?>

    With the test plugin, editing any page that has the [jltest] shortcode instead loads one of the posts from the custom loop. I tried constructing this a few different ways, but the results are always the same, as long as there is a loop, the page cannot be edited.

    Am I missing anything obvious here, is there a better way to load a loop using a shortcode?

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

  2. Samuel Wood (Otto)
    Tech Ninja
    Plugin Author

    Posted 2 years ago #

    I don't think that wp_reset_query resets the global $id variable. Check the value of the global $id before and after your loop, see if it changes. And see if changing it back fixes the problem.

    If it's not the $id, it may be something else. Check the various globals, something has to be changing due to your loop. You could var_dump the $GLOBALS before and after to see any differences.

  3. Josh Leuze
    Member
    Posted 2 years ago #

    Thanks Otto, I tested the global $id, it's the same before and after, seems to be resetting. Without wp_reset_query the global $id changes to that of one of the posts in the loop though.

    Problem persists either way though, I'll check out the the globals and see what I can learn.

  4. Josh Leuze
    Member
    Posted 2 years ago #

    I got this working, changing back the global $id didn't help, but changing back the global $post did.

    My solution seems a little crude, but it works:

    <?php /*
    	Plugin Name: JL Test Shortcode */
    	function jltest_shortcode() {
    		global $post;
    		$jltest_posttemp = $post;
    		$jltest_output = '';
    		$jltest_loop = new WP_Query( array( 'posts_per_page' => 3 ) );
    		while ( $jltest_loop->have_posts() ) : $jltest_loop->the_post();
    			$jltest_output .= '<li><a href="' . get_permalink() . '">' . the_title( '', '', false ) . '</a></li>';
    		endwhile;
    		$post = $jltest_posttemp;
    		wp_reset_postdata();
    		return '<ul>' . $jltest_output . '</ul>';
    	}
    	add_shortcode( 'jltest', 'jltest_shortcode' ); ?>

    I tried the same solution on the slideshow loop in my plugin and it worked for that as well.

  5. 3dolab
    Member
    Posted 2 years ago #

    I've been experiencing a similar problem when using SFC in conjunction with a slideshow plugin and WPEC, which heavily relies on shortcodes and custom templates.

    On single products (custom post type), when just SFC-Base is activated the site displays only the text content.

    Unfortunately, swapping the $post object and running wp_reset_postdata() seems quite useless, or anyway it's a bit hard to keep track of every manipulation in such a complex plugin as WP E-Commerce.

    So, it may not be elegant but I found it much easier to directly edit SFC, by simply commenting sfc-base.php at line 574 and replacing sfc-media.php at line 18 with $content = $post->post_content; in order to prevent filters from being applied to the content.

  6. Samuel Wood (Otto)
    Tech Ninja
    Plugin Author

    Posted 2 years ago #

    If you don't apply the filters to the content, then shortcodes won't get processed and things like images and other content added by those shortcodes won't appear.

  7. 3dolab
    Member
    Posted 2 years ago #

    alright.
    in effect, apart from static WPEC page templates (triggered by shortcodes) that aren't posted to FB, this should be a case where the shortcode output doesn't matter to FB (could it really embed a js image slideshow?)
    anyway, I don't want the bracketed [shortcode] itself to get shown: do you think I should take care of any remaining custom queries or the content filter thing should lead to another approach?

    thank you for your help

  8. 3dolab
    Member
    Posted 2 years ago #

    I guess the culprit is the function wpsc_single_template in WPEC/includes/theme.functions.php, since it does remove its own filter.

    It's really ugly to see, but I've found a simple workaround by editing the same sfc-base.php and sfc-media.php to (re)add and then remove again the filter 'wpsc_single_template' to 'the_content' around where filters get applied

  9. 3dolab
    Member
    Posted 2 years ago #

    Seems that I've talked too soon.
    The problem still remains: the fact is that SFC base stops WPEC from applying its filter (and then loading the product page template).
    Shortcodes seem not being involved in this case.
    Only single (is_singular) product pages are affected, not the homepage.
    WPEC missed conditions should be:
    if ( 'wpsc-product' == $wp_query->post->post_type && !is_archive() && $wp_query->post_count <= 1 )

    I have to disable any filter in sfc_base_make_excerpt, sfc_media_handler, sfc_media_find_images and sfc_media_find_video in order to get the content back to be loaded within the correct template file.

    What I'm missing, is how SFC could ever manipulate all that, only by applying filters to internal "placeholder" variables like $content or $text?
    I guess that some global variable alteration is occurring somewhere in the plugin. Am I wrong?

    BTW, what does $id refer to in sfc-base.php at line 503?

  10. 3dolab
    Member
    Posted 2 years ago #

    Awww I think the first guess was almost right:
    it was all about just adding
    add_filter( 'the_content', 'wpsc_single_template', 12 );
    after every apply_filters to the_content in sfc-base.php and sfc-meta.php

    * and not add_filter => apply_filters => remove_filter
    as previously explained

  11. Samuel Wood (Otto)
    Tech Ninja
    Plugin Author

    Posted 2 years ago #

    BTW, what does $id refer to in sfc-base.php at line 503?

    $id is storing the value returned from $wp_the_query->get_queried_object_id() there, which in the case of a singular page will be the main post's ID.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic