Unautop / Sanitize content added to a post through a plugin

I am using the SimpleReach plugin that adds some content to every post using the content hook. The content that is being added is

<!-- SimpleReach Slide Plugin Version: {{srslide_plugin_version}} -->
<script type='text/javascript' id='simplereach-slide-tag'>
    __reach_config = {
      pid: '{{srslide_pid}}',
      title: '{{title}}',
      tags: {{{tags}}},
      authors: {{{authors}}},
      channels: {{{channels}}},{{slide_logo_elem}}
      slide_active: {{srslide_disable_on_post}},{{slide_icon_elem}}
      date: '{{published_date}}',
      url: '{{canonical_url}}',
      header: '{{srslide_header_text}}'
    };
    var content = document.getElementById('simplereach-slide-tag').parentNode, loc;
    if (content.className){ loc = '.' + content.className; }
    if (content.id){ loc = '#' + content.id; }
    __reach_config.loc = loc || content;
    (function(){
    var s = document.createElement('script');
      s.async = true;
      s.type = 'text/javascript';
      s.src = document.location.protocol + '//d8rk54i4mohrb.cloudfront.net/js/slide.js';
      __reach_config.css = '{{srslide_css_url}}';
      var tg = document.getElementsByTagName('head')[0];
      if (!tg) {tg = document.getElementsByTagName('body')[0];}
      if (tg) {tg.appendChild(s);}
    })();
</script>

However WordPress is sanitizing the content by adding extra breaks and paragraphs before and after.

Read More

I want to disable autop only for this content so that breaks/paragraphs aren’t added.

Is there a way to do it?

Related posts

2 comments

  1. A couple of caveats before beginning. It tried to run that plugin but apparently I have to register with a third party site before I can use it, so that throws a wrench in testing.

    Second, I am having trouble duplicating this problem. That may be because I have beaten my dev stack up too much and it is time to wipe it clean and start over (which it is), so that said…

    wpautop runs on the entire post content. You can’t, by any means that I am aware, restrict it to run on only part of the content.

    I have not installed and tested that plugin so I can’t confirm that your analysis of the problem is correct, but in general there are several things that you can do to avoid having wpautop mangle content:

    1. Register and enqueue the script so as to avoid shoving this into the
      post body at all.
    2. Remove all the line breaks and extra whitespace from that block of
      code. None of that is necessary and without it wpautop would have
      nothing to bite into.
    3. Insert the content very late via a priority when the callback is
      added to the_content

    The only one that you may be able to effect is #3. The script adds that code by means of: add_filter('the_content', 'srslide_insert_slide_js');. You should be able to remove that with remove_filter('the_content', 'srslide_insert_slide_js'); and add it back with add_filter('the_content', 'srslide_insert_slide_js',1000); to get it to run after wpautop.

  2. I don’t think you can setup autop like this.

    The only idea that comes to my mind is to set a post meta after you insert the post and then setup a condition in functions.php.

    I don’t know how the post is added by that plugin, but you will need to retrieve its ID.

    Then use this to add the meta:

    <?php add_post_meta($post_id, 'no_autop', 'true'); ?> 
    

    Then disable autop on posts with that meta by adding this to functions.php:

    if(get_post_meta( $post_id, 'no_autop', true )=='') {
    remove_filter( 'the_content', 'wpautop' );
    }
    

Comments are closed.