SimpleRain / SimpleOptions

A simple wordpress options framework for developers.
16 stars 2 forks source link

Property of Non-Object #29

Closed ghost closed 11 years ago

ghost commented 11 years ago

@dovy When using the following:

<? 
global $Simple_Options;
$options = $Simple_Options->options;

blah blah blah

if ( $options['enable_something'] ) {
 my code
} 
?>

I get a PHP "Notice: Trying to get property of non-object" on the second line.

And, if I use this:

<?
global $Simple_Options;

blah blah blah

if ( $Simple_Options->options['enable_something'] ) {
 my code
}
?>

I get the same PHP notice.

Yes I have double and triple checked that the enable_something exists in options-init. Am I doing this wrong?

dovy commented 11 years ago

@WhatJustHappened

I made a booboo last night. Please wait a little bit. Trying to fix something that alters the framework quite a bit.

ghost commented 11 years ago

@dovy Ahh, I see, thanks!

dovy commented 11 years ago

OK, it's set.

Now, there's a small limitation in the framework that I have yet to resolve that I believe is causing your issue. If you've never saved an option before (meaning you recently added it) the info won't be accessible to the database.

So, to fix that you need to at least save the class once in the admin.

Here's some exact code to get what you want. The newest release should fix all your issues.

<?php
global $Simple_Options;
$options = $Simple_Options->options;
print_r($options);

Sorry about that.

ghost commented 11 years ago

@dovy Hmm, yeah I see the initial save problem now. That does appear to be the problem, as a quick look in the db and the options table does not exist yet.

However, further diving into that problem here. If I am calling $Simple_Options anywhere in my theme files I can't save the options. Reason being that I get a "Warning: Cannot modify header information - headers already sent by" error when saving or resetting.

dovy commented 11 years ago

@WhatJustHappened That sounds like a PHP file that you might have that starts with a space before the opening <? tag.

A common problem in wordpress. Here's an example:

(LINE DROP IS HERE)
<?php
$Simple...

She that space above the <?php? Wordpress will call that a defined header. Annoying, I know.

But this usually only happens in backend code, not your typical front-end template code.

Does that help? Reopening.

ghost commented 11 years ago

@dovy Unfortunately, no that didn't help. The modified header error references the first line that has:

$options = $Simple_Options->options;

So, if that is on line 4 I get "Warning: Cannot modify header information - headers already sent by (output started at /Users//Documents/Websites//wp-content/themes//:4) in /Users//Documents/Websites//wp-includes/pluggable.php on line 875".

And if I move the $SimpleOptions to line 8 and save again I get: "Warning: Cannot modify header information - headers already sent by (output started at /Users//Documents/Websites//wp-content/themes//:8) in /Users//Documents/Websites/_/wp-includes/pluggable.php on line 875".

dovy commented 11 years ago

What file are you trying to add it to? Can you gist that file?

dovy commented 11 years ago

(you must be really frustrated by now. I'm sorry for all the concerns).

dovy commented 11 years ago

Also, as per your needs I just added a fix for new fields. If you add a new one (and the count of fields is different) the defaults are set for you.

Haven't released a new version yet, but you could do a pull to get the dev build. ;)

ghost commented 11 years ago

@dovy Hey, no frustrations on my part, I appreciate the work you have been putting into this and problems are expected.

Here is the gist: https://gist.github.com/WhatJustHappened/6346505

Just a basic sample of a functions file with SOF in use.

dovy commented 11 years ago

Wow. that is quite quite odd.

Would you be willing in emailing me your theme? I won't "use any code".

I just can't really dig in. I was hoping for an easy fix.

info@simplerain.com

ghost commented 11 years ago

@dovy Just sent the files to you. I just started the project this morning so there isn't much there, but you should be able to see the errors I am working with on activation.

Btw, I am developing on a local server.

dovy commented 11 years ago

@WhatJustHappened So am I. I'll let you know what I find.

dovy commented 11 years ago

@WhatJustHappened Are you sure you sent it to me? Perhaps gmail block it.

Try wetransfer.com

dovy commented 11 years ago

@WhatJustHappened Nevermind. Got it.

dovy commented 11 years ago

@WhatJustHappened Sorry, I was gone all day. I know the issue you have. You're trying to call Simple_Options without initialising it.

You first need to do something like here: https://github.com/SimpleRain/SimpleOptions/blob/master/options-init.php#L83-L813

Specificially line 813. Until you call the $Simple_Options = new Simple_Options($sections, $args, $tabs); with properly populated arrays, the class is initialised. The error you're getting is: Trying to get property of non-object and that's right! Simple_Options is not an object until it's created.

;)