By Flo Wide


2020-08-01 13:50:14 8 Comments

Many functions of wordpress return html strings (like wp_nav_menue() for example). Is there a part of the wordpress api without this mess of pre-formatted nonsens? Something that gives objects and arrays which are intended to work with?

Have a look at this. Compared to modern techniques like jsx this is the hell on earth. Especially i like it how he has to add ."\n" everywhere so that it is readable at all later LOL.

<?php
  
function create_bootstrap_menu( $theme_location ) {
    if ( ($theme_location) && ($locations = get_nav_menu_locations()) && isset($locations[$theme_location]) ) {
         
        $menu_list  = '<nav class="navbar navbar-default">' ."\n";
        $menu_list .= '<div class="container-fluid">' ."\n";
        $menu_list .= '<!-- Brand and toggle get grouped for better mobile display -->' ."\n";
        $menu_list .= '<div class="navbar-header">' ."\n";
        $menu_list .= '<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">' ."\n";
        $menu_list .= '<span class="sr-only">Toggle navigation</span>' ."\n";
        $menu_list .= '<span class="icon-bar"></span>' ."\n";
        $menu_list .= '<span class="icon-bar"></span>' ."\n";
        $menu_list .= '<span class="icon-bar"></span>' ."\n";
        $menu_list .= '</button>' ."\n";
        $menu_list .= '<a class="navbar-brand" href="' . home_url() . '">' . get_bloginfo( 'name' ) . '</a>';
        $menu_list .= '</div>' ."\n";
           
        $menu_list .= '<!-- Collect the nav links, forms, and other content for toggling -->';
         
         
        $menu = get_term( $locations[$theme_location], 'nav_menu' );
        $menu_items = wp_get_nav_menu_items($menu->term_id);
 
        $menu_list .= '<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">' ."\n";
        $menu_list .= '<ul class="nav navbar-nav">' ."\n";
          
        foreach( $menu_items as $menu_item ) {
            if( $menu_item->menu_item_parent == 0 ) {
                 
                $parent = $menu_item->ID;
                 
                $menu_array = array();
                foreach( $menu_items as $submenu ) {
                    if( $submenu->menu_item_parent == $parent ) {
                        $bool = true;
                        $menu_array[] = '<li><a href="' . $submenu->url . '">' . $submenu->title . '</a></li>' ."\n";
                    }
                }
                if( $bool == true && count( $menu_array ) > 0 ) {
                     
                    $menu_list .= '<li class="dropdown">' ."\n";
                    $menu_list .= '<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">' . $menu_item->title . ' <span class="caret"></span></a>' ."\n";
                     
                    $menu_list .= '<ul class="dropdown-menu">' ."\n";
                    $menu_list .= implode( "\n", $menu_array );
                    $menu_list .= '</ul>' ."\n";
                     
                } else {
                     
                    $menu_list .= '<li>' ."\n";
                    $menu_list .= '<a href="' . $menu_item->url . '">' . $menu_item->title . '</a>' ."\n";
                }
                 
            }
             
            // end <li>
            $menu_list .= '</li>' ."\n";
        }
          
        $menu_list .= '</ul>' ."\n";
        $menu_list .= '</div>' ."\n";
        $menu_list .= '</div><!-- /.container-fluid -->' ."\n";
        $menu_list .= '</nav>' ."\n";
  
    } else {
        $menu_list = '<!-- no menu defined in location "'.$theme_location.'" -->';
    }
     
    echo $menu_list;
}
?>

Edit: I dont know why some people vote down this question without giving a reason. I suppose its because there is no "good" part of the wordpress api. At the moment i developing on a java project with spring and its simply a great pleasure to work with. If any new developer find this question: stay away from wordpress. The way to work with wordpress is either "clicking" or coding a plugin which is needed by at least thousand of customers. The latter is very hard because there are already plugins for nearly every problem. Anything else is not worth it.

0 comments

Related Questions

Sponsored Content

5 Answered Questions

[SOLVED] Wordpress - $wpdb->insert - MySQL NOW()

  • 2011-12-19 19:44:58
  • chris
  • 40691 View
  • 33 Score
  • 5 Answer
  • Tags:   wordpress

1 Answered Questions

[SOLVED] Customize WordPress API Query

  • 2020-01-06 13:41:50
  • Leonardo
  • 29 View
  • 0 Score
  • 1 Answer
  • Tags:   wordpress api

2 Answered Questions

[SOLVED] What is the default WordPress API

  • 2016-02-24 17:01:04
  • Chris
  • 334 View
  • 3 Score
  • 2 Answer
  • Tags:   wordpress api

1 Answered Questions

[SOLVED] Wordpress custom API development

  • 2017-11-13 06:13:50
  • Arpit
  • 257 View
  • 0 Score
  • 1 Answer
  • Tags:   wordpress api

1 Answered Questions

[SOLVED] Integrate API authentication to WordPress

  • 2016-11-25 13:31:09
  • StoneSmith
  • 74 View
  • 0 Score
  • 1 Answer
  • Tags:   wordpress api

1 Answered Questions

1 Answered Questions

[SOLVED] Use The Loop to create a custom htmlstructure

  • 2014-02-19 18:36:44
  • user2059370
  • 77 View
  • 0 Score
  • 1 Answer
  • Tags:   php wordpress

Sponsored Content