This one is easy too…
add_action( 'template_redirect', function(){
if (is_search()) {
include("archive.php");
die();
}
});
It’s important to note this uses an anonymous function which requires PHP 5.3+.
This one is easy too…
add_action( 'template_redirect', function(){
if (is_search()) {
include("archive.php");
die();
}
});
It’s important to note this uses an anonymous function which requires PHP 5.3+.
Easy as pie!
Here’s an example to include all PHP files in a subdirectory named class.
N.B. Make sure you are one level below the class folder.
foreach (glob(plugin_dir_path(__FILE__) . "class/*.php") as $filename) include $filename;
I wanted to keep some WordPress plugins on my local installation of WordPress that did not end up on my live server: mostly debugging tools (for example: 1, 2 and 3). I could have essentially added them to git ignore, but that would get pushed upstream, so I essentially needed to locally exclude these files/directories. Luckily there’s a fairly painless way to do so on a local machine (this is written from the vantage of a Linux terminal):
# git ls-files --others --exclude-from=.git/info/exclude # Lines that start with '#' are comments. # For a project mostly in C, the following would be a good set of # exclude patterns (uncomment them if you want to use them): # *.[oa] # *~ plugins/debug-bar plugins/debug-objects plugins/debug-queries
I use MAMP on my local as part of my development environment. When dropping to terminal to perform an action (e.g. git commit/checkout/etc.) I was getting tired of manually changing my directory to the default MAMP root (cd /Applications/MAMP/htdocs/wp-content) so I figured that I’d add an alias using the following:
echo 'alias mamp="cd /Applications/MAMP/htdocs/wp-content"' >> ~/.bashrc && source ~/.bashrc
This seemed to work but much to my chagrin the next time I rebooted or opened a new terminal the alias appeared to be gone, bringing me back to step one. I finally figured out how to get this to work permanently …
nano ~/.bash_profile to edit your bash profilealias mamp="cd /Applications/MAMP/htdocs/wp-content" and close and savevoila!
This is something that is extremely easy once you realize that the hook body_class exists. It is important to pass an variable argument (it can be anything, just has to be there), the hook will pass the current array of classes to that argument. Simple usage of this hook is as follows:
function add_body_class ($classes) {
$classes[] = 'myclass';
return $classes;
}
add_filter('body_class', 'add_body_class');
What this does is add the class “myclass” to the <body> class.
Here’s an example of adding a specific class to the homepage, add this to your functions.php (for a theme) or your plugin functions page (for a plugin):
function add_home_class ($classes) {
if (is_home()) $classes[] = 'myclass';
return $classes;
}
add_filter('body_class', 'add_home_class');
This can be adapted in any way shape or form, just change the logic in the if statement that wraps around the filter.
Have fun!
So it’s been some time, but I finally came across an issue that I have faced, which hopefully means you have too. Either way this will serve as a great reference for myself, since I come across this on a semi-regular basis.
So say you are given data in the type of String, either from some sort of XML, JSON, or from user input that you need to convert to boolean. Using some quick logic you would think it was as easy as:
<?php $string="false"; $string_boolean=(bool)$string;
right? Let’s try this one out:
<?php
$string="false";
$string_boolean=(bool)$string;
// note: you could use (bool) or (boolean) they do the same thing.
if ($string_boolean) {
echo $string . " is true";
} else {
echo $string . " is false";
}
If we run this script it returns, almost comically:
false is true
So what can we do here? Give up and change our career? Maybe. But let’s first do 2 things and then rethink our career-change:
Let’s examine PHP.net’s documentation regarding casting booleans.
From PHP.net: Converting to boolean To explicitly convert a value to boolean, use the (bool) or (boolean) casts. However, in most cases the cast is unnecessary, since a value will be automatically converted if an operator, function or control structure requires a boolean argument. When converting to boolean, the following values are considered FALSE: - the boolean FALSE itself - the integer 0 (zero) - the float 0.0 (zero) - the empty string, and the string "0" - an array with zero elements - an object with zero member variables (PHP 4 only) - the special type NULL (including unset variables) - SimpleXML objects created from empty tags Every other value is considered TRUE (including any resource).
So, since both the string “true” and the string “false” do not meet any of the bullets, it will evaluate to true. OK, makes sense (sort of). So what are we to do now? Well we can create a function to mitigate our scenario. The function can be called string_boolean and what it can do is evaluate a string and if it’s value is “false” return false, otherwise return true. There is one gotchas that we need to mitigate, well it’s actually two: we need to treat “TRUE” , “TRue” ” TRUE”, ” True “, “ TRUE”, etc. all as true and likewise we need to treat “FALSE”, “FALSe”, “FALSE “, “False”, etc. all the same. We can make use of our two good function friends mb_strtoupper (technically we could also use strtoupper) and trim.
So, let’s talk our way through this function. It’s signature would be function string_boolean ($string) and it’s body would basically clean up $string and evaluate it against the actual word “true” – if it equals true return TRUE (the boolean) otherwise return FALSE. This puts the onus on false, think of this as analogous to the concept of innocent until proven guilty. The value is false until it is proven in a trial (i.e. the function we create) to be true. This is a safer way to handle this in my estimation. We could use an if … then scheme, but I’d rather use our good old friend the ternary for the sake of brevity.
Let’s construct this baby (note I am purposely making this longer than it needs to be, for education’s sake. Don’t believe me, I’ll write the function’s body in one line before I’m done, that’ll teach you to go against me (just kidding)!):
function string_boolean($string){
$string = trim($string); // let's make sure we get rid of any junk whitespace surrounding our input.
$string = mb_strtoupper($string); // let's convert our input to uppercase, which will make our comparison easier.
$true_string = mb_strtoupper("TRUE"); // just because I'm paranoid;
return $string === $true_string ? TRUE : FALSE; //if processed input equals "TRUE" return true otherwise return false.
}
One quick note: I use === instead of == to be strict in my comparison. Since PHP is a weak typed language the === is a way to enforce testing both the TYPE and VALUE of $string. That is, === checks if the value of $string is equal to “TRUE” AND that both $string and “TRUE” are the same type (string). Hope that makes some semblance of sense.
Here is the function in one line!
function string_boolean($string){
return ( mb_strtoupper( trim( $string)) === mb_strtoupper ("true")) ? TRUE : FALSE;
}
Enjoy!
So I just set up a fresh WordPress install and right away I realized that I need to remove that pesky toolbar at the top of the page, you know which one I mean, this bloody thing:
Every time I install a fresh WordPress installation I completely forget that I hate this darn thing. It’s like Groundhog Day, mostly because I tend to have the memory of a goldfish.* Every time I have to Google “hook to remove admin bar WordPress” and then go through all of the results which are mostly not what I am looking for, etc.
Solution? Well now that I have a personal blog, I should use it as a resource for myself, since nobody probably will read it any time soon. That being said, I drafted my own 2 line solution to add to my theme’s functions.php and know that I can just go to my blog next time I perform a fresh install of WordPress.
The code is as follows:
functionremove_the_darn_admin_bar(){returnfalse;} add_filter('show_admin_bar','remove_the_darn_admin_bar');
A quick explanation of the code, I created a function (remove_the_darn_admin_bar()) that returns false and hooked it into the filter “show_admin_bar.” In short, without getting into a detailed explanation about action vs. filter, that “passes data through” (hat tip to the WordPress codex). Essentially since I return “false” it puts in place of the show_admin_bar hook nothing, thus, no admin bar! Yay!