• Resolved papaworx

    (@papaworx)


    In a formal sense, I didn’t hack. I created a child theme with child templates; I am a good boy. Never the less, Zeus smote me with a lightning destined for hackers. I wanted to spruce up pages for a book project with elaborate page navigation. It wasn’t difficult, and in a few hours it was done – beautiful, exactly how I had imagined it. Everything worked, except, the wp-admin-bar didn’t show anymore. Well, maybe there was an update?
    When I started to hunt down the problem, I realized that I had inadvertently called one of my loop variables by the same name used somewhere else in the model template. That couldn’t have happened in a strongly typed language, but that’s why we love php.
    Wouldn’t it be nice if there were a WP variable naming convention that clearly distinguishes add-on variables from legacy variables?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Another trap for the unwary is to leave a loop variable referencing a data structure, easier to show than describe:

      foreach($list as $x) {
         echo $x->field . '<br />';
         if( some reason )  break;
       }
    .....
       $x = 0;

    And you are left wondering how an entry in the $list array got damaged.
    The solution of course is to add:
    unset($x);
    after the loop. Do this always.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    Wouldn’t it be nice if there were a WP variable naming convention that clearly distinguishes add-on variables from legacy variables?

    There is, but you’re thinking of it backwards 🙂

    The convention is this: Name your variables uniquely.

    Like I use ipstenu_filter_name() all the time when making my own tweaks.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Lesson learned’ is closed to new replies.