I have come across an interesting problem regarding the get_template_part() .

I have created a theme options page (named sitewideSettings.php)

normally I would include this in functions.php like so:

include(TEMPLATEPATH."sitewideSettings.php");

which works fine.

But now I am trying to use get_template_part();

get_template_part('sitewideSettings');

which is also working fine except that when clicking ‘update’ to update the settings I get an error message: “Are you sure you want to do this? Please try again”.

“Please try again” links back to the theme options page and clicking update takes you back to the error message which takes you back to Theme options and round and round we go.

Nothing else has changed other than the use of get_template_part();

Anyone else come across this problem? How did you get around it? I am sure its something simple and obvious, but exactly what it is totally escapes me.

(Its not a nonce issue as a nonce is created in both instances)

5 s
5

I know you say it’s not a nonce issue, but it clearly is. That notice is the default ‘This nonce didn’t check out’ message. I don’t know exactly why the nonce is failing, but that is what is happening.

I would double check how your nonces are created and checked. If you’re using wp_nonce_field() make sure both action values and argument values are the same. Also, double check spelling of variables, or any scope-related issues that could be causing the nonce to fail.

EDIT

Following up on your insistence that it’s not a nonce issue (which it is. The nonce issue is caused by a scope issue), here’s what’s probably happened. Included/required files are executed in the scope in which they’re included. My guess is you’re using a variable to build either the nonce action or the nonce name. Because get_template_part() includes the template file in the scope of that function, it’s no longer being executed in the scope of your admin page’s display callback. You either need to globalize those variables in all locations, or you need to use constants instead of variables.

Leave a Reply

Your email address will not be published. Required fields are marked *