Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Retrieving values with "get_option();" WordPress


HI There,

I created a custom options panel within my admin area for my wordpress theme. (I followed the tutorial in [[LINK href=""]]this link[[/LINK]])

Everything is working just fine.

When I try to retrieve a value within a page template using get_option, everything works as expected.

<strong>An example of the code used to pull a value is:</strong>

The issue I'm having is that I am trying to retrieve a value using the same technique, but within a file that is not a page template. The file is a separate PHP file that is located in the themes main directory.

I'm assuming that I need to somehow add connection information, or pass some sort of values to this PHP file so that it is able to execute the built in wordpress function of: get_option();

Any help is greatly appreciated.

Answers (3)


Milan Petrovic answers:

get_option is a function registered by WordPress, and you need to load WordPress to use it. Easiest way is to include wordpress load file on top of your php file:

<?php require_once("path/to/wp-load.php"); ?>

Be aware that this will load almost entire WordPress engine (nothing visual, but all functions and classes).

WP Answers comments:

Works like a charm.

Thank you very much Milan.


Jarret Minkler answers:

Don't use a file, simply use add_option("myhack_extraction_length", '255', '', 'yes');

As from this page

If you must get from file, open the file

$fh = fopen('filename', 'r');

and loop through the lines adding each using the add_option function

while($line = fgets($fh)){

This will create a new option in the database. then you can use get_option and update_option normally.


Buzu B answers:

If you don't want to load up the entire wp engine, you can always perform a regular query to the data base. I can explain more on that if you want. Just let me know.

WP Answers comments:

Hi Buzu,

Is there any kind of security threat or anything for loading up the entire engine?

Would it be more beneficial to just perform a query to the database?

Thanks a lot for your input.