0

The function below is intended to return a list of the WordPress submenu pages.

function get_admin_menus() {
    global $submenu, $menu, $pagenow;
    if ( current_user_can('manage_options') ) {
        if( $pagenow == 'index.php' ) {
        foreach ( $submenu as $index => $menu_item ) {
            foreach ( $menu_item as $value) {
      
                    $submenu_page = $value[2];
                
                    // return $submenu_page;
                    echo $submenu_page;

                }
            }
        }
    }
}

Adding the action add_action( 'admin_notices', 'get_admin_menus' ); displays a list of the submenu items. This only works with echo.

I would like to return the values into another function however when using return $submenu_page with echo get_admin_menus() does not appear to work.

For example:

function test () {
  echo get_admin_menus();
}

What am I missing or doing wrong?

1 Answer 1

1

The foreach surrounding your return line means only the first item will be returned - perhaps that's what you want?

If not, then you should assign $value[2] to a local variable (concatenating if you want a string, or an array perhaps?) and the return the local variable after the loop.

function get_admin_menus() {
    global $submenu, $menu, $pagenow;
    $r = '';
    if ( current_user_can('manage_options') ) {
        if( $pagenow == 'index.php' ) {
        foreach ( $submenu as $index => $menu_item ) {
            foreach ( $menu_item as $value ) {
                    $r .= $value[2];
                }
            }
        }
    }
    return $r;
}
0

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.