0

While inserting Euro symbol in wordpress theme code getting "Non-printable characters were found" error and also the euro symbol was not even shown properly.

I have attached screenshot and also add the code where i am getting this issue.

  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

  • I'm not familiar with this error, which theme is this? Have you contacted the theme author? None of this is in standard WordPress, and I do not see any code in your question, can you elaborate further and provide more information? – Tom J Nowell Oct 15 '18 at 16:33
  • @TomJNowell it was not giving errror in the normal wordpress backend when we install the theme check plugin then it shows the error. But what my doubt is the currency symbol is not working. You can refer the image attached. – James Paul Oct 15 '18 at 16:43
  • Normal WP doesn't have a currency dropdown like the one shown in your image, where is it coming from? Which theme are you using? Is it a premium theme? Or something you built yourself? – Tom J Nowell Oct 15 '18 at 16:47
  • I am building it my self in custom meta field. – James Paul Oct 15 '18 at 16:52
  • 1
    @TomJNowell added entire code.. – James Paul Oct 15 '18 at 17:18
2

I would try using the HTML Unicode versions to see if any of them work, and in the options, you could array them against the full word instead of the symbol.

    array(
        'id' => 'before-price-label-for-example-per-month',
        'label' => ' Before Price Label :',
        'type' => 'select',
        'options' => array(
            'euro' => '&euro;',
            'usd' => '$',
        ),
    ) ,

Just make sure the first values are unique, since they are the ones being stored in the database. The second values are just labels, so you could say '$euro; - Euro' and '$ - US Dollar' or something to that effect.

I'm a little unsure how this is being used on the front side based on your code, but if you need help with that, please amend your question.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.