[wp-trac] [WordPress Trac] #15427: Bug when creating a new post during 'plugins_loaded'

WordPress Trac wp-trac at lists.automattic.com
Sun Nov 14 20:12:41 UTC 2010


#15427: Bug when creating a new post during 'plugins_loaded'
--------------------------+-------------------------------------------------
 Reporter:  shidouhikari  |       Owner:                 
     Type:  defect (bug)  |      Status:  new            
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Permalinks    |     Version:                 
 Severity:  minor         |    Keywords:                 
--------------------------+-------------------------------------------------
 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.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/15427>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list