0

I have a custom meta box created using Rilwis meta box script(http://www.deluxeblogtips.com/meta-box/) inside my theme functions.php.

It has 2 text fields, one field is required to fetch results from api, so, it needs to be visible on the custom meta box. But, the other field stores a serialized array which is the response fetched from external api. So, I want to make this field hidden.

Please tell me how can I hide the other field. Here is the code which defines the two text fields:

if (!class_exists('F8_Stores')) {
    class F8_Stores {

        private $fields = array();
        private $validation = array();
        private $prefix = 'f8_';

        public function __construct() {

            add_action('admin_init', array($this, 'register_store_meta_box'));
        }

        public function register_store_meta_box() {
            $this -> fields = $this -> add_custom_fields();
            $this -> validation = $this -> add_validation_rules();

            $meta_box = array('id' => 'f8_stores', 'title' => 'Online Retail Stores', 'pages' => array('post', 'page'), 'context' => 'normal', 'priority' => 'high', 'fields' => $this -> fields, 'validation' => $this -> validation);
            new RW_Meta_Box($meta_box);

        }

        private function add_custom_fields() {
            $fields = array( array('name' => 'Amazon US Product ASIN', 'desc' => 'Please enter product ASIN', 'id' => $this -> prefix . 'amazon_us_asin', 'type' => 'text', 'std' => false, 'clone' => false, ),
             array('name' => 'Amazon US Store Cache', 'desc' => 'Users should not see this', 'id' => '_' . $this -> prefix . 'amazon_us_store_cache', 'type' => 'text', 'clone' => false, ), );
            echo $fields[1]['id'];
            return $fields;
        }

        private function add_validation_rules() {
            $validation = array();

             return $validation;
        }

    }

}
$F8_Stores = new F8_Stores();

I am using this inside a class, and despite adding '_'in front of 'id' of text field 2, the field is still visible inside metabox. Don't know what I am missing?

Any help is appreciated...thanks!

P.S. I am using Rilwis Meta Box 4.2.3 and Wordpress 3.5 running on xampp.

Update: I have now posted my actual class responsible for generating metabox. Update: Thanks everyone for response, the problem is now fixed.

2 Answers 2

1

Using a leading underscore will hide the field's value from the default WordPress Custom Fields section, not the meta box you create via the plugin.

If you don't want the field visible in your custom meta box, don't add the field to the meta box at all, it really serves no purpose there anyway. You can manage the field's value in your code directly via update_post_meta and get_post_meta.

2
  • You're right here @Milo. Actually no custom field is required here.
    – fischi
    Commented Jan 17, 2013 at 17:45
  • @Milo: Thanks, and I was wondering why adding underscore in front of field id wasn't hiding it. And, thanks for suggesting, quickly removed field from the metabox, and the code is working. Commented Jan 17, 2013 at 17:56
1

You don't have to put the _ in front of 'id', but in front of the prefix.

Your code should work like that:

$fields = array(
    array(
        'name' => 'Text field 1',
        'desc' => 'This is required',
        'id' => '_' . $this -> prefix . 'field_1',
        'type' => 'text',
        'std' => false,
        'clone' => false,
    ),
    array(
        'name' => 'Text field 2',
        'desc' => 'This is required',
        'id' => '_' . $this -> prefix . 'Text field 2',
        'type' => 'text',
        'clone' => false,
    ),
);
8
  • Its not working, I had already tried this. Commented Jan 17, 2013 at 17:16
  • Can you tell me how the id field is now declared in your metabox?
    – fischi
    Commented Jan 17, 2013 at 17:17
  • @Chetan - edit your question to show where you set prefix with leading underscore.
    – Milo
    Commented Jan 17, 2013 at 17:22
  • @Milo: I have updated the code. Commented Jan 17, 2013 at 17:25
  • @Chetan can you please tell me how your plugin outputs the ID of this input field?
    – fischi
    Commented Jan 17, 2013 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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