Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am creating a plugin for a custom admin menu based in the admin area.

My wordpress template is consisted of 5 pages with different templates and some custom variables that the end user would be allowed to edit with a custom role I have assigned to him, eg. grunt. The reason I am doing this is so that the user could have the minimum knowledge of interacting with the page admin. For example, I have a page with 6 custom fields that define how the template should be shown, while in another page these field variables change to alter the display effect.

So in my admin menu I would have:

Site Management
- Page 1 Admin -> This makes a carousel with 2 items max
- Page 2 Admin -> This is a plain text page
- Page 3 Admin
- Page 4 Admin -> This is a carousel with 4 items max
- Page 5 Admin

And so on.

Since my menu is the same as the pages shown + Homepage link, I have used wp_get_nav_menu_items() function to get those menus. But now I am in a dilemma, trying to access every page's post to edit.

Here is how I list the pages on my admin menu:

$options = get_nav_menu_locations();
$menu_items = wp_get_nav_menu_items($options['main_nav']);
$i = 0;
foreach ((array) $menu_items as $key => $v):
    $post = get_post($v->object_id);
    setup_postdata($post);
    add_submenu_page('theme_admin2', 'Administration', $v->title, 'edit_posts', 'theme_admin_'.$v->object_id, array(&$this, 'display_page_test'));
endforeach;

Each menu item is a submenu page at the moment. How can I pass the $v->object_id variable to the display_page_test function of my plugin class so that I would differentiate my output as well?

UPDATE
I am referring to creating an admin menu using the public menu items as subitems (to the admin). Once a user clicks the subitem it will be an edit screen for the page: title, body and image. This is the only view that the customer will see + profile. I've created that page, but I cannot send the object id of each menu to the callback function that creates the submenu pages.

share|improve this question
    
What exactly are your referring to when you say "admin menu"? So far your question is quite confusing as it seems you're mixing the admin UI (back end) menu (on the left) with public menus in themes (front end). –  kaiser Sep 2 '13 at 11:42
    
I am referring to creating an admin menu using the public menu items as subitems. Once a user clicks the subitem it will be an edit screen for the page: title, body and image. This is the only view that the customer will see + profile. I've created that page, but I cannot send the object id of each menu to the callback function that creates the submenu pages. –  Panagiotis Sep 2 '13 at 11:51
    
Please update your question with that info. –  kaiser Sep 2 '13 at 12:09
1  
Updated as requested. –  Panagiotis Sep 2 '13 at 12:27

1 Answer 1

up vote 0 down vote accepted

You do not post how you add the top level menu page, however I think when you click on a submenu item you have an url like:

http://www.example.com/wp-admin/admin.php?page=theme_admin_{$pageid}

So the id of page is in the $_GET['page'] variable with a fixed string prepended to it.

So why don't use something like:

function display_page_test() {
  $id = (int)str_replace('theme_admin_', '', $_GET['page']);
  $page = get_post($id);
  $action = isset($_GET['action']) ? $_GET['action'] : '';
  // just for test
  switch ( $action ) {
      case 'edit' :
        echo 'Do you want to edit ' . $page->post_title. '?';
        return;
      case 'delete' :
        echo 'Do you want to remove' . $page->post_title . '?';
        return;
      default :
        echo 'Ok, now we only show ' . $page->post_title;
        echo '<pre>';
        print_r($page);
        echo '</pre>';
        return;
   }
}

Edit

If you do not like the str_replace stuff, just use the clean post id in the variable:

add_submenu_page('theme_admin2', 'Administration', $v->title, 'edit_posts', 'theme_admin_' . $v->object_id.  '&post_id=' . $v->object_id, array(&$this, 'display_page_test'));

Then rely on $_GET['post_id']

share|improve this answer
    
I've tried that, it's sort of a quick and dirty way to do it. How do you handle the actions on the submenus? For example ?page=theme_admin_{$pageid}&action=edit&postid={same as $postid}? –  Panagiotis Sep 2 '13 at 13:15
    
@Panagiotis it works also in your example. $_GET['page'] is = theme_admin_{$pageid} also in the url you post here as example. I'me editing answer to handle other variables. –  G. M. Sep 2 '13 at 13:18
    
Thanks. It's sort of what I will eventually follow for this approach. –  Panagiotis Sep 2 '13 at 13:21
    
@Panagiotis see the edit for handle actions. –  G. M. Sep 2 '13 at 13:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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