• Resolved Ravikumar Patel

    (@ravipatel)


    Warning: Cannot modify header information – headers already
    how to fix this code.

    add_action( 'wp_head', array( $this, 'update_view' ));
    --------
    -------
    public function update_view() {
                    global $post;
    		if ( is_singular( 'dwqa-question' ) ) {
                            $cookie_name = $post->ID.'_dwqa_who_viewed';
                            $cookie_value = 1 ;
    
                                if(isset($_COOKIE[$cookie_name]) && $_COOKIE[$cookie_name] == 'true' ) {
                                    echo "Cookie '" . $cookie_name . "' is set!<br>";
                                    echo "Value is: " . $_COOKIE[$cookie_name];
    
                                    $refer = wp_get_referer();
                                    if ( is_user_logged_in() ) {
                                            global $current_user;
                                            //save who see this post
                                            $viewed = get_post_meta( $post->ID, '_dwqa_who_viewed', true );
                                            $viewed = ! is_array( $viewed ) ? array() : $viewed;
                                            $viewed[$current_user->ID] = current_time( 'timestamp' );
                                    }
    
                                    if ( ( $refer && $refer != get_permalink( $post->ID ) ) || ! $refer ) {
                                            if ( is_single() && 'dwqa-question' == get_post_type() ) {
                                                    $views = get_post_meta( $post->ID, '_dwqa_views', true );
    
                                                    if ( ! $views ) {
                                                            $views = 1;
                                                    } else {
                                                            $views = ( ( int ) $views ) + 1;
                                                    }
                                                    update_post_meta( $post->ID, '_dwqa_views', $views );
                                            }
                                    }
                            }else{
                                setcookie( $cookie_name, $cookie_value, time() + DAY_IN_SECONDS,COOKIEPATH, COOKIE_DOMAIN, false, false );
                                echo "Cookie named '" . $cookie_name . "' is not set!";
                            }
                    }
    	}
Viewing 1 replies (of 1 total)
  • Thread Starter Ravikumar Patel

    (@ravipatel)

    add_action('init',array($this,'dwqa_question_cookies1'));

    public function dwqa_question_cookies1(){
                ob_start();
    }
Viewing 1 replies (of 1 total)

The topic ‘how to set cookies in inner function’ is closed to new replies.