Skip to content

Instantly share code, notes, and snippets.

@felixarntz
felixarntz / wp-lazyload-lcp-fix.php
Last active March 11, 2024 22:13
Prototype plugin used for lazy-load LCP analysis in WordPress
<?php
/**
* Plugin initialization file
*
* @wordpress-plugin
* Plugin Name: Lazyload LCP Fix
* Plugin URI: https://gist.github.com/felixarntz
* Description: Omits the first content image from being lazy-loaded, to improve LCP performance.
* Version: 1.0.0
* Author: Felix Arntz, Google
@gavin-asay
gavin-asay / regex_html_tag.md
Last active April 14, 2024 16:38
Regex and You: Matching an HTML Tag

Regex and You: Matching an HTML Tag

Regular expressions, ever versatile, will help up locate HTML tags in a string today.

Summary

Pattern matching HTML strings serves at least one crucial function in web dev: sanitizing user input. Allowing user-submitted strings opens one's application to significant vulnerability. Supposing, for example, some ne'er-do-well on the internet submitted a comment that includes <script src="[path]/stealYourData.js"></script>. Regular expressions allow us to match HTML tags in a string, because HTML tags conform to a certain pattern:

  • begin and end with brackets (<>)
  • contain a string name consisting of one or more lowercase letters, like p, a, div, strong, script
@yousufansa
yousufansa / functions.php
Created April 20, 2020 08:18
MAS WPJMC - Companies Posted Jobs in Single Company
if( ! function_exists( 'mas_wpjmc_single_company_job_listings' ) ) {
function mas_wpjmc_single_company_job_listings() {
global $post;
$company_jobs = mas_wpjmc_get_the_company_job_listing();
if( count( $company_jobs ) ) :
?><div class="mas-company-jobs"><?php
?><h3 class="mas-company-jobs__title"><?php
echo apply_filters( 'mas_wpjmc_company_jobs_title', esc_html__( 'Jobs by This Company', 'mas-wp-job-manager-company' ) );
?></h3><?php
@hmowais
hmowais / functions.php
Created February 12, 2020 07:38
Speed Optimization WordPress (Manually)
<?php
//defer css
function add_rel_preload($html, $handle, $href, $media) {
if (is_admin())
return $html;
$html = <<<EOT
<link defer="defer" rel='stylesheet' href='$href' media="print" onload="this.media='all'" id='$handle' crossorigin="anonymous"/>
@franzose
franzose / ga_reporting.php
Last active August 15, 2021 18:11
Google Analytics Reporting API usage example
<?php
// composer require google/apiclient
require __DIR__ . '/vendor/autoload.php';
// This is the file obtained from Google during the service account creation process
// see these links:
// 1. https://console.developers.google.com/apis/credentials/serviceaccountkey
// 2. https://console.developers.google.com/iam-admin/serviceaccounts/create
@kartick14
kartick14 / force-download.html
Created March 7, 2019 11:33
Javascript force file download
<button class="js-download-link button" onclick="downloadFile('https://wattswork.s3.amazonaws.com/test-media/video/test_Drop-1280x720-16-9-HD.mp4','dwn.mp4'); return false;">Download MP4 File</button>
<button class="js-download-link button" onclick="downloadFile('https://wattswork.s3.amazonaws.com/test-media/PDFs/test-file.pdf','tfile.pdf'); return false;">Download PDF File</button>
<button class="js-download-link button" onclick="downloadFile('https://wattswork.s3.amazonaws.com/test-media/Text/test-file.txt','file.txt'); return false;">Download TXT File</button>
<script type="text/javascript">
function downloadFile(data, fileName, type="text/plain") {
// Create an invisible A element
const a = document.createElement("a");
@JosephHewitt
JosephHewitt / cloudflare_worker.js
Last active November 20, 2021 14:57
Cloudflare Worker JS code to cache Wordpress HTML for logged-out users.
/*I make no guarantees this code will work reliably. Use at your own risk. Please thoroughly test before deploying.
Enabling this on a Cloudflare Worker for a Wordpress site should cache all HTML pages (if your origin headers allow that) but bypass the cache when a user is logged in.
This is tested and working on a Wordpress 5.0.2 installation and the free Cloudflare tier (but I make no guarentees it will work for anyone else).
Sets the `x-cfw-cache` header to indicate the Worker cache status (either HIT, MISS, NO, or BYPASS): `NO` = server has set cookies (won't cache) and `BYPASS` = client has cookies (bypassing cache).
*/
addEventListener('fetch', event => {
event.respondWith(handleRequest(event))
@ko31
ko31 / SampleFilterAnalytics.php
Created December 19, 2018 16:17
Get reports with multiple condition filters for Google Analytics Reporting API v4
<?php
// composer require google/apiclient
/**
* @link https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-php
* @link https://developers.google.com/analytics/devguides/reporting/core/v4/samples
* @link https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet
*/
@DavidKuennen
DavidKuennen / minimal-analytics-snippet.js
Last active March 28, 2024 01:45
Minimal Analytics Snippet
(function (context, trackingId, options) {
const history = context.history;
const doc = document;
const nav = navigator || {};
const storage = localStorage;
const encode = encodeURIComponent;
const pushState = history.pushState;
const typeException = 'exception';
const generateId = () => Math.random().toString(36);
const getId = () => {
@justlstn
justlstn / add-files-to-custom-fields.php
Created August 24, 2018 10:18
Add multiple files to acf repeater field
$files = $_FILES[ 'attachments' ];
$attachments = [];
foreach ( $files['name'] as $key => $value ) {
if ( $files[ 'name' ][ $key ] ) {
$file = array(
'name' => $files[ 'name' ][ $key ],
'type' => $files[ 'type' ][ $key ],
'tmp_name' => $files[ 'tmp_name' ][ $key ],
'error' => $files[ 'error' ][ $key ],