add_menu_page
// This will not work
add_menu_page( 'test', 'test' , 'manage_options', 'test' );
// Forth parameter is optional in Wordpress but its needed to work correctly
add_menu_page( 'test', 'test' , 'manage_options','test', function () {
// Nothing.
});
The reason for this is that inside wp-admin/admin.php
Wordpress checks if there is a hook callback registered for the current plugin page. If no hook is present it takes a different path inside the gigantic if elseif
statement that wp-admin/admin.php
is.
Instead of dispatching the normal hooks wordpress then tries to load the content from a plugin file and if it cant find any matching file an error is thrown.
@atanas-dev Can you explain what is ment by this in the docs
"You should use query() on routes which do not match any valid WordPress URL or your WP Query will consider the request as a 404 - Not Found. You can find more information on this in the dedicated Query API blog post."
->query()
called on them because the conditions require the query to have been executed in order to match (i.e. you can't test against the current post id if the main query hasn't resolved what the current post is yet).
->query()
usage is limited to the url
route condition only as that condition can be evaluated before the main WP_Query has executed.
post_id
route looks like:App::route()->any()->where( 'post_id', get_option( 'my_special_page_id' ) )->handle( 'MyController::myMethod' );
is_404
I guess :D
the condition
\App::route()->get()->where( 'query_var', 's' )->handle( $handler );
Only matches default query vars defined in WP_QUERY right?
Something like this did not work for me for "non WP Urls".
https//example.com/page?foo='bar'
::routeUrl()
will work though, since reverse construction is not supported in FastRoute
@atanas-dev FastRoute is still maintained and in fact 2.0 is soon to be released. Nevertheless 1.3 is stable and feature complete. Lumen, Slim and other still use it.
I could not resist myself and got this done. I have a working version locally which works like this now: ( note that this is basically a complete rewrite of the router and condition factory. )
I assume performance will be a lot higher for sites with many routes but I dont have the time to benchmark things right now and Im also not that experienced in setting up proper benchmarking.
Ofc I dont have any expectations of this being merged ever ( required php 7.3 ) but Ill share you the repo if you want when Im done, maybe you ll find some ideas for WPEmerge aswell.
Right now I have a suggestion for you tho, maybe you overlooked this at first.
Whoops has an option to open files directly in the editor of your choice.
Its as simple as doing this.
$pretty_page_handler->setEditor('phpstorm');
Might be a nice addition, the value would then come from the user config of course
myapp_get_index_404_message()
but i'm struggling on how to detect a page that is not going through a page template, a page or anything from WP (since it's built by the controller...). anything i've tested so far works (is_page()
, is_page_template()
, is_archive()
, etc). any idea on how to intercept that? thanks :)
get_header()
@atanas-dev hey atanas, just wanted to let you know a possible bug I found while working on my own version of wpemerge. Right now when you render a view its almost impossible to catch expection while rendering the php file. Because the __toString()
methods gets called recursivly output buffering also gets turned on end off everytime. This results in all the html before the error being print to the screen while everythings after the exception doesnt.
IMO the output buffering should be one level above the the rendering logic inside the toResponse()
method on the PhpView
class.
This way you can properly render exceptions without the view output. Maybe this is due to an edge case in my version but Im pretty certain I have not modified anything relevent inside the view related classes.
Anyway this is what fixed it form me.
// Inside PhpView
public function toResponse() : ResponseInterface {
ob_start();
try {
$this->toString();
} catch (\Throwable $exception) {
ob_end_clean();
throw new ViewException();
}
$content = ob_get_clean();
return ( new Response( $content ) )->setType( 'text/html' );
}
The output is the following with the default WP error page:
"Foobar
Fatal error: Uncaught Error: Call to undefined function non_existing_function() in /Users/calvinalkan/valet/booking/wp-content/plugins/test-plugin/view.php:10 Stack trace: #0 /Users/calvinalkan/valet/booking/wp-content/plugins/snicco-bookings-"
It also makes sense why this happens:
Inside the PhpView:
public function toString() {
if ( empty( $this->getName() ) ) {
throw new ViewException( 'View must have a name.' );
}
if ( empty( $this->getFilepath() ) ) {
throw new ViewException( 'View must have a filepath.' );
}
$this->engine->pushLayoutContent( $this );
if ( $this->getLayout() !== null ) {
// ERROR HAPPENS HERE
return $this->getLayout()->toString();
}
return $this->engine->getLayoutContent();
}