Skip to content

Instantly share code, notes, and snippets.

@DevWael
Created June 27, 2019 11:33
Show Gist options
  • Save DevWael/f28966a9db54beb8ab606e0a416cce44 to your computer and use it in GitHub Desktop.
Save DevWael/f28966a9db54beb8ab606e0a416cce44 to your computer and use it in GitHub Desktop.
create wordpress admin menu links and sub links dynamically with external redirects functionality
<?php
function idl_menu_build_data() {
$data = [
[
'slug' => 'rtdhrt',
'page_title' => 'first page',
'menu_title' => 'first page',
'cap' => 'manage_options',
'callable_function' => '',//write a function that holds all content of this page or leave it empty to listen to the redirect url
'redirect_url' => 'https://google.com',
'icon_url' => false,
'position_number' => 15,
'children' => [
[
'slug' => 'tger',
'page_title' => 'etertrer sub',
'menu_title' => 'fiereetrrst sub',
'cap' => 'manage_options',
'callable_function' => '',//write a function that holds all content of this page or leave it empty to listen to the redirect url
'redirect_url' => 'https://google.com',
],
[
'slug' => 'rtjdse',
'page_title' => 'second sub',
'menu_title' => 'second sub',
'cap' => 'manage_options',
'callable_function' => '',//write a function that holds all content of this page or leave it empty to listen to the redirect url
'redirect_url' => 'https://google.com',
],
[
'slug' => 'rtyhktur',
'page_title' => 'third sub',
'menu_title' => 'third sub',
'cap' => 'read',
'callable_function' => '',//write a function that holds all content of this page or leave it empty to listen to the redirect url
'redirect_url' => 'https://google.com',
],
]
],
[
'slug' => 'rteytudhrt',
'page_title' => 'second page',
'menu_title' => 'second page',
'cap' => 'manage_options',
'callable_function' => 'wdl_welcome_page_content',
'redirect_url' => 'https://google.com',
'icon_url' => false,
'position_number' => 20,
'children' => [
[
'slug' => 'tgetrtr',
'page_title' => 'first sub',
'menu_title' => 'first sub',
'cap' => 'manage_options',
'callable_function' => '',//write a function that holds all content of this page or leave it empty to listen to the redirect url
'redirect_url' => 'https://google.com',
],
[
'slug' => 'rrttjdse',
'page_title' => 'second sub',
'menu_title' => 'second sub',
'cap' => 'manage_options',
'callable_function' => '',//write a function that holds all content of this page or leave it empty to listen to the redirect url
'redirect_url' => 'https://google.com',
],
[
'slug' => 'rtyuyjkhktur',
'page_title' => 'third sub',
'menu_title' => 'third sub',
'cap' => 'read',
'callable_function' => '',//write a function that holds all content of this page or leave it empty to listen to the redirect url
'redirect_url' => 'https://google.com',
],
]
],
];
return apply_filters( 'idl_menu_build', $data );
}
add_action( 'admin_menu', 'idl_welcome_page' );
function idl_welcome_page() {
$menus_data = idl_menu_build_data();
foreach ( $menus_data as $menu_item ) {
add_menu_page(
$menu_item['page_title'],
$menu_item['menu_title'],
$menu_item['cap'],
$menu_item['slug'],
empty( $menu_item['callable_function'] ) ? function(){} : $menu_item['callable_function'],
$menu_item['icon_url'],
$menu_item['position_number']
);
if ( isset( $menu_item['children'] ) && ! empty( $menu_item['children'] ) ) {
foreach ( $menu_item['children'] as $child ) {
add_submenu_page(
$menu_item['slug'],
$child['page_title'],
$child['menu_title'],
$child['cap'],
$child['slug'],
empty( $child['callable_function'] ) ? function(){} : $child['callable_function']
);
}
}
}
}
add_action( 'admin_init', 'idl_menu_item_redirect' );
function idl_menu_item_redirect() {
$menus_data = idl_menu_build_data();
$menu_redirect = isset( $_GET['page'] ) ? $_GET['page'] : false;
foreach ( $menus_data as $menu_item ) {
if ( $menu_redirect == $menu_item['slug'] ) {
wp_redirect( $menu_item['redirect_url'] );
exit();
}
if ( isset( $menu_item['children'] ) && ! empty( $menu_item['children'] ) ) {
foreach ( $menu_item['children'] as $child ) {
if ( $menu_redirect == $child['slug'] ) {
wp_redirect( $child['redirect_url'] );
exit();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment