duanpu1111 2016-08-22 11:51
浏览 31
已采纳

在函数php / wordpress之外的函数内访问变量

I've created a variable within a function it looks like this:

function my_plugin_options() {

    //must check that the user has the required capability
    if (!current_user_can('manage_options'))
    {
      wp_die( __('You do not have sufficient permissions to access this page.') );
    }

    // variables for the field and option names
    $opt_name = 'mt_favorite_color';
    $hidden_field_name = 'mt_submit_hidden';
    $data_field_name = 'mt_favorite_color';

    // Read in existing option value from database
    $opt_val = get_option( $opt_name );
    doingthistest($opt_val);

    // See if the user has posted us some information
    // If they did, this hidden field will be set to 'Y'
    if( isset($_POST[ $hidden_field_name ]) && $_POST[ $hidden_field_name ] == 'Y' ) {
        // Read their posted value
        $opt_val = $_POST[ $data_field_name ];

        // Save the posted value in the database
        update_option( $opt_name, $opt_val );

        // Put a "settings saved" message on the screen

?>
<div class="updated"><p><strong><?php _e('settings saved.', 'help-menu-settings' ); ?></strong></p></div>
<?php

    }

    // Now display the settings editing screen

    echo '<div class="wrap">';

    // header

    echo "<h2>" . __( 'Help block details', 'help-menu-settings' ) . "</h2>";

    // settings form

    ?>

<form name="form1" method="post" action="">
<input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y">

<p><?php _e("Whatever:", 'menu-test' ); ?>
<input type="text" name="<?php echo $data_field_name; ?>" value="<?php echo $opt_val; ?>" size="20">
</p><hr />

<p class="submit">
<input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" />
</p>

</form>
<span><?php echo $opt_val ?></span>
</div>

<?php

}

Now I can echo out the $opt_val variable within the scope of that function but i'm struggling to access it outside.

You'll see where I set the variable $opt_val = get_option( $opt_name ); below it I pass it to a function doingthistest($opt_val); then I create an action below so I can call it in another page (WordPress method).

So my action below looks like:

add_action('testingthis', 'doingthistest');
function doingthistest(t) {
    var_dump(t);
}

for some reason, the variable isn't getting passed to my action. Am I misunderstanding something?

I call it in another page like this:

<span>info is there: <?php do_action( 'testingthis' ) ?></span>
  • 写回答

1条回答 默认 最新

  • dongmi19720408 2016-08-22 13:00
    关注

    if you want something to be available in a different page (therefore presumably during a different HTTP request?) then you'd have to put it into a session variable or other persistent storage (e.g. file, database) and the retrieve it from there in your other page. HTTP is inherently stateless and the server won't remember the value of your variables from one request to the next unless you use one of the above mechanisms to store them.

    Calling doingthistest($opt_val); within the context of the code you posted will dump the variable onto that page. But then if you call that same method from another page entirely - which must by definition be in a different request - it doesn't automatically remember what you the value of it was last time. It's a separate request with a separate context.

    I don't understand the Wordpress stuff precisely, but I suspect your action would be better looking something like this:

    add_action('testingthis', 'doingthistest');
    function dosomething($o) {
      var_dump(get_option( $opt_name ));
    }
    

    But obviously you'd have to set the value of $opt_name somehow. In your sample code it's hard-coded but I don't know if that's really how you're setting it in the finished solution.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵