• Resolved nielss

    (@nielss)


    I have a class that registers a new endpoint, rewrites rules and then flushes it. It works perfectly except that it also gives a 404 error, making the page title “page not found” and adding some css which I dont want. I need to make it so that it actually knows my page exists. The added css and page title change are problems I need to resolve. This is the class I use, but it might not have anything to do with the problem:

    <?php
    
        use Jigoshop\Helper\Render as RenderCore;
        use Jigoshop\Frontend\Page\PageInterface;
        use Jigoshop\Integration\Helper\Render;
    
        if ( ! defined( 'ABSPATH' ) ) {
            exit; // Exit if accessed directly.
        }
    
        class twizo_Jigoshop_TFASettings implements PageInterface
        {
            function __construct()
            {
                Render::addLocation('twizo-verification', __DIR__);
                add_action('init', function () {
                add_rewrite_endpoint("twizo-verification", EP_ROOT | EP_PAGES | 
        EP_PERMALINK);
                add_filter('generate_rewrite_rules', function ($rewrite) {
                    $mySettings = [
                        '^account/twizo-verification/?$' => 'index.php? 
        pagename=twizo-verification'
                    ];
                    $rewrite->rules = $mySettings + $rewrite->rules;
                    return $rewrite->rules;
                });
                flush_rewrite_rules();
            });
            add_filter('jigoshop.frontend.page_resolver.page', function ($args) {
                global $wp_query;
                if ($wp_query->query['pagename'] == 'twizo-verification') {
                    return $this;
                }
                return $args;
            });
        }
    
        public function action()
        {
            $this->renderTFASettings();
        }
    
        public function render()
        {
        }
    
        public function renderTFASettings()
        {
            add_action('jigoshop\template\shop\content\before', function () {        
                echo '<h1>My account &raquo; 2FA Settings</h1>';
            });
            add_action('jigoshop\template\shop\content\after', function() {
                echo '<br><a href="./" class="btn btn-default">Go back to My 
        account</a>';
            });   
            
            switch(get_template()) {
                case "twentyfifteen":
                    RenderCore::output('layout/twentyfifteen', [
                        'content' => Render::get('twizo-verification', 'tfa- 
           settings-body', array())
                    ]);
                break;
                case "twentysixteen":
                    RenderCore::output('layout/twentysixteen', [
                        'content' => Render::get('twizo-verification', 'tfa- 
           settings-body', array())
                    ]);
                break;
                case "twentyseventeen":
                    RenderCore::output('layout/twentyseventeen', [
                        'content' => Render::get('twizo-verification', 'tfa- 
           settings-body', array())
                    ]);
                break;
                default: 
                    RenderCore::output('layout/default', [
                        'content' => Render::get('twizo-verification', 'tfa- 
           settings-body', array())
                    ]);
                break;
            }
            
            exit;
        }
        }
    • This topic was modified 6 years, 1 month ago by nielss.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Is there an actual WP page whose slug is twizo-verification? It seems like no to me. Regardless of what your class is doing, WP is still trying to query for twizo-verification. When it fails to find such a page, a 404 is generated even though your class is providing output. Once WP receives a request, it needs to find something in its query, even if it’s a dummy page with no content.

    If by chance such a page does exist, the query is not finding it. Probably due to conflicting query vars. Check all of the global $wp_query->query_vars. Something there is preventing a successful query. If the conflict is not apparent, check $wp_query->request, the actual SQL query. There’s a reason it’s not finding a match in there somewhere.

    Thread Starter nielss

    (@nielss)

    Thank you! I guess I’ll make a dummy page then. It worked. Do you perhaps know a solution to the following error?

    Fatal error: Uncaught Error: Call to a member function get_page_permastruct() on null in C:\xampp\htdocs\TwizoAdminTFA\wp-includes\link-template.php

    The code that is causing it:

    if(!$this->page_exists_by_slug('twizo-verification')){
                $newPage = array(
                    'post_title' => '2FA Settings',
                    'post_name' => 'twizo-verification',
                    'post_type' => 'page',
                    'post_status' => 'publish'
                );
    
                wp_insert_post($newPage);
            }
    
    public function page_exists_by_slug($page_slug) {
            
            $page = get_page_by_path( $page_slug , OBJECT );
        
            if ( isset($page) )
               return true;
            else
               return false;
        }
    
    Thread Starter nielss

    (@nielss)

    Nvm, I fixed it. I was adding the page before wp_loaded was called.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘I made a custom endpoint. Getting page not found title but content is there.’ is closed to new replies.