• Resolved kalsan

    (@kalsan)


    Dear forum,

    I’m struggeling with the philosophy of WordPress and I’d like to know what “the WordPress” way would be to manage a database from the administrative interface.

    To illustrate my needs, consider the following toy example:
    ToyAnim: A WordPress plugin that displays animated circles using shortcodes. In any post, the user may enter [toyanim template="crazy-red-one" /]. The plugin would then look in its animation database for an animation object with the ID crazy-red-one, load all the values (e.g. radius 50, color red, background white, speed 200, threshold 2, randomness 80 etc.) and substitute the shortcode by an animated circle.
    ToyAnim would provide a menu in the administrative interface: ToyAnim -> Manage animations. Clicking the menu entry would open a list of the user-defined animations that the user can edit or delete, as well as a button “add new animation”. When creating a new anim or editing an existing one, another admin page should open and display a form letting the user enter the new values for the animation (name, radius, color, …). Upon pressing the “Save”-button, the database would be updated and thus all the shortcodes referring to that animation ID would produce a different animation (whee!).

    My problem is how to modify the database from the administrative area. I tried to use a menu page and I managed to read in content of a custom table. However:
    -> I don’t know how I could implement the “Add new animation”- or “Edit”-Buttons: What would href be? Writing something like <a href="<?php echo plugins_url('editor.php', __FILE__); ?>">Edit</a> produces a working link, but since editor.php is an admin page, it is simply wrapped in <div class="wrap"><p>content</p></div>, therefore the page is not displayed correctly as it would if a menu button pointing to it was clicked.
    -> For the same reason, I don’t know what action of the edit form would be. Outside WordPress I’d simply have used something like <form method="post" action="submit.php">...</form>, but here submit.php cannot be used as action since admin pages are called in WordPress using GET, not the direct URL pointing to them.

    I conclude that I’m thinking the wrong way. Alternative solutions would be to forget the idea of a custom data base and:
    -> Save my animation objects as custom post types. This doesn’t seem very elegant to me, since we’re not really talking about posts (animation objects are simply read when generating the shortcode, never displayed to the visitor).
    -> Or save them as options: again, this seems wrong since animation objects are not options. Especially, a user may create an arbitrary amount of them which is conceptually different from static options.

    So what would be the WordPress way to save / edit such animation objects?

    Sorry for the long post! I hope the toy example illustrates my need well enough. I’m happy to clarify anything that I explained badly. Any idea would be much appreciated.

    Cheers,
    Kalsan

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    You should probably get over the idea of WP posts being for blog posts. These days posts are merely data containers. Plugin devs have used post objects for all manner of things completely unrelated to blogging ??

    So you could use post objects to contain your data. The advantage is some built in user interface exists so you do not need to recreate it. The disadvantage is the default UI may not be appropriate and you need to build a new one anyway. Using a custom table to store animation objects also makes sense, especially if queries need to easily work with data that is not part of the normal post schema.

    For custom tables added to the WP database, use wpdb class methods, accessed through the global $wpdb instance.

    Because menu items are supposed to be settings pages, I’d avoid trying to execute functions that manipulate the DB directly from menu items. It’d be better to build a user interface page that does all the actions. That page is still accessed by adding it to the menu system, everything else could be managed by sending AJAX requests or by submitting forms. You can post forms to admin-post.php. Part of the data will be an ‘action’ parameter that is used to build an action tag to which you add an action callback that does whatever needs doing. It’s similar to AJAX but there is no need for javascript.

    I’ve found building pages that are added to the menu to be rather confusing when things besides settings are involved. I like to build a custom page template, and add a page post type based on it. The “page” I add to the menu merely forwards to the actual page by way of inline javascript. This last bit is obviously not “The WordPress Way”. Feel free to build similar functionality into a normal settings page for the WP way. If everything is done through forms or AJAX you shouldn’t run into any difficulty.

    Thread Starter kalsan

    (@kalsan)

    Dear bcworkz,

    thank you so much for your post! You answer is exactly the kind of thing I was hoping for. I’ll have tons of fun trying out all your suggestions ??

    Thanks again,
    Kalsan

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Managing a database from the admin area’ is closed to new replies.