您好,我正在使用woocommerce的设置API创建表单字段。我想在这里从数据库中提取电子邮件,并将其显示在frontendnow have fetch the email的下拉列表中,但当我将其分配到选项字段时,所有引号都会转换为“";”
使用WC\\u Payment\\u类网关
参考this 编写表单字段代码的步骤
我用来获取电子邮件的代码是
$blogusers = get_users();
// Array of stdClass objects.
$str = "";
foreach ( $blogusers as $user ) {
$str .= \'"\'.$user->user_email.\'" =\\> __("\'.$user->user_email.\'", "mails"),\';
}
$str = rtrim($str,\',\');
这就是我使用它创建下拉列表的方式
$this->form_fields = array(\'sec_r2_mail\' => array(
\'title\' => __(\'Receiver 2 Mail\', \'mail\'),
\'type\' => \'select\',
\'label\' => __(\'Emails\', \'mail\'),
\'default\' => \'\',
\'options\' => array($str)));
我所期望的是:
"[email protected]" => __("[email protected]", "mails"),"[email protected]" => __("[email protected]", "mails")
查看页面源代码时得到的结果是:
"[email protected]" => __("[email protected]", "mails"), "[email protected]" => __("[email protected]", "mails")
这就是下拉列表没有正确呈现的原因,我哪里出错了,或者我在这里遗漏了什么。我应该怎样做才能“不转化为”提前感谢。
最合适的回答,由SO网友:TheDeadMedic 整理而成
当您需要实际的数组时,您正在创建数组的字符串表示!
$blogusers = get_users();
$options = array();
foreach ( $blogusers as $user ) {
$options[ $user->user_email ] = $user->user_email; // No need to pass through __(), don\'t translate emails!
}
然后把它传给你的
options
参数:
$this->form_fields = array(
\'sec_r2_mail\' => array(
\'options\' => $options,
// Other arguments
),
);