我使用add_theme_page()
在我的孩子主题中functions.php
实现了如下内容。。。
// other functions located here that create a custom options page in the Dashboard.
// THIS IS NOT A FILE PATH ISSUE -> THE CUSTOM PAGE IS CREATED IN BOTH CASES.
function my_menu() {
...
if ( \'save\' == $_REQUEST[\'action\'] ) {
echo \'<meta http-equiv="refresh" content="0;url=themes.php?page=functions.php&saved=true">\';
die;
}
add_theme_page(...)
}
add_action(\'admin_menu\', \'my_menu\');
As per the WordPress docs:注意:如果您正在运行中,您没有足够的权限访问此页面。«中的消息wp_die()
屏幕,那么你已经上钩太早了。你应该用的钩子是admin_menu
.
这正是我在保存选项后遇到的错误。然而,我已经按照建议使用了正确的挂钩。
仅当我将函数移动到新文件并使用require_once
在内部funtions.php
. 我按照this answer.
require_once(\'includes/functions-theme-options.php\');
为什么只需将函数移动到外部文件中,就会出现这种情况?我想这可能与位置有关,但require_once()
位于原始代码所在的准确位置。当代码位于functions.php
, 它工作得很好。
当代码移动到外部文件并引用时require_once()
, 它会中断(根据上面引用的文档保存时出现的错误消息)。
显然,目前,我将其控制在function.php
, 但也许有人能解释为什么我移动它时它会断裂。
<小时>
EDIT
This is not a file path issue. 引用的每个文件require_once()
正在找到并包含。问题似乎是如何执行。请阅读以下答案:https://wordpress.stackexchange.com/a/1406/11092
<小时>
EDIT 2 - revised 1/16/13
这是一个经过验证的精简版本,可充分演示该问题。这段代码在“外观”下构造了一个子菜单页,称为“主题选项”。单击“保存选项”按钮时,页面将重新加载并显示“您的主题设置已保存”消息。但是,它会显示WordPress错误页面,“您没有足够的权限访问此页面。”只需将最后一个函数移动到functions.php
清除此权限问题。我想知道为什么以及如何修复它。在里面functions.php
文件:
<?php
require_once(\'includes/functions-test.php\');
在我的子主题目录中,includes/functions-test.php
文件:<?php
$themename = wp_get_theme();
function mytheme_admin() {
global $themename;
if ( $_REQUEST[\'saved\'] ) echo \'<div id="message" class="updated fade"><p><strong>\'.$themename.\' settings saved.</strong></p></div>\';
?>
<div class="wrap">
<h2><?php echo $themename; ?> Options</h2>
<div style="border-bottom: 1px dotted #000; padding-bottom: 10px; margin: 10px;">Options specfic to this theme.</div>
<form method="post">
<p class="submit">
<input name="save" type="submit" class="button button-primary" value="Save Options" />
<input type="hidden" name="action" value="save" />
</p>
</form>
</div>
<?php
}
function wp_initialize_the_theme_load() {
if (!function_exists("wp_initialize_the_theme")) {
wp_initialize_the_theme_message();
die;
}
}
add_action(\'admin_menu\', \'mytheme_add_admin\');
/* move only the following function to \'functions.php\' and the problem is gone
__________________________________________________________________________*/
function mytheme_add_admin() {
global $themename, $shortname;
if ( $_GET[\'page\'] == basename(__FILE__) ) {
if ( \'save\' == $_REQUEST[\'action\'] ) {
echo \'<meta http-equiv="refresh" content="0;url=themes.php?page=functions.php&saved=true">\';
die;
}
}
add_theme_page($themename." Options", "Theme Options", \'edit_theme_options\', basename(__FILE__), \'mytheme_admin\');
}