Skip to content

Instantly share code, notes, and snippets.

View rxnlabs's full-sized avatar

De'Yonte W. rxnlabs

View GitHub Profile
@rxnlabs
rxnlabs / php-find-replace-string-cli.php
Last active December 5, 2023 14:08
PHP - find and replace string from MySQL database. Look in all tables and replace text, even in serialized strings. Best to use from the PHP CLI (Command Line Interface) when working with large databases.
<?php
ini_set('memory_limit','3200M');
// This script is to solve the problem of doing database search and replace
// when developers have only gone and used the non-relational concept of
// serializing PHP arrays into single database columns. It will search for all
// matching data on the database and change it, even if it's within a serialized
// PHP array.
// The big problem with serialised arrays is that if you do a normal DB
// style search and replace the lengths get mucked up. This search deals with
@rxnlabs
rxnlabs / add-blog-to-wordpress-posts-only.md
Last active August 31, 2023 13:41
WordPress - WP: Add /blog to your Post post type only and remove it from your other custom post types and taxonomies after adding /blog

Add a /blog to your WordPress blog posts only and remove it from the other post types

This is useful if you want to know when a user is viewing a blog post in Google Analytics vs a page. Or if you want to just have an easy way to distinguish your blog content from your other content using the URL.

There may be an easier way to do this but all of my Googling didn't turn up any answers that let you modify the post URLs only.

  1. Add the code from remove-blog-front-from-custom-post-types.php to your theme's functions.php or to a custom plugin or some file where it will get loaded. This code prevents the new blog base from being applied to the URLs of Custom POst types and taxonomy archives.
  2. Log into your WordPress admin
  3. Go to Settings -> Permalinks
  4. Select the Custom Structure field and add /blog to the Permalink structure (add this before the /%postname% part of the URL
@rxnlabs
rxnlabs / redirect-apache-git-folder
Created July 25, 2023 16:15
Apache - .htaccess prevent Git repos .git/ folder from being viewable
# BEGIN Cornershop
# VCS lockdown
# Deny access to version control content
# Will not work if both Alias & Rewrite modules are disabled
# To verify with git content, check these two URLs:
# 1. /.git/HEAD
# 2. /.gitignore
<IfModule mod_alias.c>
# Deny access to VCS content using Alias Module
RedirectMatch 404 ^(.*/)?\.(git|svn|hg|bzr)+
@rxnlabs
rxnlabs / wp-upload-file-post-wp-http.php
Last active July 18, 2023 20:13
WordPress - Upload files to remote API using WP_Http / wp_remote_post + cURL + fsockopen + CurlFile
<?php
$url = 'https://insert-domain-here.com/api-endpoint';
// The file is stored on your system/host
$path_to_uploaded_file = '/full-path-to-file/picture.jpg';
$form_fields = [ 'first_name' => 'Foo', 'last_name' => 'Bar' ];
if ( file_exists( $path_to_uploaded_file ) ) {
$form_fields['profile_picture'] = new CurlFile( $path_to_uploaded_file );
}
/*
@rxnlabs
rxnlabs / gf-api-submit-form-bug-example.md
Last active July 14, 2023 09:23
Gravity Forms - GFAPI::submit_form bug when the $_POST variable contains existing data because of array_merge_recursive

Gravity Forms GFAPI::submit_form bug

There is a bug with the Gravity Forms GFAPI::submit_form method when the $_POST variable contains any existing values that match what you pass to GFAPI::submit_form.

Because GFAPI::submit_form internally uses array_merge_recursive, any existing values in the $_POST variable will be added to any values that exist in the second parameter that you pass to GFAPI::submit_form.

This bug appears in the latest version of Gravity Forms 2.4.20.5 and in the latest 2.5 beta.

UPDATE ON 07/14/2023: This is a known bug in Gravity Forms and the plugin developers (i.e. Rocket Genius) are aware of it. Since other developers have made custom workaround to get around this issue, patching this bug would break that custom code, so Rocket Genius has not patched it. This code is probably present in the latest version of the plugin and will probably be present in all future versions.

@rxnlabs
rxnlabs / wp-allow-external-ajax-cors.php
Last active June 27, 2023 20:02
WordPress - Allow CORS headers for external sites. Allow external site to make an AJAX request against the WP site if you don't have access to the .htaccess file or your site is hosted on an NGINX server
<?php
/**
* Add CORS HTTP headers to the page request to allow the MTS curriculm site to make an AJAX request against the site
*
* Modify the HTTP headers that WordPress outputs before they are sent so we can add CORS headers to the request for things like an AJAX request
*
* @param array $headers The HTTP headers that WordPress is about to send
* @param WP $wp The current WordPress environment instance
*
@rxnlabs
rxnlabs / php-wp-list-hooks.php
Last active June 15, 2023 17:07
WordPress - list all action hooks registered
<?php
// http://www.wprecipes.com/list-all-hooked-wordpress-functions
function list_hooked_functions($tag=false){
global $wp_filter;
if ($tag) {
$hook[$tag]=$wp_filter[$tag];
if (!is_array($hook[$tag])) {
trigger_error("Nothing found for '$tag' hook", E_USER_WARNING);
return;
}
@rxnlabs
rxnlabs / htaccess-redirect-load-images-from-production
Last active November 18, 2022 15:06
htaccess - Redirect image and file request from the localhost to the production host if the file does not exist on the localhost. Needs to be placed in the root htaccess if you're using WordPress or another system that redirects all request to a index.php file.
#place above all other rewrite rules if using a cms or program that redirects all request to index.php file (e.g. WordPress, Drupal, Laravel, etc...)
#if a file or directory does not exist on the local host, 302 redirect the request to the production host so the browser doesn't cache the file in case you replace the file locally
<IfModule mod_rewrite.c>
RewriteEngine On
#Redirect WordPress uploads
#Prevent infinite redirect. Only apply this if not on any subdomains of the live domain. Comment out line if not needed
RewriteCond %{HTTP_HOST} !^(.*)example\.com
#Prevent infinite redirect. Only apply this if not on the root domain of the live domain. Uncomment line if needed
#RewriteCond %{HTTP_HOST} !^example\.com [NC]
#Prevent infinite redirect. Only apply this if not on the www version of the exact domain of the live domain. Uncomment line if needed
@rxnlabs
rxnlabs / php-wp-enqueue-conditional-scriptload-before-dependent.php
Last active September 26, 2022 12:09
WordPress - enqueue javascript data based on WordPress conditions and other scripts. Load scripts tags before and after dependent script loads
<?php
function load_scripts(){
global $wp_scripts;
wp_register_script( 'theme-scripts', get_bloginfo('template_url').'/js/scripts.js', array('jquery'), '1.0', true );
//if we're on the woocommerce checkout page
if( is_checkout() ){
$wp_scripts->add_data('theme-scripts','data','<!--START SCRIPT STRING-->
// script to load before name-of-enqueued-script loads
@rxnlabs
rxnlabs / php-mysql-add-insert-multiple-rows-single-query.php
Last active September 19, 2022 18:49
PHP - Insert multiple rows into MySQL table using a single query statement. Example based on code from https://phpacademy.org/topics/how-to-insert-multiple-rows-in-mysql-in-one-query-using-pdo-from-a-form/34096
<?php
// https://phpacademy.org/topics/how-to-insert-multiple-rows-in-mysql-in-one-query-using-pdo-from-a-form/34096
function placeholder( $text, $count = 0, $separator = ',' ) {
$result = array();
if ($count > 0) {
for ($x = 0; $x < $count; $x++) {
$result[] = $text;
}
}