Formatting a custom number field to add commas

Code Snippet Related Categories

Reason for needing the Code Snippet

I needed to format a number field (999,999,999), coming from a custom field number-format (no other characters permitted in the number entry field). No “$” no commas or decimal points, etc. to output as a normal currency format.

When displaying the number I could add a “$” in front of the number, but couldn’t find a way to format the number since you can’t do that with CSS.

(This post was viewed 2,850 times) in the last month.)

What the Code Snippet does

This is a combo of a WordPress function added to the functions.php file that creates a shortcode that can then be used in a Beaver Builder Themer text or html module to output the raw number into a currency format.

For example you would enter 10000 into the custom field when editing the post, but when viewing the post the number now looks like $10,000.

What the page looks like Before and After

Here's the Code Snippet

  • Step 1: add this code to your functions.php file
    • function format_my_number($atts) {
      $num = $atts["value"];return number_format($num, 0, '.', ',');
      }
      add_shortcode("custom-number-format", "format_my_number");
  • Step 2: now you  can use a shortcode in a text area to display a number that is entered in a custom number field, but with commas added when displayed:
    [custom-number-format value="PASS-YOUR-CUSTOM-FIELD-VALUE"]

    • this code is NOT dependent on Beaver Builder or Metabox.io, you could use any area that takes a shortcode and alternatively use ACF for example.
  • Step 3: In the case of Beaver Themer and using Metabox.io custom fields you wind up inserting a shortcode by using the Beaver Themer field connectors and passing the custom number field and inserting it inside the custom number format shortcode.
    • The custom field is inserted as a shortcode, in this example it's:

      -- So we wind up with Nested Shortcodes!

    • The complete nested shortcode looks like this:
      [custom-number-format value=""]

Limit search for phrases by using " " around the phrase

Subscribe to Code Snippet updates

You will get an email the morning after we post a new Code Snippet

Something went wrong. Please check your entries and try again.

Other Recent Code Snippets

Bulk remove posts from category 3

Bulk remove posts from category

Posted in ,

After creating a child category I wanted to remove the parent category from 30 posts. I finally searched for “how to remove a post category in bulk edit in wordpress” and and lo and behold the second result led me to this plugin. It works perfectly. I’m only sorry I […]

Read More
wizard of oz lions, tigers, and bears

Privacy, Cookies, & ADA Compliance — Oh My!
— Privacy Pt. 1

Posted in , , , ,

With more and more privacy laws on the horizon, we advise all of our clients to not only have a Privacy Policy in place but also have a strategy to keep it up to date with newly required disclosures. An up to date Privacy Policy can help you avoid significant […]

Read More
Elementor vs Beaver Builder review

Elementor vs Beaver Builder review

Posted in , , ,

I came across an Elementor vs Beaver Builder comparison review that was published in August 2020. While I am pretty biased in favor of the Beaver Builder platform the reviewer seemed to favor Elementor. If you are trying to decide which page builder platform to use I’d encourage you take […]

Read More
Mix and Match your RSS feeds 4

Mix and Match your RSS feeds

Posted in , , ,

Ever want to provide your readers with email updates to your website? WordPress’ built in RSS feed functionality provides an easy way to provide the content to email newsletter services like Mailchimp or Mailerlite. But what if you want to create email updates that don’t include all your new posts, […]

Read More
Online GIF tools for regular and animated GIF images 8

Online GIF tools for regular and animated GIF images

Posted in , ,

I made an animated gif screenshot recording to demonstrate a code snippet, but I wanted to resize the image and make it loop. I found a useful website that has easy tools to do that. I reduced the size by 50% and made it auto-loop 30x. Then optimized it to […]

Read More
When should I NOT use WordPress for a website 11

When should I NOT use WordPress for a website

Posted in , , , ,

click the green switch at the top of the screenshots to compare the WordPress vs the Static HTML versions of the same website the static version is live at: http://halcyonlearning.com/

Read More

Pieter Hartsook

WordPress website coaching, design, implementation, support, and training. Background in Marketing Research and Communications. See my profile at: https://www.linkedin.com/in/hartsook/

Reader Interactions

Comments

  1. Hi Pieter,
    This code really helped me.

    I want to accomplish something similar – do you think you can help me?
    Here’s what I want to do:

    IF custom field value = “specific value” THEN change value to “different value”

Leave a Reply

Your email address will not be published. Required fields are marked *

Skip to content