从WordPress中的另一个函数访问变量函数


Access to a variable function from another function in WordPress

有一个问题,我不明白我做错了什么..

我想获取WordPress中其他功能的功能值。

此代码替换了代码的某些部分。

我想获取参数变量单词的值(它需要去$attr['words'](,然后使用其他函数(new_quote(。

    <?php
    /*
    * Plugin Name: Random Quotes
    */
    function random_quote($atts) {
        extract( shortcode_atts( array(
        'path' => plugin_dir_path(__FILE__).'quotes.txt',// default, if not set
        'label_new' => 'New Quote',
        'words' => 'no'   // yes or no 
        ), $atts ) );
        $temp = $attr['words']; // no
        ...
    }
    add_shortcode('randomquotes','random_quote');

    function new_quote(){
    global $temp;  // NULL
    /*
    global $attr;
    $temp = $attr['words']; // again NULL
    */
        ...
        if($temp == "no") {
        ...
        }
    }
   ...
?>

我做错了什么?也许只是无法获取此变量的值?

看起来你需要在 random_quote(( 函数中声明全局$temp。现在,random_quote(( 使用的是本地版本的 $temp,当函数完成时,该版本将丢失。

编辑:这是一个示例代码段

<?php
function test() {
  global $temp;
  $temp = 'no';
}
function my_test() {
  global $temp;
  var_dump($temp);
}
test();
my_test();
?>