WordPress: Fix WP-CLI error with sed or find commands

Fix WP-CLI errors with sed or find commands

Every so often while running wp search-replace or other WP-CLI commands, i receive this error:

Fatal error: require_once(): Failed opening required 'wp-mycreds.php' (include_path='.:') in phar:///usr/local/Cellar/wp-cli/1.5.0/libexec/wp-cli-1.5.0.phar/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(1169) : eval()'d code on line 3

Sidenote: the wp-mycreds.php is not a standard WP configuration file. I use it to store WP credentials instead of keeping them in the standard wp-config.php file, which is tracked with git. The wp-mycreds.php file i ignore via .gitignore. It’s a terrible practice to version control credentials.

Moving past that, i started googling for info regarding more important parts, such as "phar", "WP_CLI/Runner.php", and "eval()’d" which produced some results.

According to this Github issue, the problem was most likely due to some non-visible characters, specifically Unicode BOM, being saved at the beginning of the file(s).

To determine if that is the case, run this command

hexdump -C wp-config.php | head -1

and if it produces something like this

00000000 3c 3f 70 68 70 0a 0a 2f 2f 20 4d 61 63 61 72 69 |<?php..// Macari|

then it’ll need to be removed from the file(s) in question. A suggested command for this is

sed -i '1s/^\x3C\x3F\x70//' wp-config.php

But when doing that, it kept giving me an error

sed: -i may not be used with stdin

and so i was back to googling.

I found out that the sed command operates slightly differently on Linux than it does on macOS. Ahh, but of course.

One suggested solution was to install the GNU version of the sed command — npm install gnu-sed — but i eventually found a command-line solution once adapted to my situation that worked.

I ran it both for wp-config.php and wp-mycreds.php by simply changing the filename in the command.

find wp-config.php -name "*" -type f | xargs grep -l "xyz" |xargs sed -i "s/'${line}'/'${rep}'/g"

I was then able to run wp search-replace "/blog" "/blog" with no problems. Yay.

I hope that saved you some if you ran into a similar issue as me. As usual, let me know if anything didn’t work in the comments below.

Update! 2019 Aug 29 I found an even quicker, easier fix, and it’s using vim (my cli text editor of choice). Simply do:

vim wp-config.php
:set nobomb
:wq

The above commands open wp-config.php with vim, turn on the option for No Byte Order Mark, and then save wp-config.php and quit.

Cheers and peace.

(Visited 107 times, 1 visits today)

If you found this post useful ...

Buy Me a Coffee logo
Wondering why you keep seeing lower-cased 'i' in my posts? Read -> Why ‘i’ is not capitalized