By default Gravity forms isn’t accessible by editors. There are ways to do this with plugins like members. As some might guess I prefer wp-cli for this:
Running this will pretty much give all rights except the settings of Gravifty Forms. It will allow creatign, editing & deleting forms. But also view entries and export them.
<?php
namespace abc;
use function is_string;
use function get_class;
class main {
protected $string;
public function __construct( $string ) {
if ( is_string( $string )) {
$this->string = $string;
} else {
$this->string = get_class( $this );
}
}
}
Nothing fancy. Just a class in a namespace using two functions from the global namespace (is_string & get_class). Those two functions are imported from the global namespace as that will give a small performance boost.
But if you have 20-30 build in PHP functions that list will get very long….
Luckily you can merge them:
use function is_string, get_class;
For now I’m not sure I’ll always import build in PHP functions, the boost is small. And it’s annoying to keep track of.
For some reason files changed on a server. Site down, always fun. Restored a backup all good. This site did not have git on the server. But I still wanted to monitor the files for changes.
<?php
$query_name = new WP_Query();
if ( $query_name->have_posts() ) :
while ($query_name->have_posts()): $query_name->the_post();
the_date();
the_title();
the_content();
endwhile;
endif;
Nothing special right? Will by default just display the date, title and content of the first 10 posts. But if 2 posts are published on the same day it will skip display that posts date.
When looking at the source code of the_date it compares the date of the previous post with the current using is_new_day. I guess it can make sense in some scenario’s but too me it’s a bit weird by default.
Today we will handle a case of “Premature Optimization Is the Root of All Evil“. But this is my blog and I was working with a very big api set, and will only get bigger, so (premature) thinking about memory usage and execution time might be a good idea in the long run.
Before I started this I thought that a for loop was faster then a foreach loop. And I usually pick foreach because it’s easier to write and read.
A quick google lands on a stackoverflow question which concludes the opposite. So I started to test a bit.
There is a big difference in my use case here.
I need to remove array items that need to be excluded from the api results. Most examples you will find online are about editing items.
First tests where quite clear, using a foreach was in most configurations faster than for. The test array I create has 10000 items and every 3rth item should be excluded:
The same as before, but the $item is passed by reference. This is the big difference, since we are not editing the $item we want to remove it from the parent array
I ran each of these loops 5000 times and measured the total time that took. This was to insure the time between results was big enough to exclude the randomness (at least enough) The test code I ran
5.6122910976414sec Loop: foreach traditional
6.0467801094055sec Loop: foreach unset key
7.7878839969635sec Loop: foreach traditional pass by reference
7.0686309337616sec Loop: foreach unset key pass by reference
8.6388339996338sec Loop: for
The traditional foreach I’ve been using for years turned out to be the fastest. Research hours well spend ?
Bonus edit array item
As said before most examples use editing a array. So I also ran that scenario. Test code I upped the loops from 5000 runs to 7500 because the difference was so small. And still it’s close.
Anonymous functions have been around for a long time. And since WordPress now supports php 5.6 it can be safely used. And appears to be allowed?
Personally I’m not a fan of anonymous functions in combination with WordPress actions and filters. My main concern is you can’t remove them once registered. How ever today I found a use case which was very usefull in combination with the use
My example:
<?php
/* Template name: some-template */
// Gather all data
$condition_for_title = true;
$h1_title_override = 'very heavy and complicated check';
// the H1 also needed as the <title>
add_filter( 'pre_get_document_title', function( $title ) use ($h1_title_override, $condition_for_title) {
if ($condition_for_title) {
return $h1_title_override;
}
return $title;
}, 20, 1 );
get_header();
// start body
?>
<h1><?php echo $h1_title_override ?>
Here I pass 2 variables h1_title_override and $condition_for_title which are created outside the function. In my case these where quite complicated and heavy checks. Of course I could put those in a function and cache the result. And call that check in the filter function. But still I need to check the current template before doing the function.
More traditional Example:
in functions.php
<?php
function complicated_check() {
// Gather all data
$condition_for_title = true;
$h1_title_override = 'very heavy and complicated check';
return [
'condition_for_title' => $condition_for_title,
'h1_title_override' => $h1_title_override,
];
}
function title_exception_for_template( $title ) {
if ( ! is_page_template('clean-template.php')) {
return $title;
}
$template_data = complicated_check();
if ( $template_data['condition_for_title'] ) {
return $template_data['h1_title_override'];
}
return $title;
}
add_filter( 'pre_get_document_title', 'title_exception_for_template', 20, 1 );
Both these approaches do the same thing. But the more traditional way is a lot more code. Although it has cleaner template. I probably won’t use this much. If the anonymous function was more complicated it will get hard to read.
But for this case I think it was neat that I could use this little feature.
If you use WP-cli command on a multisite it be default will only run on the mainsite. But often you want to change a setting for all the sites. In my case I wanted to set the timezone to Amsterdam for the whole network. That’s not hard:
wp option set timezone_string 'Europe/Amsterdam'
On a multisite this is a bit more difficult. But the script below will do the same for each site in a multisite.
wp site list --field=url | xargs -I % sh -c 'printf "SITE: %\n"; wp option set timezone_string 'Europe/Amsterdam' --url=%'
It consists of 3 parts. First create a list of all site url’s
wp site list --field=url
Secondly we pass that on to xargs. xargs is a very powerfull tool. One that I hardly understand and should go into deeper one day. This is the best tutorial I found if you want to start with xargs.
The only thing important now is the -I %. This sets the variable to %. But the most important thing here is that inside the '***' You can run any command. like normal.
xargs -I % sh -c '***'
Which brings us to the final part. First print the site url on a line, then do the actual command we want to do on each sub site. As you can see we pass on the --url=% where we set the variable given in xargs.
printf "SITE: %\n"; wp option set timezone_string 'Europe/Amsterdam' --url=%
As of the release of VVV 3.0 this can all be done by adding the following in the vvv-custom.yml
general:
# Backup the databases to the database/backups subfolder on halt/suspend/destroy, set to false to disable
db_backup: false
# Import the databases if they're missing from backups
db_restore: false
Pre VVV 3.0
VVV is great but if you have 20+ sites in it most of which are quite big doing a reload or halt can be quite slow. Disabling it isn’t the easiest. In vagrant-root/config/homebin/ create these 3 files:
vagrant_destroy_custom
vagrant_halt_custom
vagrant_suspend_custom
I wish there was a way to easily disable these. The vvv-custom.yml would be great for this. Also adding a way to exclude specific databases.