Комментирование на виртуальных страницах wordpress

Создаю виртуальные страницы в wordpress таким образом

if (!class_exists('WP_EX_PAGE_ON_THE_FLY')){

    class WP_EX_PAGE_ON_THE_FLY {

        public $slug ='';
        public $args = array();

        function __construct($arg){
            add_filter('the_posts',array($this,'fly_page'));
            $this->args = $arg;
            $this->slug = $arg['slug'];
            add_action( 'template_include', array( $this, 'change_template' ) );
        }

        public function fly_page($posts){
            global $wp,$wp_query;
            $page_slug = $this->slug;
            //check if user is requesting our fake page
            if(count($posts) == 0 && (strtolower($wp->request) == $page_slug || $wp->query_vars['name'] == $page_slug)){

                //create a fake post
                $post = new stdClass;
                $post->post_author = 1;
                $post->post_name = $page_slug;
                $post->guid = get_bloginfo('wpurl' . '' . $page_slug);
                $post->post_title = 'This is the fake page title';
                $post->post_type = 'page';
                //put your custom content here
                $post->post_content = 'This is the fake page content';
                //just needs to be a number - negatives are fine
                $post->ID = -42;
                $post->post_status = 'publish';
                $post->comment_status = 'open';
                $post->ping_status = 'closed';
                $post->comment_count = 0;
                //dates may need to be overwritten if you have a "recent posts" widget or similar - set to whatever you want
                $post->post_date = current_time('mysql');
                $post->post_date_gmt = current_time('mysql',1);

                $post = (object) array_merge((array) $post, (array) $this->args);
                $posts = NULL;
                $posts[] = $post;

                $wp_query->is_page = true;
                $wp_query->is_singular = true;
                $wp_query->is_home = false;
                $wp_query->is_archive = false;
                $wp_query->is_category = false;
                unset($wp_query->query["error"]);
                $wp_query->query_vars["error"]="";
                $wp_query->is_404 = false;
                
            }

            return $posts;
        }

        function change_template( $template ) {

            $real_slug = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
            $page_slug = $this->slug;
            
            if ($real_slug === $page_slug) {
                $newTemplate = locate_template( array( '/page-fake.php' ) ); 
                if( '' != $newTemplate )
                    return $newTemplate ;
            }
            return $template;
        } 
    }//end class
}//end if

$all_url = array('fake-page', 'fake-page2', 'fake-page3'); // и т.д.
foreach ($all_url as $urls => $url) { 
    $args = array(
        'slug' => $url,
        'post_title' => 'Заголовок виртуальной страницы',
        'post_content' => 'Содержание виртуальной страницы'
    );
    new WP_EX_PAGE_ON_THE_FLY($args);
}

Как реализовать возможность комментирования. Хоть в коде и открыты комментарии, но по факту после нажатия на кнопку отправить происходит редирект на wp-comments-post.php и все белый экран. Я предполагаю что возможно это из за того что физически страница не существует и нет записи о ней в БД, но как это обойти не понимаю.


Ответы (0 шт):