To create a custom template in WordPress for specific categories based on the slug is not a built-in function.  Not sure why since it makes sense to build this capability without creating a new filter.  This could be plug-in, but it’s so simple, it’s one of those things you wonder why it’s taking so long to be included in the core capabilities.  Who wants to install all those single-purpose plug-ins, anyway?

So if you have a category slug called “news” and you want all your news posts to use this new template, just add this filter to your child theme’s footer.php file.  In this example, you would need to create a template file called “single-news.php” and WordPress will automatically use this to override the theme’s template for that category.

//Gets WP category slug of post and looks for single-[cat slug].php template file:

add_filter('single_template', create_function(

      '$the_template',
      'foreach( (array) get_the_category() as $cat ) {
            if ( file_exists(STYLESHEETPATH . "/single-{$cat->slug}.php") )
                  return STYLESHEETPATH . "/single-{$cat->slug}.php"; }
           return $the_template;' )
);

NOTE: If you’re not using a child theme, read this first, then if you’re still not convinced, use:   TEMPLATEPATH  instead of STYLESHEETPATH for above.  But don’t ever do it that way, every time WordPress gets updated you’ll end up having to make these changes, unless you just like letting code get broken because you’re too insecure of a web developer to want to create new solutions so you’d prefer to just keep fixing things you could prevent from breaking…


Trying to putty into your server, you may get a “Connection Refused” error.  That’s usually because the Open SSH server and client aren’t installed, just run this at the command line and restart Apache.

sudo apt-get install openssh-server openssh-client

In Symfony, you don’t always have to actually pass values from your controllers to your Twig views.  Twig can actually pull global and server values directly through the super global “app” variable.  Most frequently, you can access these corresponding objects like Kernel, Request, Security and Session variables like so:

{{app.kernel.cachedir}}
{{app.kernel.logdir}}
{{app.kernel.rootdir}}
 
{{app.request.basepath}}
{{app.request.baseurl}}
{{app.request.uri}}

{{app.security.token.user}}

{{app.session.locale}}

Need more Twig tips?  View our Symfony Twig Cheatsheet!

Looking for a PHP developer, a Symfony expert or WordPress web developer?  Perhaps a business system architect that specializes in scalable MySQL business system design and deployment?  We have reliable, fast and professional developers that meets your organization’s needs!  Contact us for your next web development project and schedule a free consultation today.


Padlock - Security Image from Prolific Futility
“Rusty Padlock” artwork used with permission from ProlificFutility.com

You may need to password protect a file before sending it to someone.  Yes, there are Windows utilities, some free, that do this but I found this script works surprisingly well as long as “zip” is installed on your server. Just simply save the following file below to your website then upload the file you want to password protect. Be sure to rename “file.txt” in the below example to the name of the file you want to password protect and rename the “file.zip” to the name you want it output as then execute this script in the browser.  All you should have to do is download the ZIP archive version of this file.

<?php

$path=$_SERVER['DOCUMENT_ROOT'].'/pathToThisUtility/';
$path='';

echo system("zip -P pass ".$path."file.zip $path"."file.txt");

?>

When you download the file, you should be prompted with a password before opening.

– Aaron Belchamber


Many websites are still displaying HTML tables with those “<table><tr><td>” tags.  The problem is, try loading a table that looks nice on your desktop computer onto a mobile device and chances are, parts of your table will no longer be visible on screen, or the table will resize and squish the content.  Tables aren’t meant to be resized so drastically, so the best solution would use HTML 5 structure and CSS to help your data look like a table but have the flexibility of breaking out of the table structure in your CSS media queries easily.

Here is a sample of the structure to imitate HTML tables without using those antiquated table tags.

<section style="display: table;">

  <header style="display: table-row;">

    <div style="display: table-cell;"></div>

    <div style="display: table-cell;"></div>

    <div style="display: table-cell;"></div>

  </header>

  <div style="display: table-row;">

    <div style="display: table-cell;"></div>

    <div style="display: table-cell;"></div>

    <div style="display: table-cell;"></div>

  </div>

</section>