Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Problem:

Before I created meta-boxes I did things the old fashion way by entering custom fields directly. I went through the process of filling in the ID and the Value by hand and hitting the "update button". I would input whatever I wanted, spaces and . (periods) and they would be correctly applied in my database (ERROR FREE). However I no longer want to do this manually which is why I have created meta-boxes, but since I began using them, special characters like . and spaces wont take, resulting in a blank field when I attempt to update the post in order to save the meta-box values. What am I missing and why do the rules for special characters differ when you manually enter the ID as opposed to having it preset through meta-boxes. Ultimately how can I create meta-box array ID's with special characters?

Note: All my non-special character meta-boxes function properly. So the problem is NOT in the functionality of the code

Details:

I am using the mp3 player plugin "mp3-jplayer" which uses custom fields to display audio. The custom field format (which the creator of this plugin determined and I cannot change myself) is...

ID = TrackNumber mp3.Artist

(note the space and the period above, this is what trips up the submission of the metabox value)

Value = SongTitle@SongURL

enter image description here

At the moment I am using meta-boxes to input custom fields as follows:

$meta_boxes[] = array(
'id' => 'musicboxes',
'title' => 'Music Box Head',
'pages' => array('post'),
'context' => 'normal',
'priority' => 'low',

'fields' => array(
array(
        'name' => 'Musicbox 1',
        'desc' => 'ID',
        'id' => '1 mp3.Artist',
        'type' => 'text',
        'std' => '',
    )
)
);

However, when I input in the array ID field "1 mp3.Artist" the custom field does not update correctly (in other words, remains blank) and I'm thinking it's because there is a space between the number and the word "mp3". So how would I (if its possible) still be able to keep the space so that the custom field functions properly with the mp3-jplayer plugin.

share|improve this question
    
that could be the main reason since spaces and periods are not valid in the name attribute of an input tax, mainly because of the GET restrictions. –  Bainternet Nov 16 '11 at 10:29
    
Any creative way around this? Would I be better off changing the template for the plugin so that the space and period aren't needed? –  mmaximalist Nov 16 '11 at 10:58
    
Any progress? Was one of the answers helpful? –  toscho Oct 6 '12 at 22:26

3 Answers 3

You may have build your meta box wrong: If your custom field names are not valid variable names PHP will convert all characters not matching [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff] to underscores. The reason: register_globals. A request field must be able to work as a variable.

There are two workarounds:

  1. Name the field like an array: <input name="foo[TrackNumber mp3.Artist]">. Then just parse foo to get the value. That's what WordPress does with the custom field box.

  2. Search for TrackNumber_mp3_Artist and convert the value before you save it to the database.

share|improve this answer

Have you tried escaping the text before it's saved to the database?

esc_textarea

$mp3ID = esc_textarea($_REQUEST['_mp3IDBoxData']);
update_post_meta(esc_attr($post_id), '_mp3IDBoxData', $mp3ID);
share|improve this answer

In case you are still looking for an answer; I think you will have to get your hands dirty on this. First off, you need to trim the value that gets registered in the Name custom-field ("1 mp3.Artist").

For example: $id = trim($field['id']); $newID = get_post_meta($post->ID, $id, true);

Hope that helps. I haven't tested it, but the logic behind it should do the trick. Good luck!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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