1

I need a way not to load the comments form when previewing a post, is there a way to achieve this? How?

If you need a reason to help: I use disqus and it generates a url for the "discussion" the first time the comment form loads, if this is the preview then it will look something like site.com/?post_type=food&p=41009 And this is a problem because afterwards when the post is published under a real url disqus will not recognize the comments count. The only way is to manually change the discussion url. I've already contacted disqus and they say, "not a bug" if you do not want disqus to pick the preview url, don't load disqus on the preview page, the only way i know is to completely remove the comments form, so how would i go about this? Is there some sort of conditional for the preview page?

2 Answers 2

2

I took a quick peek at the disqus plugin. This works in disabling the option before the plugin decides to print out the form.

add_filter( 'pre_option_disqus_active', 'wpse_conditional_disqus_load' );
function wpse_conditional_disqus_load( $disqus_active ) {
  if( is_preview() ){
    return '0';
  }

  return $disqus_active;

}

You could also try something like this (not tested)

add_filter( 'the_content', 'wpse_load_disqus');
function wpse_load_disqus( $content ){
  if( is_preview() ){
    return $content;
  }

  if( is_singular() ) { // displays on all single post types. use is_single for posts only, is_page for pages only

    $content .= ?>
      // You disqus script here
    <?php ;
  }

  return $content;

}
7
  • Thanks! I use disqus plugin for wordpress i don't know how it works but wouldn't it conflict with this? Commented Aug 8, 2016 at 19:51
  • Oh, I thought disqus used some sort of javascript code that you pasted where you wanted the form to show. I'm afraid that while using the plugin there's not much you can do beside what the plugin is capable of doing. And I'm not familiar with that plugin (and it would be off topic for WPSE). If you could find some sort of HTML/Javascript code in your disqus account that you could use, I would get rid of the disqus plugin and use my approach instead.
    – bynicolas
    Commented Aug 8, 2016 at 19:57
  • a quick google search returned this code that you could use where you want to load disqus disqus.com/admin/universalcode Now if you use this approach, you would need to change the 'init' hook to where you actually want your form to load (after the content I assume). If you tell me where you currently load your form, I could update my answer with the appropriate hook
    – bynicolas
    Commented Aug 8, 2016 at 20:00
  • Where do i load the comments form? below the post content. Unless i misunderstood you. Commented Aug 8, 2016 at 20:09
  • 1
    yet another update, but this last one was tested and should work (at least it does for me). Based on @birgire's suggestion, I believe it's better to check on option and return false on is_preview() == true . dequeuing the script wasn't working. So now, we are forcing the option disqus_active to be false before the plugin decides to print either inline or external js, it is the better solution.
    – bynicolas
    Commented Aug 8, 2016 at 22:38
0

Here's one suggestion: Close the comments when previewing:

add_filter( 'template_redirect', function()
{
    if( is_preview() )
        add_filter( 'comments_open', '__return_false' );
} );

This should stop the comments_template() from loading if it's e.g. wrapped with

if( comments_open() ) 
    comments_template();

in your theme. There's also a comments_open() check within comment_form() to prevent the form from displaying if the comments are closed.

We could also do it manually in our child theme:

if( ! is_preview() ) 
    comments_template();

But I'm not sure though how it works with plugins like Disqus.

Here are some very interesting suggestions on how to interfere with the loading of comments_template().

PS: I noticed the the disqus_active option check in the dsq_can_replace() function:

if (get_option('disqus_active') === '0'){ return false; }

so we might try something like:

add_filter( 'template_redirect', function()
{
    is_preview() && add_filter( 'pre_option_disqus_active', 
         function( $value ) { return '0'; }
    );
} );

but note that this is untested!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.