So I'm working on a small plugin, and this plugin allows users to save data to my db. This data is mostly plain text but 3 fields are will have tags inside them.
1 field can be used to save all kind of code(js/php/html....) into my db, and 2 fields can only accept JS code.
My question how should I save this kind of data into the DB to prevent injections and such.
I am still new to the whole validation stuff so any tips and help would be nice (dont put the links to the wordpress docs in the comments below, I already read those)
My code until now
$wpdb->insert($table,
array(
'field-no-tags' => sanitize_text_field($_POST['field-no-tags']),
'field-have-all-tags' => $_POST['field-have-all-tags'],
'field-have-js-tags' => $_POST['field-have-js-tags']
)
,array(
's%',
's%',
)
);
<form>
<label>No code allowed</label>
<textarea name="field-no-tags"></textarea>
<label>All code allowed</label>
<textarea name="field-have-all-tags"></textarea>
<label>Only JS allowed</label>
<textarea name="field-have-js-tags"></textarea>
<button type="submit">Save</button>
</form>
<script>
tags), right?strip_tags
is obviously insufficient. Are you sure you want to allow PHP code? One of the problems you'll encounter is that nobody usesCDATA
in script tags, and that's why simple XML processing is not enough to filter<script>
tags. Allowing code in text is quite unusual, why do you want to do it this way?