0

I would like to avoid the include_once() in order to remove one INFO using Theme Check, and when I try to load my Options page using get_template_part('custom-options'); it returns the message:

«Invalid argument supplied for foreach() in [...]/custom-options.php on line 83».

Where the code is the typical:

//[...]
<?php foreach ($options as $value) { //#83
  switch ($value['type']) { case "open": ?>
<?php break; case "close": ?>
<?php break; case "title": ?>
    <h3><?php echo $value['name']; ?></h3>
//[...]

I'm not sure, but perhaps it's not allowed for loading options.

Then I tried load_template( trailingslashit( get_template_directory() ) . 'custom-options.php' ); with the same result.

My questions are: Is that possible to load the options file by using get_template_part()?

If not, is there a way to load the options file and, at the same time, being it valid with the Theme Check plugin?

  • 1
    where do you set $options and is it in the scope of the file you are currently trying to use it in? – Milo Apr 22 '14 at 15:47
  • That's not a Theme Check error; it's merely INFO. The get_template_part() function is a template tag, and is intended to include template-part files, within the template. It is not intended to include functional files within functions.php. You should use include() or require() to include functional files in functions.php. – Chip Bennett Apr 22 '14 at 16:27
  • @Milo, I have the options defined in the file custom-options.php, and then I retrieve some of them in the functions.php file (and also in the other files, of course). I also separated the array of the Settings in a third file, like this: functions.php loads custom-options.php, and this one loads custom-options-array.php. Always approximately at the top of the files. – Gerard Apr 22 '14 at 17:32
  • @ChipBennett, yes, you are right, that's INFO instead of an error. I'm sorry for this mistake. I'm gonna fix it in the post. Thanks for the explanation also :) – Gerard Apr 22 '14 at 17:34
1

You should have a read here. Kaiser wrote a complete article on this on his blog

I quote from that

Although get_template_part is similar to a PHP include or require, you should not use it to include things like your theme options code, sidebars registration, custom widgets, etc. The get_template_part function should only be used to get template parts.

You can go and check out the complete article here

1

You are having scope issues. When file is included inside of a function — it has a scope of that function.

That's why load_template() (which all template "including" function in WP trickle down to) declares number of WP's globals so that they can be accessed inside the templates.

So you'll have either to pass data through globals or otherwise, rework your code from using separate template file, or use some other approach (like dedicated non-PHP template files for example).

  • I'm not sure if the retrievement method is a problem in that case, because the options page works perfect when I load the options using include_once(). But yes, I think that you are right about it, the loading method generates scope issues on it, and as others say, I have to use include_once() instead of get_template_part(), so I will skip the info message of Theme Check plugin. Thanks again for your answer! :) – Gerard Apr 22 '14 at 17:50
  • @Gerard include_once is not a function, it's a language control structure. – Rarst Apr 22 '14 at 18:11

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.