1
<?php
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
define( 'MY_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
define( 'MY_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
register_activation_hook(__FILE__,'my_plugin_install');
register_deactivation_hook( __FILE__, 'my_plugin_remove' );

function my_plugin_install() {
    global $wpdb;
    $the_page_title = 'TEST';
    $the_page_name = 'test';
    delete_option("my_plugin_page_title");
    add_option("my_plugin_page_title", $the_page_title, '', 'yes');
    delete_option("my_plugin_page_name");
    add_option("my_plugin_page_name", $the_page_name, '', 'yes');
    delete_option("my_plugin_page_id");
    add_option("my_plugin_page_id", '0', '', 'yes');
    $the_page = get_page_by_title( $the_page_title );

    if ( ! $the_page ) {
        $_p = array();
        $_p['post_title'] = $the_page_title;
        $_p['post_content'] = "";
        $_p['post_status'] = 'publish';
        $_p['post_type'] = 'page';
        $_p['comment_status'] = 'closed';
        $_p['ping_status'] = 'closed';
        $_p['post_category'] = array(1);
        $the_page_id = wp_insert_post( $_p );
    }
    else {
        $the_page_id = $the_page->ID;
        $the_page->post_status = 'publish';
        $the_page_id = wp_update_post( $the_page );
    }

    delete_option( 'my_plugin_page_id' );
    add_option( 'my_plugin_page_id', $the_page_id );
}

function my_plugin_remove() {
    global $wpdb;
    $the_page_title = get_option("my_plugin_page_title");
    $the_page_name = get_option("my_plugin_page_name");
    $the_page_id = get_option('my_plugin_page_id');
    if( $the_page_id ){wp_delete_post( $the_page_id );}
    delete_option("my_plugin_page_title");
    delete_option("my_plugin_page_name");
    delete_option("my_plugin_page_id");
}

function custom_template($template){
    $template = plugin_dir_path( __FILE__ ) . 'my-custom-page.php';
    return $template;
}

if ( !is_admin() ) {
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post();
            if(get_the_ID() == get_option('my_plugin_page_id')){
                add_filter( 'template_include', 'custom_template' );
            }
        }
    }
}
?>

This is the error given by the code :

Fatal error: Call to a member function have_posts() on a non-object in /var/www/html/wordpress/wp-includes/query.php on line 782

my-custom-page.php is a basic page with only HTML and CSS in it.

The page creation part is ok, due to the !is_admin() part, there is no error while I'm in the administration panel.

Any hint where the problem is ? Thanks :)

P.S. The wordpress installation is the 4.4.2 base with no modifications (no new pages, different theme, ecc...)

0

Your code is being executed too early before the "base" query was evaluated.

As a thumb rule, in a plugin always hook your code to an appropriate hook, probably wp_loaded is best if there is nothing more specific

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.