Skip to content

Instantly share code, notes, and snippets.

@yousufansa
yousufansa / functions.php
Created Apr 20, 2020
MAS WPJMC - Companies Posted Jobs in Single Company
View functions.php
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
@franzose
franzose / ga_reporting.php
Last active Aug 15, 2021
Google Analytics Reporting API usage example
View ga_reporting.php
<?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
@JosephHewitt
JosephHewitt / cloudflare_worker.js
Last active Nov 20, 2021
Cloudflare Worker JS code to cache Wordpress HTML for logged-out users.
View cloudflare_worker.js
/*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 Dec 19, 2018
Get reports with multiple condition filters for Google Analytics Reporting API v4
View SampleFilterAnalytics.php
<?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
*/
View minimal-analytics-snippet.js
(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 Aug 24, 2018
Add multiple files to acf repeater field
View add-files-to-custom-fields.php
$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 ],
@cp6
cp6 / bunnycdn_storage.php
Last active Jan 3, 2021
Bunny CDN Storage API format
View bunnycdn_storage.php
<?php
header("Content-type:application/json");
function storage($key, $dir = '')
{
return json_decode(file_get_contents("https://storage.bunnycdn.com/" . $dir . "/?AccessKey=" . $key . ""), true);
}
$array = storage('STORAGE-PASSWORD-HERE', 'STORAGENAME');
$items = array('data' => array());
foreach ($array as $value) {
@jay7793
jay7793 / Instruction.sh
Last active May 5, 2021
Install Laravel 5.6 With PHP7.2 NGINX and PHP7.2-FPM on Ubuntu 16.04
View Instruction.sh
# Install software-properties-common package to give us add-apt-repository package
sudo apt-get install -y software-properties-common
# Install latest nginx version from community maintained ppa
sudo add-apt-repository ppa:nginx/stable
# Update packages after adding ppa
sudo apt-get update
# Install nginx
@AnthumChris
AnthumChris / README.md
Last active Jun 27, 2022
Clear Nginx Cache
View README.md

Clearing Nginx's HTTP Cache

I recently implemented Nginx HTTP content caching on our WordPress web servers to improve page load speeds and eliminate redundant, unneeded server-side page rendering. Caching the pages was relatively straightforward, but clearing the cache required a custom workaround.

Nginx comes in two versions: free and “Nginx Plus” at $2,500/year. The free version of Nginx does not offer the needed cache-clearing features of Nginx Plus, and I wasn’t comfortable paying $20,000 for 8 instances without trying to build my own solution.

Our Nginx servers run as an HTTP proxy for multiple PHP/MySQL-backed WordPress sites. The goal was to cache the dynamic PHP HTML responses in Nginx and serve the HTML pages from Nginx to avoid redundant, CPU-intensive PHP renders.

Site Cache Configuration

The example below shows how PHP response caching is configured for a site (other nginx configuration details are excluded for brevity). A cache named cachedemo-prod is defined to store cached HTML f

@isuke01
isuke01 / custom-wp.ajax
Last active Nov 18, 2019
Custom WP ajax, capable of using SHORTINIT, and much faster than standard WP
View custom-wp.ajax
<?php
/*
If you use short init by passing argument shortinit.
You have base and a bit more wp components loaded, like get_meta, current user .. etc.
*/
if (!isset( $_POST['action']))
die('-1');
if ( isset( $_POST['shortinit']) ){
$type = trim($_POST['shortinit']);