输入欧元符号时发现获取不可打印的字符

时间:2018-10-15 作者:James Paul

当在wordpress主题代码中插入欧元符号时,出现“发现了不可打印的字符”错误,而且欧元符号甚至没有正确显示。

我附上了截图,还添加了我得到这个问题的代码。

  class forrent_details_Meta_Box

{
    private $screens = array(
        \'forrent\',
    );
    private $fields = array(
        array(
            \'id\' => \'price\',
            \'label\' => \'Price:\',
            \'type\' => \'text\',
        ) ,
        array(
            \'id\' => \'before-price-label-for-example-per-month\',
            \'label\' => \' Before Price Label :\',
            \'type\' => \'select\',
            \'options\' => array(
                \'€\',
                \'$\',
            ),
        ) ,
        array(
            \'id\' => \'size-only-numbers\',
            \'label\' => \'Size :\',
            \'type\' => \'text\',
        ) ,
        array(
            \'id\' => \'lot-size-only-numbers\',
            \'label\' => \'Lot Size:\',
            \'type\' => \'text\',
        ) ,
        array(
            \'id\' => \'rooms-only-numbers\',
            \'label\' => \'Rooms :\',
            \'type\' => \'text\',
        ) ,
        array(
            \'id\' => \'bedrooms-only-numbers\',
            \'label\' => \'Bedrooms:\',
            \'type\' => \'text\',
        ) ,
        array(
            \'id\' => \'bathrooms-only-numbers\',
            \'label\' => \'Bathrooms :\',
            \'type\' => \'text\',
        ) ,
    );
    /**
     * Class construct method. Adds actions to their respective WordPress hooks.
     */
    public

    function __construct()
    {
        add_action(\'add_meta_boxes\', array(
            $this,
            \'add_meta_boxes\'
        ));
        add_action(\'save_post\', array(
            $this,
            \'save_post\'
        ));
    }

    /**
     * Hooks into WordPress\' add_meta_boxes function.
     * Goes through screens (post types) and adds the meta box.
     */
    public

    function add_meta_boxes()
    {
        foreach($this->screens as $screen)
        {
            add_meta_box(\'property-details\', __(\'Property Details\', \'realsha\') , array(
                $this,
                \'add_meta_box_callback\'
            ) , $screen, \'advanced\', \'high\');
        }
    }

    /**
     * Generates the HTML for the meta box
     *
     * @param object $post WordPress post object
     */
    public

    function add_meta_box_callback($post)
    {
        wp_nonce_field(\'property_details_data\', \'property_details_nonce\');
        $this->generate_fields($post);
    }

    /**
     * Generates the field\'s HTML for the meta box.
     */
    public

    function generate_fields($post)
    {
        $output = \'\';
        foreach($this->fields as $field)
        {
            $label = \'<label for="\' . $field[\'id\'] . \'">\' . $field[\'label\'] . \'</label>\';
            $db_value = get_post_meta($post->ID, \'property_details_\' . $field[\'id\'], true);
            switch ($field[\'type\'])
            {
            case \'select\':
                $input = sprintf(\'<select id="%s" name="%s">\', $field[\'id\'], $field[\'id\']);
                foreach($field[\'options\'] as $key => $value)
                {
                    $field_value = !is_numeric($key) ? $key : $value;
                    $input.= sprintf(\'<option %s value="%s">%s</option>\', $db_value === $field_value ? \'selected\' : \'\', $field_value, $value);
                }

                $input.= \'</select>\';
                break;

            default:
                $input = sprintf(\'<input %s id="%s" name="%s" type="%s" value="%s">\', $field[\'type\'] !== \'color\' ? \'class="regular-text"\' : \'\', $field[\'id\'], $field[\'id\'], $field[\'type\'], $db_value);
            }

            $output.= $this->row_format($label, $input);
        }

        echo \'<table class="form-table"><tbody>\' . $output . \'</tbody></table>\';
    }

    /**
     * Generates the HTML for table rows.
     */
    public

    function row_format($label, $input)
    {
        return sprintf(\'<tr><th scope="row" class="width-50">%s</th><td>%s</td></tr>\', $label, $input);
    }

    /**
     * Hooks into WordPress\' save_post function
     */
    public

    function save_post($post_id)
    {
        if (!isset($_POST[\'property_details_nonce\'])) return $post_id;
        $nonce = $_POST[\'property_details_nonce\'];
        if (!wp_verify_nonce($nonce, \'property_details_data\')) return $post_id;
        if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) return $post_id;
        foreach($this->fields as $field)
        {
            if (isset($_POST[$field[\'id\']]))
            {
                switch ($field[\'type\'])
                {
                case \'email\':
                    $_POST[$field[\'id\']] = sanitize_email($_POST[$field[\'id\']]);
                    break;

                case \'text\':
                    $_POST[$field[\'id\']] = sanitize_text_field($_POST[$field[\'id\']]);
                    break;
                }

                update_post_meta($post_id, \'property_details_\' . $field[\'id\'], $_POST[$field[\'id\']]);
            }
            else
            if ($field[\'type\'] === \'checkbox\')
            {
                update_post_meta($post_id, \'property_details_\' . $field[\'id\'], \'0\');
            }
        }
    }
}
new forrent_details_Meta_Box;

View Image

1 个回复
最合适的回答,由SO网友:socki03 整理而成

我会尝试使用HTML Unicode 版本,以查看其中任何一个是否有效,并且在选项中,可以将它们与完整的单词而不是符号进行阵列。

    array(
        \'id\' => \'before-price-label-for-example-per-month\',
        \'label\' => \' Before Price Label :\',
        \'type\' => \'select\',
        \'options\' => array(
            \'euro\' => \'&euro;\',
            \'usd\' => \'$\',
        ),
    ) ,
只需确保第一个值是唯一的,因为它们是存储在数据库中的值。第二个值只是标签,所以可以说\'$euro; - Euro\'\'$ - US Dollar\' 或者类似的东西。

根据您的代码,我有点不确定这是如何在前端使用的,但如果您需要这方面的帮助,请修改您的问题。

结束

相关推荐

Encoding Method for URLs?

WordPress是否有一种编码URL的方法或API,类似于在URL中使用标题时生成部分URL的方式?我正在编写一个生成URL的插件,并希望使用与其他所有插件相同的方法。例如,我在标题中键入“这是我的博客文章”,然后生成“这是我的博客文章”。