Last Updated: February 25, 2016
·
528
· georgiee

seriously? ridiculous wp coder

just finished debugging code from a freelancer.
this wp function will always extract the first segment of an url. The programmer used that url to match an hardcoded group of strings to display some special content.
BUT testing this on my local machine, where the page doesn`t sit on the root gives me a bunch of useless ids and therefore a bunch of not so special content. Annoying that I spent the last 30 minutes to find that.

// www.example.com/a/b/c
// this will always extarct the first segmemnt
//but localhost/x/y/z/a/b/c will fail as x != a
//stupid lazy freelancer
function page_section_id() {
    global $post;

    /* Try to get the section from the URL path */
    if (!is_front_page()) {
        $url_parts = explode("/", $_SERVER["REQUEST_URI"]);
        if (count($url_parts) >= 2) {
            //print_r($url_parts);
            $section_slug = $url_parts[1];
            //echo "***$section_slug***";
            //echo "####".$_SERVER["REQUEST_URI"]."####";
            $page = get_page_by_path("/$section_slug");
            //print_r($page);
            if ($page->ID != 120) {
                return $page->ID;
            }
        }
    }

    /* otherwise return the post parent ID */
    return $post->post_parent;
}

edit: here my temporary fix as I do NOT understand what the freelancer intended to do.

function page_section_id() {
    global $post; 
    /* Try to get the section from the URL path */
    $id = $post->ID;
    if (!is_front_page() && $id!=120) {
      return $id;
    }
    /* otherwise return the post parent ID */
    return $post->post_parent;
}