WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a class that will count the Lines of Code, Source Lines of Code, and the Code disk space used. I would like to save these values into the database so that these values can be calculated only when a new post is saved or updated instead of running the code on every page view

So my question is, what is the best way to make sure these are added/updated everytime a new post is made and everytime that post is updated/edited.

Also where should I store these, in a custom meta field? If so, how can I make sure that the meta box does not show up on the page, it should all be done behind the scenes

share|improve this question
Def: Meta Data. – kaiser Dec 14 '11 at 0:08
TIP: If you use a meta_key starting with an underscore it will be hidden from view(that's how core does it). – t31os Dec 14 '11 at 0:49
Thanks for the tip! – JasonDavis Dec 14 '11 at 1:01
up vote 1 down vote accepted

Best way for storing them is Wordpress' Options Mechanism.

ANd best way to trigger it when a post saved is using Wordpress save_post action.

Example :

add_action( 'save_post', 'count' ); //Execute count() when a post saved/updated
function count()
    your codes here ...

    //save them to DB
    update_option("lines-of-code", $lines);
    update_option("disk-space", $diskspace);
share|improve this answer
I need this saved for every post though so the options thing wouldn't work as it is for saving 1 key/value for the site and not 1 for each post. You save_post action will be useful though – JasonDavis Apr 11 '12 at 9:45

Your Answer


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.