error fatal get_page_permastruct ()

2

Recibo el siguiente error Fatal error: Call to a member function get_page_permastruct() /link-template.php on line 276  cuando invoco al constructor de la clase el wp_insert_post($new_post); .

add_action( 'plugins_loaded', array ( 'test', 'init' ) );
class test{

    public static function init()
    {
        new self;
    }

    //the consructor for parser admin
    function __construct(){

        global $wpdb;
        $this->db = $wpdb;      

        if ($_POST['action'] == 'add-page'){
            add_action( 'plugins_loaded', array ( $this, 'add_newpage' ) );
            $this->add_newpage();
        }
    public function add_newpage(){
        $new_post = array(
                'post_title' => $_POST['page-name'],
                'post_content' => '',
                'post_status' => 'publish',
                'post_type' => 'page'
        );

        $post_id = wp_insert_post($new_post);   
        $newtarget = $this->db->insert( 
                    'wp_test_table', 
                    array( 
                        'post_id' => $post_id, 
                        'slug' => 'test'
                    ), 
                    array( 
                        '%d', 
                        '%s' 
                        ) 
                    );

    }

}
    
pregunta kakuki 11.12.2012 - 12:58

1 respuesta

5

Esto sucede porque plugins_loaded se dispara antes de que exista la variable global $wp_rewrite .

Desde wp-settings.php :

// Load active plugins.
foreach ( wp_get_active_and_valid_plugins() as $plugin )
    include_once( $plugin );
unset( $plugin );

// Load pluggable functions.
require( ABSPATH . WPINC . '/pluggable.php' );
require( ABSPATH . WPINC . '/pluggable-deprecated.php' );

// Set internal encoding.
wp_set_internal_encoding();

// Run wp_cache_postload() if object cache is enabled and the function exists.
if ( WP_CACHE && function_exists( 'wp_cache_postload' ) )
    wp_cache_postload();

do_action( 'plugins_loaded' );

// Define constants which affect functionality if not already defined.
wp_functionality_constants( );

// Add magic quotes and set up $_REQUEST ( $_GET + $_POST )
wp_magic_quotes();

do_action( 'sanitize_comment_cookies' );

/**
 * WordPress Query object
 * @global object $wp_the_query
 * @since 2.0.0
 */
$wp_the_query = new WP_Query();

/**
 * Holds the reference to @see $wp_the_query
 * Use this global for WordPress queries
 * @global object $wp_query
 * @since 1.5.0
 */
$wp_query =& $wp_the_query;

/**
 * Holds the WordPress Rewrite object for creating pretty URLs
 * @global object $wp_rewrite
 * @since 1.5.0
 */
$GLOBALS['wp_rewrite'] = new WP_Rewrite();

Solución: enganche en wp_loaded , no en plugins_loaded .

    
respondido por el fuxia 11.12.2012 - 13:10

Lea otras preguntas en las etiquetas