How do i save plugin-specific info?

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 ?

Read More

Any help appreciated.
Thanks!

Related posts

2 comments

  1. 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

  2. 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.

Comments are closed.