fauzanjeg

JNews || Add Custom Menu in My Account Page Sidebar

Mar 30th, 2026
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.01 KB | None | 0 0
  1. /**
  2.  * Central configuration for child account menus.
  3.  * Modify this array to add or change custom account menu items.
  4.  * Keys are internal endpoint keys; each item may include: slug, label, title, text, url, after
  5.  */
  6. if ( ! function_exists( 'jnews_child_account_menus' ) ) {
  7.     function jnews_child_account_menus() {
  8.         return array(
  9.             'custom-menu'  => array(
  10.                 'slug'  => 'custom-menu',
  11.                 'label' => 'custom_menu',
  12.                 'title' => esc_html__( 'Custom Menu', 'jnews-child' ),
  13.                 'text'  => esc_html__( 'Custom Menu', 'jnews-child' ),
  14.                 'url'   => esc_url( 'https://www.jegtheme.com/' ),
  15.                 'after' => 'order',
  16.             ),
  17.             // Example additional item
  18.             'another-menu' => array(
  19.                 'slug'  => 'another-menu',
  20.                 'label' => 'another_menu',
  21.                 'title' => esc_html__( 'Another', 'jnews-child' ),
  22.                 'text'  => esc_html__( 'Another', 'jnews-child' ),
  23.                 'url'   => esc_url( 'https://jnews.local/test-page/' ),
  24.             ),
  25.         );
  26.     }
  27. }
  28.  
  29. if ( ! function_exists( 'jnews_custom_menu_account_page' ) ) {
  30.     /**
  31.      * Add Custom Menu in Account page
  32.      *
  33.      * @param mixed $endpoint
  34.      * @return array
  35.      */
  36.     function jnews_custom_menu_account_page( $endpoint ) {
  37.         $menus = function_exists( 'jnews_child_account_menus' ) ? jnews_child_account_menus() : array();
  38.  
  39.         $to_add = array();
  40.         foreach ( $menus as $mkey => $m ) {
  41.             $to_add[ $mkey ] = array(
  42.                 'slug'  => isset( $m['slug'] ) ? $m['slug'] : $mkey,
  43.                 'label' => isset( $m['label'] ) ? $m['label'] : $mkey,
  44.                 'title' => isset( $m['title'] ) ? $m['title'] : ( isset( $m['text'] ) ? $m['text'] : $mkey ),
  45.                 'url'   => isset( $m['url'] ) ? esc_url( $m['url'] ) : '',
  46.             );
  47.         }
  48.  
  49.         if ( ! empty( $to_add ) ) {
  50.             $endpoint = array_merge( $endpoint, $to_add );
  51.         }
  52.  
  53.         return $endpoint;
  54.     }
  55.     add_filter( 'jnews_account_page_endpoint', 'jnews_custom_menu_account_page', 99 );
  56. }
  57.  
  58. /**
  59.  * Redirect account endpoint path to external/full URL when configured
  60.  */
  61. if ( ! function_exists( 'jnews_custom_menu_account_redirect' ) ) {
  62.     function jnews_custom_menu_account_redirect() {
  63.         if ( is_admin() ) {
  64.             return;
  65.         }
  66.  
  67.         if ( empty( $_SERVER['REQUEST_URI'] ) ) {
  68.             return;
  69.         }
  70.  
  71.         // Get account slug via AccountPage if available
  72.         $account_slug = 'account';
  73.         $endpoints    = array();
  74.         if ( class_exists( 'JNews\\AccountPage' ) ) {
  75.             $account_obj = JNews\AccountPage::getInstance();
  76.             if ( $account_obj && method_exists( $account_obj, 'get_endpoint' ) ) {
  77.                 $endpoints = (array) $account_obj->get_endpoint();
  78.                 if ( isset( $endpoints['account']['slug'] ) ) {
  79.                     $account_slug = $endpoints['account']['slug'];
  80.                 }
  81.             }
  82.         }
  83.  
  84.         // Load configured menus from central config
  85.         $menus = function_exists( 'jnews_child_account_menus' ) ? jnews_child_account_menus() : array();
  86.         if ( empty( $menus ) ) {
  87.             return;
  88.         }
  89.  
  90.         // Normalize request path (strip query string)
  91.         $request_uri = wp_unslash( $_SERVER['REQUEST_URI'] );
  92.         $path        = strtok( $request_uri, '?' );
  93.         $path        = untrailingslashit( $path );
  94.  
  95.         // If WP is installed in a subdirectory, remove the site path prefix
  96.         $home_path = parse_url( home_url(), PHP_URL_PATH );
  97.         if ( $home_path && '/' !== $home_path && 0 === strpos( $path, $home_path ) ) {
  98.             $path = substr( $path, strlen( $home_path ) );
  99.             if ( '' === $path ) {
  100.                 $path = '/';
  101.             }
  102.         }
  103.  
  104.         foreach ( $menus as $mkey => $m ) {
  105.             $slug     = isset( $m['slug'] ) ? $m['slug'] : $mkey;
  106.             $expected = '/' . trim( $account_slug, '/' ) . '/' . trim( $slug, '/' );
  107.  
  108.             if ( $path === $expected || substr( $path, -strlen( $expected ) ) === $expected ) {
  109.                 $target_url = '';
  110.                 if ( isset( $m['url'] ) && ! empty( $m['url'] ) ) {
  111.                     $target_url = $m['url'];
  112.                 } elseif ( isset( $endpoints[ $mkey ]['url'] ) && ! empty( $endpoints[ $mkey ]['url'] ) ) {
  113.                     $target_url = $endpoints[ $mkey ]['url'];
  114.                 }
  115.  
  116.                 if ( empty( $target_url ) ) {
  117.                     continue;
  118.                 }
  119.  
  120.                 wp_redirect( esc_url_raw( $target_url ) );
  121.                 exit;
  122.             }
  123.         }
  124.     }
  125.     add_action( 'template_redirect', 'jnews_custom_menu_account_redirect', 1 );
  126. }
Advertisement
Add Comment
Please, Sign In to add comment