Skip to content
Advertisement

require_once wordpress error with plugins_url

I’m working on a custom plugin for wordpress to show a maintenance mode screen to users. Ive followed a guide but I have some truble while I’m debugging the code. xdebug will show me these errors:

Warning: require_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0

Warning: require_once(http://localhost/wordpress/wp-content/plugins/maintenance-mode/assets/maintenance.php): failed to open stream: no suitable wrapper could be found

Fatal error: require_once(): Failed opening required ‘http://localhost/wordpress/wp-content/plugins/maintenance-mode/assets/maintenance.php‘ (include_path=’.:/usr/local/php5/lib/php’)

I’m using the plugins_url to load the path where the maintenance.php file is saved, and before th error I was using plugins_dir_path but with the same result.

Is there a fix for this?

class Maintenance {

  public function init()
  {
    add_action( 'wp_loaded', array($this, 'maintenance_mode') );
    //add_action( 'admin_init', array($this, 'maintenance_settings') );
  }

  public function maintenance_mode()
  {
    global $pagenow;
    if( $pagenow !== 'wp-login.php' && !current_user_can('manage_options') && !is_admin() ){
      header( $_SERVER['SERVER_PROTOCOL'] . '503 Service Temporarily Unavailable', true, 503 );
      header( 'Content-Type: text/html; charset=utf-8' );
      require_once plugins_url('assets/maintenance.php' ,__FILE__);
    }
    die();
  }
}

Advertisement

Answer

Try use plugin_dir_path( __FILE__ ):

class Maintenance {

    public function init()
    {
        add_action( 'wp_loaded', array($this, 'maintenance_mode') );
        //add_action( 'admin_init', array($this, 'maintenance_settings') );
    }

    public function maintenance_mode()
    {
        global $pagenow;
        if( $pagenow !== 'wp-login.php' && !current_user_can('manage_options') && !is_admin() ){
            header( $_SERVER['SERVER_PROTOCOL'] . '503 Service Temporarily Unavailable', true, 503 );
            header( 'Content-Type: text/html; charset=utf-8' );
            require_once plugin_dir_path( __FILE__ ) . 'assets/maintenance.php';
        }
        die();
    }
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement