Skip to content

Instantly share code, notes, and snippets.

View elvismdev's full-sized avatar
🏴

Elvis Morales elvismdev

🏴
View GitHub Profile
@elvismdev
elvismdev / wp-cfm-ssot.php
Created September 10, 2019 06:15
Example `wpcfm_is_ssot` filter to conditionally set WP-CFM bundles files as SSOT when running on Pantheon live environment.
<?php
/*
Plugin Name: WP-CFM SSOT
Description: Sets WP-CFM bundles as the Single Source of Truth.
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
@elvismdev
elvismdev / wp-cfm-ssot.php
Created September 9, 2019 19:44
Example usage filter `wpcfm_is_ssot`
<?php
/*
Plugin Name: WP-CFM SSOT
Description: Sets WP-CFM bundles as the Single Source of Truth for all tracked options.
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
@elvismdev
elvismdev / wpcfm_multi_env_disable.php
Last active September 9, 2019 15:13
Example usage filter `wpcfm_multi_env` to disable multi-environment feature in Pantheon
<?php
// wp-content/mu-plugins/wp-cfm-multi-env.php
// ...
// Return empty array to disable multi-environment feature.
function my_wpcfm_multi_env_disable( $environments ) {
return [];
}
add_filter( 'wpcfm_multi_env', 'my_wpcfm_multi_env_disable' );
@elvismdev
elvismdev / wpcfm_current_env.php
Last active September 9, 2019 14:15
Example usage filter `wpcfm_current_env`
<?php
// wp-content/mu-plugins/wp-cfm-multi-env.php
// ...
function my_wpcfm_current_env_set( $env ) {
// Detect with your own code logic the current environment the WordPress site is running.
// Generally this will be defined in a constant inside `$_ENV` or `$_SERVER` super-globals.
// ...
$env = 'dev';
return $env;
@elvismdev
elvismdev / wpcfm_multi_env.php
Created September 9, 2019 13:16
Example usage filter `wpcfm_multi_env`
<?php
// wp-content/mu-plugins/wp-cfm-multi-env.php
// ...
function my_wpcfm_multi_env_register( $environments ) {
// Define an array containing the hosting environment names.
// Or detect these with your own code logic if all are available in `$_ENV` or `$_SERVER` super-globals.
// ...
$environments = [
'dev',
@elvismdev
elvismdev / wp-cfm-multi-env.php
Created September 9, 2019 13:01
Enables configuration management for multiple environments with WP-CFM.
<?php
/*
Plugin Name: WP-CFM Multi-environment
Description: Enables configuration management for multiple environments with WP-CFM.
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
<?php
/*
Plugin Name: WP All Import - Redirection AddOn
Description: Add a redirect for each post imported.
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
@elvismdev
elvismdev / functions.php
Created January 28, 2019 12:09
Create valid date format from custom date string.
<?php
function correct_date_string( $str ) {
if ( $str ) {
$date = DateTime::createFromFormat('Y.m.d', $str);
return $date->format('m/d/Y');
} else {
return '';
}
}
@elvismdev
elvismdev / functions.php
Created January 28, 2019 11:05
Relates a post by title.
<?php
// Relates a post by title.
function rel_post_by_title( $title, $post_type ) {
// If we don't have a title to lookup, then return null;
if ( !$title ) return null;
// Check if related post already exists. If it doesn't then create it.
// Attempt to find page by value.
$page = get_page_by_title( $title, null, $post_type );
@elvismdev
elvismdev / script.js
Created August 2, 2018 23:00
Replaces a parameter value from a given URL query string.
function replaceUrlParam(url, paramName, paramValue) {
if (paramValue == null) {
paramValue = '';
}
var pattern = new RegExp('\\b('+paramName+'=).*?(&|#|$)');
if (url.search(pattern)>=0) {
return url.replace(pattern,'$1' + paramValue + '$2');
}
url = url.replace(/[?#]$/,'');