不能访问wordpress函数外的变量



我很难理解为什么我不能访问$theme_text_domain时,它被放置在以下功能之外的wordpress。

<?php
$theme_text_domain = 'theme-text-domain';
function theme_widgets()
{
register_sidebar( array(
'id'            => 'primary',
'name'          => __( 'Primary Sidebar', $theme_text_domain ),
'description'   => __( 'Primary Sidebar', $theme_text_domain ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget'  => '</aside>',
'before_title'  => '<h3 class="widget-title">',
'after_title'   => '</h3>',
) );
}
add_action( 'widgets_init', 'theme_widgets' );

我得到以下错误

Warning: Undefined variable $theme_text_domain in D:DevelopmentPhpwordpresswp-contentthemescustom-componentsfunctions.php on line 33
Warning: Undefined variable $theme_text_domain in D:DevelopmentPhpwordpresswp-contentthemescustom-componentsfunctions.php on line 34

,但如果我把它放在函数里面,它工作得很好。但我希望对functions。php文件中的所有函数只声明一次文本域而不是每次都输入文本域。这可能是一个愚蠢的问题,但为什么会发生这种情况。因为它在函数之外,所以我认为它在作用域中,但显然不在。

因为你正在使用一个动作。

Actions是WordPress核心启动的钩子

你能做的就是把它包装成一个类。

最新更新