dongtao4890 2016-09-29 01: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 03: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' );
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部