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 企业资源规划ERP沙盘模拟
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答