dongtao4890 2016-09-29 09:01
浏览 67
已采纳

WordPress中的PHP类:使用get_option()赋值

The below is a dependency of another script, and in its current form it works just fine. It's just some info needed to authenticate an API call.

<?php
class getStuff {
    public $subdomain = 'somedomain';
    public $key = '123-456-789';
}
?>

However, the values are static. I want to use get_option() to make these values easily changeable from wp-admin. So I figured that this would make sense...

<?php
class getStuff {
    public $subdomain = get_option( 'option_subdomain' );
    public $key = get_option( 'option_key' );
}
?>

Of course, it won't work. I've read and tried a lot of examples on constructors they seem to be addressing different problems. I'm not exactly sure what to look for...

BTW, there is nothing wrong with the way information is stored in options.php - it's working just fine.

  • 写回答

1条回答 默认 最新

  • donglinyi4313 2016-09-29 11:38
    关注

    You should be able to set the class properties inside the constructor like this

    class getStuff {
        public $subdomain;
        public $key;
    
        public function __construct() {
            $this->subdomain = get_option( 'option_subdomain' );
            $this->key = get_option( 'option_key' );
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记