0

Does anyoone know if its possible to check a custom field value for a character such as a currency symbol and then remove it from the field value so that the field contains a numerical value, not an alphanumercial / text value ?

If so, what would be the best approach for this ?

The field auto updates so it would need to be some kind of regular conditional test and modification - is there a way to attach the test to trigger when the field updated ?

Maybe this would require some kind of field mapping ?

  • Just thought about it and something like this, mapping the value into a new field would do it I think; <?php global $post; $pricewithsymbol = get_post_meta( $post->ID, 'THEORIGINALFIELD', true ); if (strpos($pricewithsymbol, '$') !== false) { $pricewithoutsymbol = preg_replace("$", "", $pricewithsymbol); } update_post_meta($post_id, 'THENEWFIELD', $pricewithoutsymbol); ?> – Nic Windley Jul 23 '13 at 16:14
  • How would I que this so that it only ran when viewing or editing post pages ? – Nic Windley Jul 23 '13 at 16:29
  • This is more a PHP/JavaScript question than a WordPress one. – Wyck Jul 23 '13 at 16:52
  • @Wyck, that actually depends on where he is using this. Where are you using this? You trying to filter this out of the edit page? Or on display? – GhostToast Jul 23 '13 at 17:09
2

You might try this:

<?php
$str = 'This is a very large number 999,999,999.999 !';
echo $str = preg_replace( '/[^\d,.]/', '', $str );

to only allow:

  • digits (0-9)
  • commas (,)
  • dots (.)

You might then consider filters like:

add_{$meta_type}_metadata
update_{$meta_type}_metadata
get_{$meta_type}_metadata

where $meta_type is the type of object metadata (comment, post or user).

ps: just remember to take backup before testing these filters ;-)

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.