Category: Notes

  • Install WordPress at specific version with wp-cli-valet

    wp valet new sitename --version=5.8.2
    

  • Download WordPress VIP database to local install

    First, download what you need via VaultPress and then merge the files with the following command in the terminal. (It helps to name filename.sql to the database name you are going to import to).

    cd Downloads/unzipped-folder/sql
    cat *.sql > filename.sql

    If you don’t want to overwrite the current database, comment out the previous one and create a new one in wp-config.php:

    # define( 'DB_NAME', 'databasename' );
    # define( 'DB_NAME', 'databasename_preprod' );
    define( 'DB_NAME', 'databasename_2023-03-19' );

    Place the file you downloaded in the WordPress root folder (where wp-config.php is, i.e. ‘above’ the wp-content folder etc.).

    (If you are using Local for your local site environment, you may need to access the terminal via the menu option in there, rather than the regular Terminal app, for the following commands to work).

    If you want to overwrite an existing database you will need to drop it first. With wp-cli installed, run the following command from the WordPress root folder:

    wp db drop

    Then:

    wp db create
    wp db import
    wp search-replace livesite.com localsite.test

    Or for preprod sites:

    wp search-replace www-preprod-url-vip.net localsite.test


  • Website not loading after clone in WP Engine control panel

    If this happened to you like it did to me don’t assume – like I did – that WP Engine’s clone tool does a search and replace on your database to update the URL too.

    Instead, when I spoke to WP Engine support they suggested it had to be done as a separate step.

    Me: I thought that would happen as part of the clone process

    Me: So next time we do a clone we should do a manual search and replace in the database too?

    WP Engine Support: Correct.

    WP Engine Support: Or jump on chat and we can help.

    I still feel like this should happen automatically – maybe I was just unlucky on this occasion – but if it happens to you you can do a search and replace via the Advanced menu, if you have it available, in the WP Engine admin under the install you’re trying to fix.

    To do this, use the wp search-replace command in WP-CLI, e.g.

    wp search-replace urloforiginalsite.com newinstallurl.wpengine.com

    Otherwise just ask WP Engine support and they’ll do it for you.


  • Convert .pfx SSL certificate to .cer

    From the command line, replacing the filename as necessary (you will need the password for the certificate, if one is assigned):

    $ openssl pkcs12 -in wildcard.filename.com_2022.pfx -out wildcard.filename.com_2022.cer -nodes

    If the above does not generate a certificate for you along with the private key, try adding the -legacy flag, like this:

    $ openssl pkcs12 -in wildcard.filename.com_2022.pfx -out wildcard.filename.com_2022.cer -nodes -legacy

    I needed to do this to convert a .pfx SSL certificate for use with Pagely.

    EDIT: If you’re doing this for Pagely and the original certificate is a .pfx, after converting it you will likely need to open the .cer file in TextEdit and grab the two different parts.

    One starting with -----BEGIN PRIVATE KEY----- and one starting with -----BEGIN CERTIFICATE----- and paste them in the appropriate fields after you select Add Certificate > Import Existing Private Key & Certificate options in the Pagely dashboard.


  • PHP_CodeSniffer for WordPress VIP Go

    phpcs –standard=WordPress-VIP-Go -sp –basepath=. –ignore=vendor path/to/your/code

    Source: Install PHP_CodeSniffer for WordPress VIP · WordPress VIP Documentation


  • Change text in Excel to Sentence Case

    In cell B2, type =PROPER(A2), then press Enter.

    Source: Change the case of text – Office Support


  • Add async attribute to enqueued WordPress script

    <?php
    
    namespace NickDavis\AsyncExample;
    
    const ASYNC_EXAMPLE = 'nd-async-example';
    
    add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_script' );
    /**
     * Enqueues the async example script.
     *
     * @since v1.0.0
     */
    function enqueue_script() {
    	wp_enqueue_script( ASYNC_EXAMPLE, '//async.example.com', [], null, true );
    }
    
    add_filter( 'script_loader_tag', __NAMESPACE__ . '\add_async_attribute', 10, 2 );
    /**
     * Add an aysnc attribute to an enqueued script.
     *
     * @url https://wordpress.stackexchange.com/a/198372
     *
     * @since v1.0.0
     *
     * @param string $tag Tag for the enqueued script.
     * @param string $handle The script's registered handle.
     * @return string Script tag for the enqueued script
     */
    function add_async_attribute( $tag, $handle ) {
        // Just return the tag normally if this isn't one we want to async.
        if ( ASYNC_EXAMPLE !== $handle ) {
            return $tag;
        }
    
        return str_replace( ' src', ' async src', $tag );
    }