Support » Plugin: Simple Facebook Connect » Simple Facebook Connect may break javascript added by other plugins

  • I found out that when Simple Facebook Connect is active, the content passes through WordPress filter wptexturize() after the application of shortcodes, not before as expected.

    I’m not really sure of the extent of this issue, but at least I found out that wptexturize converts && (two ampersands) to & #038;& (ampersand html escape code and an ampersand). So if you have a shortcode that adds a javascript to the content, and the javascript contains && (the AND operator), this is escaped by wptexturize, so javascript parser throws an error and the code is not executed.

    So, is it really necessary the code in sfc_base_make_excerpt, where you apply the_content filters on the text? Why you have to remove wptexturize and then re-add it putting it at the end of the_content filter queue? Is there another way to do this?

Viewing 7 replies - 1 through 7 (of 7 total)
  • As a quick and dirty solution, I have added the function _sfc_apply_filters in sfc-base.php. This function is the copy of apply_filter as in WordPress 3.5.1, but I added a condition so it executes all the filter functions except wptexturize. In sfc_base_make_excerpt I have removed the remove_filter and add_filter calls and I use _sfc_apply_filters instead of apply_filters

    function _sfc_apply_filters($tag, $value) {
    	global $wp_filter, $merged_filters, $wp_current_filter;
    	$args = array();
    	// Do 'all' actions first
    	if ( isset($wp_filter['all']) ) {
    		$wp_current_filter[] = $tag;
    		$args = func_get_args();
    	if ( !isset($wp_filter[$tag]) ) {
    		if ( isset($wp_filter['all']) )
    		return $value;
    	if ( !isset($wp_filter['all']) )
    		$wp_current_filter[] = $tag;
    	// Sort
    	if ( !isset( $merged_filters[ $tag ] ) ) {
    		$merged_filters[ $tag ] = true;
    	reset( $wp_filter[ $tag ] );
    	if ( empty($args) )
    		$args = func_get_args();
    	do {
    		foreach( (array) current($wp_filter[$tag]) as $the_ )
    			if ( !(is_null($the_['function']) || $the_['function'] == 'wptexturize' ) ){
    				$args[1] = $value;
    				$value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
    	} while ( next($wp_filter[$tag]) !== false );
    	array_pop( $wp_current_filter );
    	return $value;
    // code to create a pretty excerpt given a post object
    function sfc_base_make_excerpt($post) { 
    	if ( !empty($post->post_excerpt) )
    		$text = $post->post_excerpt;
    		$text = $post->post_content;
    	$text = strip_shortcodes( $text );
    	// filter the excerpt or content, but without texturizing
    	if ( empty($post->post_excerpt) ) {
    		$text = _sfc_apply_filters('the_content', $text);
    	} else {
    		$text = _sfc_apply_filters('the_excerpt', $text);

    I hope you could rethink about the way excerpt text is generated: it seems over-complex to me at a first sight, maybe this could be done in a simpler way, but I had no time to study it and maybe all this is necessary.
    Neither my fix is optimal, because _sfc_apply_filters works only with WordPress 3.5.1 and in versions with the same code for apply_filters, and may not work with other versions. So if you use a fix like mine you have to check if apply_filters changes in future WordPress versions.

    Plugin Author Samuel Wood (Otto)


    If you have JS code being inserted by shortcodes, then that JS code should be stripped out later anyway, ideally. This is because the sfc_base_make_excerpt code is trying to create a text-only excerpt to give to Facebook, not to create something to be displayed on the page.

    Also, you have read the code incorrectly, and the wptexturize filter is NOT applied at all by sfc_base_make_excerpt. That is the purpose of removing and re-adding it afterwards. It is just re-adding the filter as it is per default, it is not actually running it. It removes the filter’s call to wptexturize, then it calls the filter, then it adds the filter back. Thus, it’s running it without wptexturize.

    Furthermore, shortcodes are completely stripped from the sfc_base_make_excerpt function, so your complaint, and your code, make no real sense. Shortcodes won’t be processed at all by this function, because they’re not in the text anymore.

    Plugin Author Samuel Wood (Otto)


    Also, you might be thinking that the removing and re-adding it would change the order, and you’d be correct, but you’d be missing then that wptexturize doesn’t affect things in script-tags to begin with. Look at the $default_no_texturize_tags value and the code the function has to not mess with things inside those sets of tags. If the shortcode you have is inserting proper script tags, then wptexturize will ignore those.

    I think you didn’t understand my point: I got that sfc_base_make_excerpt is basically a function that strips the html to create a textual excerpt, and it’s ok if it strips javascript for its purpose. I got also that for some reason you don’t want to apply wptexturize during apply_filters(‘the_content’, $text) in the context of sfc_base_make_excerpt.

    It seems you think that if you remove filter wptexturize and add back again is ok, but it’s not, because the order of filters is important. wptexturize is one of the first functions added to the_content filter queue at default priority (10), if you add it back it becomes the last filter.

    As sfc_base_make_excerpt is called in the header, the change of behavior of the_content filters breaks the javascript code in the_content later in the main content. If you still think this is not an issue, I may provide a simple case with a simple plugin so you can do your tests.

    G you guys are extra terrestrial you lost me on the first sentence of this topic lol

    Plugin Author Samuel Wood (Otto)


    fabio84: I see what you’re saying about the order of the filters, but the thing is that the order is not supposed to be important because those are all at the same priority. While the current apply_filters code does execute them in the order added, that’s more of a side-effect than anything else. If an order is needed, then it should be explicitly defined.

    Furthermore, wptexturize itself is not supposed to affect script code. If it actually is doing so, then that’s a core bug, and a test case demonstrating that would be suitable for making a core patch to WordPress to prevent that from happening.

    @roterdam909 No, I’m just Italian and we love long sentences, and I’m trying to explain technical issues in a language that I don’t know very well 😀

    @otto: Ok, at a first try, I wasn’t able to reproduce the issue in a vanilla WordPress with only a test plugin and Simple Facebook Connect. In fact I had the issue on a WordPress installation with many plugins. On this installation, I tried enabling and disabling Simple Facebook Connect, and my fix described in the second post seemed to resolve the issue.

    I have another hypothesis: maybe another plugin disables wptexturize and replaces it with another function (or gives to it another priority with the third optional parameter), so you don’t actually disable wptexturize (because another plugin already disabled it) and you add it, break something. I’ll investigate further….

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Simple Facebook Connect may break javascript added by other plugins’ is closed to new replies.