#15427 closed defect (bug) (invalid)
Bug when creating a new post during 'plugins_loaded'
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | minor | Version: | 3.0.1 |
Component: | Permalinks | Keywords: | close |
Focuses: | Cc: |
Description
Actuvate the following plugin exemple:
<?php /* Plugin Name: Bug Test Plugin URI: http://Hikari.ws Description: Exemplifies a Core bug Version: 0.00.00 Author: Hikari Author URI: http://Hikari.ws */ add_action('plugins_loaded', 'createMngmtPage'); function createMngmtPage(){ $management_page_array = array( 'post_title' => 'Bug test', 'post_content' => 'Oops, Huston we have a problem! Please contact the administrator of this blog for further details.', 'post_type' => 'page', 'ping_status' => 'closed', 'comment_status' => 'closed', 'post_status' => 'publish', 'post_author' => 1 ); $management_page_id = wp_insert_post($management_page_array); }
wp_insert_post(), called during 'plugins_loaded', calls get_permalink(), which calls get_page_link(), which calls $wp_rewrite->get_page_permastruct().
But $wp_rewrite isn't instantiated yet and throws an error:
Fatal error: Call to a member function get_page_permastruct() on a non-object in D:\Docs\webserver\wphik\wp-includes\link-template.php on line 275
One may argue that wp_insert_post() is being called too early, of course it would be better called during 'init', but the point is that during 'plugins_loaded' it's already declared, but it requires an object that's not ready yet.
I suggest testing if $wp_rewrite is an object and if not return an WP_Error, this way core becomes more solid and the plugin calling it will have the responsibility to test if it received an error instead the permalink or the post ID.
Change History (4)
#2
@
14 years ago
Init is the earliest hook that you should use for calling core API.
Plugins Loaded is all about doing things once all the plugins have been included - for example if you want to behave differently if another plugin was installed.
Register a post type before init, with rewrite = true.
$wp_rewrite will fail that way too.
Plugins_loaded is early enough to load your plugin. That's about it. You need to wait for init until basically everything.
Clearly, it's not working. What more error do you need? :-) If anything WP_Error will mask it further.