1

I have a plugin that creates two pages in admin menu:

/wp-admin/admin.php?page=list-records

/wp-admin/admin.php?page=add-edit-record

I'm regularly using add-edit-record to to create some sort of record and to put it in wp database. On the other hand page list-records lists all the records I already created plus allows me to delete record. I want to let add-edit-record also to load certain existing record from database and let me edit it (and save afterwards of course). But how can I call add-edit-record with information which record I want to open and work on?

My idea (maybe naive) is either save ID of edited record in some config option of wp-database and always check that item when opening add-edit-record page - which seems to me very complicated. Or call add-edit-record page with certain data as a parameter, like

/wp-admin/admin.php?page=add-edit-record&edit-id=999

but here I have no idea how can I obtain this parameter in my add-edit-record page.

Maybe I'm thinking in completle wrong direction so any advice would be appreciated. Thanks.

1 Answer 1

2

Go with your last idea - generate a URL like so:

$raw_url = admin_url( 'admin.php?page=add-edit-record&edit-id=' . $id );
$esc_url = esc_url( $raw_url ); // For href attributes and the like

And then in your handler for add-edit-record:

if ( ! empty( $_GET['edit-id'] ) && $id = absint( $_GET['edit-id'] ) ) {
    if ( ! $record = get_record( $id ) )
        wp_die( 'Nope' ); // Or something similar
}
2
  • Excellent, it works! But in general, is this correct way how plugins should internally communicate? Doesn't violate it some rules of Wordpress?
    – Artaban
    Sep 17, 2015 at 12:03
  • Nope. It's standard procedure for almost any web-based server-side application to pass arguments between requests in URL parameters, including WP. Sep 17, 2015 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.