WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Using WordPress functions inside classes without constructs? (3 posts)

  1. Jerry
    Member
    Posted 2 years ago #

    Before I begin to ask, please see my file structure
    /functions.php
    /lib
    /lib/settings.php

    /functions.php has just a little bit of code, aside from my copyright statments:

    require_once(TEMPLATEPATH.'/../landBaronSl/lib/admin.php');
    require_once(TEMPLATEPATH.'/../landBaronSl/lib/postMeta.php');
    require_once(TEMPLATEPATH.'/../landBaronSl/lib/settings.php');
    require_once(TEMPLATEPATH.'/../landBaronSl/lib/invoke.php');
    
    new lbInvoke;
    
    // Setup the settings and registration variables
    $lbSettings = new lbSettings;
    register_activation_hook(__FILE__,$lbSettings->registerSettings());

    The last line is causing an issue, I get "Fatal error: Call to undefined function register_setting()" and it's referencing the settings file, line 37. The settings file looks like so:

    class lbSettings{
    
    	function lbSettings(){
    		$this->__construct();
    	}
    
    	function __construct(){
    
    	}
    
    	public function registerSettings(){
    		$settingsArray = array(
    		// General Settings
    			'lbTemplate',	// The selected template
    
    		// PostMeta Settings
    			'lbLandTypes', // Land types, will be an array()
    			'lbLandStyle', // Grass, snow, etc... an array of course
    			'lbLandManagers', // Users that will be able to have management options for the box in SL.
    
    		);
    
    		foreach($settingsArray as $setting){
    			register_setting('lbSettingsGroup', $setting);
    		}
    	}
    
    }

    During the last foreach loop is what it's saying is a fatal error!

    So before you say, "use the action hook in your construct" please know that's not the solution I'm looking for. So my question is, how come WordPress functions cannot be utilized inside a class when it's called the way I did? I'm lost and would appreciate an explanation of my failure if possible.

  2. Jerry
    Member
    Posted 2 years ago #

    FYI this is the only workaround I can see to utilize the plugin functions within a class without adding action hooks:

    function __construct(){
    		require_once(ABSPATH.'/wp-admin/includes/plugin.php');
    	}

    I would still like to know why I have to require a file just to use functions that are available anywhere outside the class.

  3. Jerry
    Member
    Posted 2 years ago #

    Well since no one cares to entertain my brain with an explanation, and counting that I have utilized a workaround, I'll mark this as resolved.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.