Category: Notes

  • Cleaning up post content after Wix to WordPress import

    wp search-replace '>n<' '><'
    wp search-replace '>n  <' '>  <'

    To deal with situations like:


  • Increase PHP memory limit in Laravel Valet

    In /opt/homebrew/etc/php/8.0/php.ini – or whichever PHP version you’re running – find and increase memory_limit to 512M or 1024M or whatever you need to debug.

    (If the memory_limit line is commented out – i.e. has a prefix of ; – make sure to remove it).

    You may also need to add the following line to your wp-config.php (matching whatever value you set in php.ini):

    define( 'WP_MEMORY_LIMIT', '1024M' );

    Finally, you should probably run valet restart from the command line.


  • Fix 504 Gateway Timeout in Laravel Valet

    Add:

    proxy_connect_timeout 1200;
    proxy_read_timeout 1200;
    proxy_send_timeout 1200;
    fastcgi_read_timeout 1200;
    fastcgi_send_timeout 1200;

    In /opt/homebrew/etc/nginx/nginx.conf, right before:

    include "/Users/.../.config/valet/Nginx/*";
    include servers/*;
    include valet/valet.conf;

    All credit to / solution from:

    https://github.com/laravel/valet/issues/315#issuecomment-1362039656

    https://dev.to/aubreypwd/how-i-configured-niginx-valet-to-stop-the-504-gateway-time-out-during-long-xdebug-sessions-174i


  • Pull all remote git branches to local

    Used this when moving away from a git repository that it wasn’t possibly to transfer. Unfortunately the repository had a lot branches that I wanted to keep a copy of just in case.

    So I used the following bash script to pull them all down locally so I had a backup.

    #!/bin/bash
    
    # Clone the repository
    git clone [email protected]:username/repository.git
    
    # Navigate into the repository
    cd repository.git
    
    # Fetch all branches
    git fetch --all
    
    # For each remote branch
    for branch in `git branch -r | grep -v HEAD`;do
        # Get the name of the branch without the "origin/" prefix
        name=`echo $branch | cut -d/ -f2-`
    
        # Create and checkout local branch
        git checkout -b $name $branch
    done

    Just replace username and repository with the actual username and repository name.

    Save this script to a .sh file, give it execute permissions with chmod +x scriptname.sh, and then run it with ./scriptname.sh.


  • Use –precise if wp search-replace does not get all matches

    I just discovered that you can add the --precise flag when using wp search-replace with WP-CLI.

    I’ve never needed to use this before, so I wouldn’t suggest using it by default, but I had a site that was stubbornly refusing to replace all instances of a test domain after switching to the production domain and this solved it.


  • Solving MySQL error with Laravel Valet on macOS Ventura

    After updating to macOS Ventura, everything seemed to be fine with my Laravel Valet (v4.1.2) setup, but after restarting / doing some other thing which screwed everything, I couldn’t connect to the database properly.

    I tried everything.

    In the end how I fixed it was abandoning MariaDB (& removing all any any traces of it), dumping the my.cnf file in /opt/homebrew/etc and install MySQL v8.0.33 via homebrew.

    How much of that was really necessary, I’m not sure, but the key step was deleting the my.cnf file in /opt/homebrew/etc.

    Until then the brew install mysql would continually fail at the last step on the brew postinstall mysql step.

    (When I tried it ignore this step I would constantly bump into an ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) error).

    In case it was relevant, I also completely removed Laravel Valet – valet uninstall force – and only re-installed it after I was sure MySQL was OK.

    I also switched to using PHP v8.0 locally (as that’s what I’m running all my sites on right now anyway) before doing the valet install (including the initial composer install).

    EDIT #1: Here’s what the my.cnf file looked like once it was working again:

    # Default Homebrew MySQL server config
    [mysqld]
    # Only allow connections from localhost
    bind-address = 127.0.0.1
    mysqlx-bind-address = 127.0.0.1

    EDIT #2: Found similar solution here https://andy-carter.com/blog/resolving-post-install-issue-with-mariadb-install-via-homebrew


  • Resize Pagely images with URL parameters & PressThumb

    Pagely doesn’t resize images that are the wrong dimensions (height / width) on the fly.

    (Though, if PressThumb is enabled, it will convert them to ‘next gen’ formats and reduce the file size of the image automatically).

    For example, if you upload an image to the WordPress admin and then later on register a new image size it won’t try to crop the image on the fly to match the new image size on the front end if you then call that image size in get_the_post_thumbnail() or similar (WordPress VIP, will do this however).

    My advice there would be to resize the image (or images) via the WP-CLI wp media regenerate command or similar.

    However if you just want to resize a particular image for a particular image and PressThumb is enabled you can do this by modifying the URL.

    From Pagely support:

    You would want to update the image URL to include the desired dimensions, yes. You can do so by appending -widthxheight after the file name but before the file extension.

    As an example, if you wanted to change the dimensions of https://example.com/wp-content/uploads/2021/10/example.jpg to 123 pixels wide by 456 pixels high then the URL you’d need to use for the image would be https://example.com/wp-content/uploads/2021/10/example-123×456.jpg. You can even combine that with optimized webp images by appending .webp to the URL as normal. For something like that, the URL would be https://example.com/wp-content/uploads/2021/10/example-123×456.jpg.webp


  • Disable adding or updating plugins, themes or WordPress itself in the WordPress admin

    Add to wp-config.php:

    define( 'DISALLOW_FILE_MODS', true );

    Useful when managing plugin updates via Composer.


  • Reset WordPress permalinks via WP-CLI

    wp rewrite flush

    To save having you to do it via Settings > Permalinks…

    Source: https://developer.wordpress.org/cli/commands/rewrite/


  • Solve 404 error for .well-known/apple-app-site-association file on SpinupWP with Nginx

    I love SpinupWP, but I discovered that the Nginx config was giving 404 errors for the .well-known files used by an app that used the same domain.

    To solve it – and the .well-known/assetlinks.json file not returning either – I added the following lines to the /etc/nginx/sites-available/{DOMAIN}/{DOMAIN} file for domain on the SpinupWP powered server after the location ~ \.php$ { ... } section:

    location ~ /.well-known/apple-app-site-association {
    	default_type application/pkcs7-mime;
    }
    
    location = /.well-known/assetlinks.json {
    	alias /sites/{DOMAIN}/files/.well-known/assetlinks.json;
    }
    

    Thanks to the following suggestions:

    You will need to SSH into the SpinupWP server as a Sudo user to do this: https://spinupwp.com/doc/understanding-system-users/#sudo-users

    And you can find out more info on changing Nginx settings on SpinupWP here: https://spinupwp.com/doc/changing-nginx-settings/