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
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.
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:
Name the field like an array:
<input name="foo[TrackNumber mp3.Artist]">
. Then just parsefoo
to get the value. That’s what WordPress does with the custom field box.Search for
TrackNumber_mp3_Artist
and convert the value before you save it to the database.Have you tried escaping the text before it’s saved to the database?
esc_textarea
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!