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 Stata链式中介效应代码修改
  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 添加组件无法加载页面,某块加载卡住
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用