0

I get this error

Fatal error: Call to a member function get_page_permastruct() on a non-object in C:\xampp\htdocs\wordpress\wp-includes\link-template.php on line 358

when i call this function

function pn_create_page() {
    global $wpdb;
    $check_page = $wpdb->get_var("SELECT option_value FROM ".$wpdb->prefix."pn_options WHERE id = '10'" );

    // if no page create one
    if ($check_page==''){
        $pagedata = array(
        'post_content'   => 'Page content will go here.',
        'post_title'     => 'Page Title',
        'post_status'    => 'publish',
        'post_type'      => 'page',
        'post_author'    => 1,
        'ping_status'    => 'closed',
        'comment_status' => 'closed'
        );
        $post_id = wp_insert_post( $pagedata );
        $wpdb->update($wpdb->prefix.'pn_options', array('option_value'=>$post_id), array('id'=>'10'));

    // if page excist do nothing    
    } else {
        return;
    }
}

The line wich causes the error is this one

$post_id = wp_insert_post( $pagedata );

Also i get this notice

Notice:  Trying to get property of non-object in C:\xampp\htdocs\wordpress\wp-includes\post.php on line 3688

from the same line.....

Don´t know whats wrong.... Any ideas?

I want to create a wordpress page directly from a plugin....

  • 3
    when do you call pn_create_page? – Milo Jan 29 '15 at 21:19
  • In the main file from the plugin after the functions file where this function is in. – wassereimer Jan 29 '15 at 21:34
  • 2
    that's too early, you need to hook it to an action that runs after everything is set up, like init. – Milo Jan 29 '15 at 21:37
  • After i put it in the buttom of all and call add_action('init', 'pn_create_page'); its working now. Thank you! Can you explain why it must called later and with an action? – wassereimer Jan 29 '15 at 21:47
  • possible duplicate of fatal error get_page_permastruct() – T.Todua Apr 13 '15 at 8:55
2

The WordPress environment is not yet fully initialized when your plugin files are loaded, it's almost always too early to safely run code that interacts with core at that point.

WordPress achieves its initial state through a process which contains its own set of actions and filters, so plugins load before that happens to allow them to hook into that process.

The action and filter system is the basis of core / plugin interaction, just about everything a plugin does will be tied to an action or filter. It's a simple system that provides a roadmap of the whole request process, allowing you to manage the order of when your plugin code executes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.