Impaled by a PHP Open Tag

I was recently moving a WordPress blog and ran into a problem where a series of errors were appearing on screen in the target location. Back on the original site, all was fine, so what was different? Naturally, I went through a whole host of checks, including:

  • PHP version
  • .htaccess file
  • Apache config files
  • The database
  • Copy of the theme
  • Duplicating ALL the site files
  • Version of WordPress used…etc.

You get the idea…but I kept drawing a blank. Changing the debug level in the wp-config file was helpful:

1
2
3
define('WP_DEBUG', true);

Notice: Undefined variable: cat_id in /var/www/blog-uat.site.org/wp-content/themes/site-theme/functions.php on line 1364

Let’s take a look at the code there:

1
2
3
4
5
6
1361 <?php
1362 $args = array(
1363 'numberposts' => 3,
1364 'category' => $cat_id
1365 );
1366 $cat\_posts = get\_posts ( $args );

I could now see where it was “beginning” to go wrong, but setting that value only took away the PHP error - the bad text remained. Then I looked up the source file and saw this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1338     </div>
1339
1340 <?
1341 }
1342 endif;
1343 if ( !function_exists('bm_get_category_id_by_slug')):
1344 function bm_get_category_id\_by_slug($slug){
1345 return get_category_by_slug($slug)->cat_ID;
1346 }
1347 endif;
1348
1349 if ( !function_exists('bm_show_category')):
1350 function bm_show_category($cat_id){
1351 $category = get_category( $cat_id );
1352 $category_link = get_category_link( $category->term_id );
1353 ?>
1354 <div class="full_line"></div>
1355 <div class="category_cards_container">
1356 <div class="category_title_box">
1357 <span class="category_title"><a class="" href="<?php echo $category_link; ?>"><?php$
1358 <span class="category_line"></span>
1359 </div>
1360 <div class="category_cards">
1361 <?php
1362 $args = array(
1363 'numberposts' => 3,
1364 'category' => $cat_id
1365 );
1366 $cat_posts = get_posts ( $args );

Look closely (and pretend you can’t remember the name of this post!). It’s the “<?” but what’s wrong with that? That’s a valid opening tag, isn’t it> Well, yes and no. It is valid, but only if you say it is in your php.ini file. For me, on my system with Apache, I need to head here:

1
/etc/php/5.6/apache2

In your case, should you suffer this predicament, you need to alter the folder for the version of PHP you are using. OK, back to the file. Edit that and search for this: “short_open_tag” and set this to “On” like so:

1
short_open_tag = On

Or should you!? Whilst that will fix your problem, that style is deprecated and can cause some problems with XML files.

Honestly, the best approach is to edit your file and make all opening tags begin with <?php. So that’s what I did :-)


Hi! Did you find this useful or interesting? I have an email list coming soon, but in the meantime, if you ready anything you fancy chatting about, I would love to hear from you. You can contact me here or at stephen ‘at’ logicalmoon.com