• ronnyrook

    (@ronnyrook)


    Hello WordPress people,

    At the moment I’m building a plugin.
    Via a form people can add some data that will be inserted in a database table.
    At the same page I’m reading that table the display al the content added like :
    See this picture.

    Now what I want is the following:
    When the user presses the ‘Aanpassen’ (Dutch for Edit) button, I want to load a file or action or something that displays the form with the data of the current row.

    I know how to deal with the query, update and so on.
    But I don’t know how to make the edit part.

    I worked with some custom CMS things before where I could edit by a action that loaded a new page like:

    function action_index() {
       // standard view
    }
    
    function action_edit() {
      // edit view
    }

    But I could not find anything like this in de WordPress codex.

    Someone who can help me out?
    Thanks! 🙂

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Don’t forget WP is not a true CMS, but it is powerful enough to be easily extended. The Codex will not help much because you have custom data in a custom table. Basic CMS stuff, but nothing to do with blogs. You will need to develop custom code to handle your custom data in a custom table. It’s not too bad, even if you are starting from the beginning.

    I will outline the basic process. Once this is working, you will probably want to enhance it with jQuery and AJAX. Each aanpassen button should result in a GET request for your PHP edit form, with the row ID as an URL parameter.

    Your edit form should require_once wp-admin/admin.php, assuming the user should be logged in to be editing content. This allows your form to use WP resources and functions. Your form can use the row ID to get the corresponding data from the DB table. Assuming this is in the WP DB, you can use the $wpdb global object to get the data. Also confirm the user has capability to do this edit.

    Your form can now send content to the user with it’s fields pre-populated with the current data. The form should also contain hidden fields with the row ID and a nonce to secure the transaction.

    The user now edits the data. The form is submitted as a POST request this time. It can be to the same PHP page that served the form based on a GET request.

    When your form page receives the POST request, it behaves very differently than a GET request. It still requires once admin.php. It must also verify again the user has the capability to do this edit and that the nonce is valid. If everything checks out, the fields are sanitized and the DB table is updated with the new data. On success, perhaps the original page with the aanpassen buttons is served, with the updated contents.

    That pretty much covers it. The code required is not much more involved than typing the process in plain English. Have fun!

Viewing 1 replies (of 1 total)
  • The topic ‘Plugin Edit file/table data?’ is closed to new replies.