• Resolved Ciao121

    (@ciao121)


    Hi,
    I’m using this code in a mu-plugin to stop not logged users to comment posts. I’m using mu-plugin because I need to have control over this setting in a multi user environment (WPMU).

    <?php
    function disable_network_anonymous_comments() {
    if (is_user_logged_in()) {
    		return true;
    	}else{
    		return false;
    	}
    }
    add_filter('comments_open', 'disable_network_anonymous_comments', 20, 2);
    

    The problem is that in this way a logged user can always send a comment (also if the blog owner disabled them for a particular post).

    So I tried to add comments_open() in this way:

    <?php
    function disable_network_anonymous_comments() {
    if (is_user_logged_in() && comments_open()) {
    		return true;
    	}else{
    		return false;
    	}
    }
    add_filter('comments_open', 'disable_network_anonymous_comments', 20, 2);

    But this causes a php-fpm segmentation fault (!!!) And here I really do not know how to solve.
    Any idea? Thank you!!!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    It appears you’ve caused an infinite loop by hooking “comments_open” and within calling comments_open(), which fires “comments_open” and calls comments_open(), which fires… :).

    Your callback either needs to decide if comments are truly open without calling comments_open(), or it needs to remove itself from the “comments_open” action stack before it calls comments_open(). If need be, it can be added back again once the true comments_open status is determined.

    Thread Starter Ciao121

    (@ciao121)

    Lol… you’re right!
    I solved using:

    <?php
    add_action( 'the_post', 'disable_comments_if_not_logged' );
    	function disable_comments_if_not_logged()
    	{
            if (is_user_logged_in() )
    		{
    			return;
    		}else{
    			add_filter( 'comments_open', 'st_close_comments_on_category', 10, 2 );
    			add_action('wp_enqueue_scripts', 'st_deregister_reply_js');
    		}
    	}
    	function st_deregister_reply_js()
    	{
    		wp_deregister_script( 'comment-reply' );
    	}
    	function st_close_comments_on_category ($open, $post_id)
    	{
    		$open = false;
    	}
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘comments_open() and php segmentation fault.’ is closed to new replies.