How to redirect to action on custom page within admin section

Currently I’m developing(well at least trying to;) plugin which will handle custom gallery of images. I stumbled upon one problem though. I’ll try to picture it.

I’ve created custom menu page on which I can edit gallery options as well as I can add/delete/edit elements of gallery. I did add link to every gallery item so after choosing ‘edit’ I could be able to modify element.

Read More
function column_rls_name($item) {
  $actions = array(
            'edit'      => sprintf('<a href="?page=%s&action=%s&rls_element=%s">Edit</a>',$_REQUEST['page'],'edit',$item['rls_name']),
        );
  return sprintf('%1$s %2$s', $item['rls_name'], $this->row_actions($actions) );
}

Problem begins when ‘edit’ on element is clicked. It should redirect to settings page.

Here I have a question. What is the best way to do this. Are there any helper functions/hooks/etc out there which would help in creating such page (and detect redirection).

Of course I could do it in function responsible for displaying main gallery options like:

if($_GET['action']=='edit')
    //then do sth
else
    //display main gallery options

but I don’t think it’s the best way.

I would like to know which functions I could use, from creating custom link, creating custom page and registering them.

What method should be used when action=edit is triggered. How to spot it was requested on the actual page. How to show then page related to the content.

wp-admin/options-general.php?page=rls_logotypes&action=edit&rls_element=element1

Related posts

Leave a Reply

1 comment

  1. Get the url of a specific admin page (e.g. the settings page (options-general.php)):

    admin_url( 'options-general.php' )
    

    Codex admin_url()

    Adding query arguments to an url:

    $params = array(
        'page'          => $_REQUEST['page'],
        'action'        => 'edit',
        'rls_element'   => $item['rls_name']
    );
    
    $url = add_query_arg( $params, $org_url );
    

    Codex add_query_arg()

    Put a and b together:

    $params = array(
        'page'          => $_REQUEST['page'],
        'action'        => 'edit',
        'rls_element'   => $item['rls_name']
    );
    
    $url = add_query_arg( $params, admin_url( 'options-general.php' ) );
    

    Now we can create links:

    public function column_rls_name( $item ){
    
        $actions = array(
        'edit'  => $this->create_admin_link(
                            array(
                                'text'      => 'Edit',
                                'action'    => 'edit',
                                'item'      => $item['rls_name']
                            )
                    )
        );
    
        return sprintf( '%1$s %2$s', $item['rls_name'], $this->row_actions( $actions ) );
    
    }
    
    public function create_admin_link( $args = array() ){
    
        $params = array(
            'page'          => $_REQUEST['page'],
            'action'        => $args['action'],
            'rls_element'   => $args['item']
        );
    
        $url = add_query_arg( $params, admin_url( 'options-general.php' ) );
    
        return sprintf( '<a href="%s">%s</a>', $url, $args['text'] );
    
    }