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

  • Def: Meta Data. – kaiser Dec 14 '11 at 0:08
  • 1
    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

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);
  • 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 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.