Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Im currently building a plugin that imports some data from an XML feed. This is all working great. Now i want to add some info, letting the user know when the last import happened. "Last import: 03/10/2014"

How should i save that data? does wordpress provide plugin meta in the database or should it go into the options table even though its not actually an option ?

Any help appreciated. Thanks!

share|improve this question
    
The site options should work fine recording the last import time, but John's answer is great if you need to work with the Data later. –  Douglas.Sesar Mar 5 at 16:07

2 Answers 2

up vote 1 down vote accepted

I would recommend you store it as an option

Here is the API:

add_option( $option, $value, $deprecated, $autoload );
update_option( $option, $new_value );
get_option( $option, $default );
delete_option( $option );

For your option I would not set autoload to true for performance reasons ( unless you are grabbing this option on every page ). I would also prefix every option name with a unique identifier e.g. 'MaltheMilthers_plugin_last_import'

Creating a dedicated table will mean writing SQL, and it means your plugin cant be used on hosts such as WordPress VIP or WP Engine.

In future you may also find these APIs useful:

  • User meta
  • Post meta ( and by conjunction attachments and nav menus )
  • Comment meta

Though I wouldn't recommend them for this use case. Term/taxonomy meta is under discussion, and can be added via several plugins, though this also suffers from the same issue a dedicated table would have.

Further Reading

share|improve this answer
    
Thanks! i think i will try this approach. But if i did want to do what John suggests; keeping track of all imports, than the options wouldn't make sense anymore, would it? –  Malthe Milthers Mar 5 at 16:29
    
Also, does that mean that you can't use gravity forms on WP Engine, since they use their own tables? –  Malthe Milthers Mar 5 at 16:30
    
Ah I've checked and yes, WPEngine supports custom tables, if you're importing items from an XML feed and want to know when the last feed import ran, an option would be best, as it's a global piece of information. If you want to know when a post was imported, use post meta. Use a custom post type or a post to store individual imported items, and the post meta API to attach their import date. –  Tom J Nowell Mar 5 at 16:38
    
For tracking multiple import dates, you then have 2 options, either store a list of dates in your option, or do a WP_Query search on the imported posts for the meta that stores the import date, and display those dates ( making sure to only show dates that haven't already been shown so they're all unique and in order ) –  Tom J Nowell Mar 5 at 16:39
1  
Thanks a lot Tom! –  Malthe Milthers Mar 5 at 16:48

Depending on what your use case is, you might consider adding a table and tracking the import actions by adding a new record with some meta info on each import.

https://codex.wordpress.org/Creating_Tables_with_Plugins

This way you can reference the newest import by the date the record was created, as well as keep track of any import failures/other relevant info.

share|improve this answer
4  
If you're going to do this I would instead suggest using a custom post type rather than a custom table. You're almost always better off using the API where it fits rather than trying to reinvent wheels. –  Milo Mar 5 at 16:14

Your Answer

 
discard

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.