Gravity Forms give editors access

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:

wp cap add editor gravityforms_create_form gravityforms_edit_forms gravityforms_view_entries gravityforms_export_entries gravityforms_delete_entries graviyforms_delete_forms gravityforms_edit_entries gravityforms_view_entry_notes gravityforms_edit_entry_notes --grant
Difference in rights after setting capabilities

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.

Full list of Gravity Forms Capabilities

WP-cli run command over each subsite

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=%

Running php from command line

This is one of those “I know this is possible, but don’t know how”.

php -r '$parts = [ "hello", "world" ]; echo implode( " ", $parts ) . PHP_EOL;' # hello world

Only useful for simple onelines. You’re very likely to be better of putting code in a php file and run that script like:

php ./helloworld.php

Run PHP with WordPress loaded.

Of course your good old friend wp-cli can help.
It can run code with wp fully loaded. So if you add things in the init action or even after the wp_loaded, those plugin/theme functions, posttypes and such are all available.

First off the plain php code execution, with wp eval

wp eval 'var_dump(did_action( "wp_loaded" ));'  # int(1)

Secondly we execute a file with WordPress fully loaded using wp eval-file

wp eval-file ./helloworld.php

wp cli output format with –porcelain

wp db export

#output
Succes: Exported to 'dbname-2019-09-16-2790c11.sql'

Often you need the filename. And if you only need the file name that’s annoying to parse.

--porcelain flag to the rescue!

wp db export --porcelain

#output
dbname-2019-09-16-2790c11.sql

This will work on a lot of commands that have one item output.
Stuff like wp post create ....
Here is a complete list of commands that have the –porcelain flag

Commands that output more items usually have a --format flag to handle output.