The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End ID the Body Based on URL – WordPress/PHP

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
  • #30732

    I’ve been trying to implement the ID the Body Based on URL wordprsess snippet found here Your text to link…

    First issue seems to be a syntax error

    function curr_virtdir($echo=true){
    $url = explode('/',$_SERVER);
    $dir = $url[1] ? $url[1] : 'home'; // defaults to this if in the root
    $dir = htmlentities(trim(strip_tags($dir))); // prevent injection into the DOM through this function
    if ($echo) echo $dir;
    return echo $dir; // ie. curr_virtdir(false)
    function get_curr_virtdir(){

    I Get a syntax error on this line below

    return echo $dir; // ie. curr_virtdir(false)

    If i remove this line completly the script seems to work for in the following situation

    although I am trying to use the same functions as a conditional for my hardcoded navigation but it doesnt seem to work – any ideas?

  • ">Home

  • ">Solutions

  • ">About us
  • #72560
    Rob MacKay

    yea you need to either return OR echo, you can’t return echo $dir.

    Return is used in functions to pass a value back – but dosen’t need to echo it.

    For example to display the text “Garry”:

    function name() {

    $e = "Garry";

    return $e;


    echo name(); //this would NEED echo because the return dosen't echo.


    function name() {

    $e = "Garry";

    echo $e;


    name(); //this would NOT need echo because the echo is already in the function.

    The best practice is to return the value and then echo it out.

    It might work after that because you are actually returning a value, where as before you were not.

    Does that make sense?


    thanks Robskiwarrior – i think I am getting it now!

    Rob MacKay

    No probs – it gets easier once your brain get’s into it :)


    although, for the life of me I cant work out why the following doesnt work

    essentially, what im trying to say is if the value is not one of these, home, solutions, about-us, contact then the value should be ‘navnewscurrent’ – i think the OR part of my code might be giving the problem, as the following words a treat?

    Rob MacKay

    Using OR statement there resets to a new instance of something for it to look at. So what you are saying is if get_curr_virtdir() == home or if solutions or if about-us or if partners .

    Instead you should be asking the comparison over and over.

    if get_curr_virtdir() == home or if get_curr_virtdir() == if solutions etc etc

    || get_curr_virtdir() == "solutions"
    || get_curr_virtdir() == "about-us"
    || get_curr_virtdir() == "partners"
    || get_curr_virtdir() == "contact-us" )


    echo "navnews";

    } else {

    echo "navnewscurrent";


    The reason it does that, although it seems long winded is what if you had another comparison?

    get_curr_virtdir() == “home” || $garry == “Your Dad” || ….

Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.