Use remote images and media on local development env

How many times you needed to copy remote website to test some changes?
And every time you needed to download wp-content/uploads to view images and media, right?
With this three lines you can “use” remote files in your local dev environment.
Just add to your local .htaccess (change paths and hosts, of course)

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/wp-content/uploads/[^\/]*/.*$
RewriteRule ^(.*)$ https://your-remote-website.com/$1 [QSA,L]

Fix jQuery and WordPress “url.indexOf is not a function” error

So, you have updated your WordPress to 5.6 and something strange happened to your website? You open the inspector (Command+Option+C (Mac) or Control+Shift+C (Windows, Linux, Chrome OS), and you find a red message like url.indexOf is not a function error ? Wtf Javascript? How to fix it?

WordPress 5.6 comes with an updated jQuery version: 3.5.1.

WordPress 5.5 stopped enabling the jQuery Migrate helper script by default, and with the upcoming release of WordPress 5.6, the bundled version of jQuery will be upgraded (from 1.12.4-wp to 3.5.1).

What this involves for users, is that any plugin or theme that previously relied on the jQuery Migrate tool to work as expected (due to being older code, or just a missed update to deprecated functions), may now have unexpected behaviors.

make.wordpress.org

.load, .unload, and .error, deprecated since jQuery 1.8, are no more. Use .on() to register listeners.

jQuery blog

So the correct way is to change in your javascript all your occurrences of

$(window).load(function() { ... });

into something like

$(window).on('load', function() { ... });

But sometimes errors are not related to your own code, right? So what to do?
Try adding this code before other javascript scripts and after jQuery inclusion.

jQuery.fn.load = function(callback){ jQuery(window).on("load", callback) };

CF7: send submitted data to another URL as JSON

Contact Form 7

If you need to send data from a Contact Form 7 (CF7) form to another URL you can use this simple PHP script. Just add it in your WordPress functions.php file inside your theme.

// define the URL where data should be submitted, this constant can be stored in your `wp-config.php` file
define('CF7_TO_API_URL', 'https://...........');
function action_wpcf7_mail_sent( $contact_form ) { 
    // you can limit this script to certains forms, just change the IDs (111,222,333) and uncomment line 10 and 47
    // if (in_array($WPCF7_ContactForm->id(), [111,222,333])) {
        $wpcf7      = WPCF7_ContactForm::get_current();
        $submission = WPCF7_Submission::get_instance();
        if ($submission) {
            $data = $submission->get_posted_data();
            $url = $submission->get_meta( 'url' ); // url where the submit come from
            $postId = url_to_postid($url); // from the url we get the post ID
            $postType = get_post_type($postId); // from post ID we get the post type
            $title = get_the_title($postId); // and the title
            $output = [
                "firstname" => $data['firstname'],
                "lastname" => $data['lastname'],
                "email" => $data['email'],
                "phone" => $data['phone'], // add all needed fields
                "source" => $url,
                "is_product" => $postType === 'product', // ex: we want to know if the page was a product (custom post)
                "product_name" => $postType === 'product' ? $title : '', // if it is a product tell me the product name (title)
                "want_newsletter" => $data['newsletter'] === 1  ||   $data['newsletter'] === "1" // ex. for checkbox    
            ];
            $options = array(
                'http' => array(
                    'method'  => 'POST', // you can change the method (GET, POST)
                    'content' => json_encode( $output ), // php array as json
                    'header'=>  "Content-Type: application/json\r\n" .
                        "Accept: application/json\r\n"
                )
            );
              
            $context  = stream_context_create( $options );
            $result = file_get_contents( CF7_TO_API_URL, false, $context );
            $response = json_decode( $result ); // you can use/output the response from 
        }
    // }
}; 
         
// add the action 
add_action( 'wpcf7_mail_sent', 'action_wpcf7_mail_sent', 10, 1 );