Skip to content

Instantly share code, notes, and snippets.

@rochow
rochow / snippet.php
Created August 1, 2022 01:53
Cleanup files in a directory based on time
<?php
$files = glob("*");
$time = time();
$ignore = array('cleanup.php','.ftpquota','index.html','robots.txt');
foreach($files as $file) {
if(is_file($file) && !in_array($file,$ignore)) {
if($time - filemtime($file) >= 60*60*24*30) { // 30 days
unlink($file);
}
@rochow
rochow / snippet.php
Created October 13, 2020 03:46
Remove variation data from WooCommerce Cart & Emails (used by YITH Product Addons and similar too) #woocommerce #yith-product-addons
<?php
// Remove bits of variation data from WooCommerce cart & emails we don't want to show
add_filter( 'woocommerce_get_item_data', 'mr_filter_variable_item_data', 10, 2 );
function mr_filter_variable_item_data( $item_data, $cart_item ) {
// Labels we want to remove
$items_to_remove = [ 'Base price' ];
// Remove any items where the label matches
foreach( $item_data as $key => $data ) {
@rochow
rochow / snippet.php
Last active October 12, 2020 21:54
Elementor Widget Content Override #elementor
<?php
class your_elementor_hooks {
use \Essential_Addons_Elementor\Traits\Helper;
function __construct() {
add_action( 'elementor/widget/render_content', array( $this, 'roxx_carousel_render' ), 10, 2 );
}
public function roxx_carousel_render( $content, $widget ) {
if ( 'eael-post-carousel' === $widget->get_name() ) {
$settings = $widget->get_settings();
@rochow
rochow / snippet.php
Created May 17, 2020 23:56
WooCommerce Optimisation - No Empty Searches
<?php
// Don't allow blank searches (too intensive)
add_action( 'init', function() {
if( isset( $_GET['s'] ) && '' == $_GET['s'] && !is_admin() ) {
wp_redirect( site_url() );
exit;
}
});
@rochow
rochow / snippet.php
Last active October 12, 2020 21:54
WordPress Login authentication requires same IP + Browser
<?php
add_action( 'wp_login', 'set_user_agent', 10, 2 );
add_action( 'init', 'check_user_agent', 1 );
function set_user_agent( $user_login, $user ) {
$user_agent = sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] );
$user_ip = sanitize_text_field( $_SERVER['REMOTE_ADDR'] );
update_user_meta( $user->ID, 'user_agent', $user_agent );
update_user_meta( $user->ID, 'user_ip', $user_ip );
@rochow
rochow / cleanup.php
Created June 24, 2018 01:37
Simple cleanup script to remove files in a directory older than X days
<?php
$files = glob("*");
$time = time();
$ignore = array('cleanup.php','.ftpquota','index.html','robots.txt');
foreach($files as $file) {
if(is_file($file) && !in_array($file,$ignore)) {
if($time - filemtime($file) >= 60*60*24*30) { // 30 days
unlink($file);
}
@rochow
rochow / snippet.php
Created June 18, 2018 02:48
Push a zip file to external server via FTP (handy for non-WP things, when on a dodgy connection etc)
<?php
$server = 'something.com';
$ftp_user_name = 'username';
$ftp_user_pass = 'password';
$filename = 'June2Site.zip';
$source = 'aaa/'.$filename;
$dest = '/new/'.$filename;
$mode = FTP_BINARY; // FTP_ASCII for text files (html, css, js etc)
$connection = ftp_ssl_connect ($server,24);
@rochow
rochow / snippet.php
Created March 27, 2018 00:29
WordPress - Remove admin email confirmation when updating
<?php
// Disable the confirmation notices when an administrator changes their email address.
// Per http://codex.wordpress.com/Function_Reference/update_option_new_admin_email
// Remove existing hooks
remove_action( 'add_option_new_admin_email', 'update_option_new_admin_email' );
remove_action( 'update_option_new_admin_email', 'update_option_new_admin_email' );
// Add our new hooks
add_action( 'add_option_new_admin_email', 'mr_update_option_new_admin_email', 10, 2 );
@rochow
rochow / snippet.php
Created March 19, 2018 23:31
WordPress - Set no index on a bulk list of URLs
<?php
function noindex_urls() {
global $wpdb;
$debug = true;
$urls = array(
'http://example.com/ok',
'http://example.com/blah/?g=ok',
'http://example.com/blah/foo/'
@rochow
rochow / snippet.php
Last active October 12, 2020 21:55
WordPress - Show Only Future Posts/Events
<?php
// Usage: Example below makes the CPT archive "Events" show all upcoming events only, and order by soonest->latest
// Make sure to change the 'key' to your meta_value that has the post ID (you can use future posts but that can cause other problems)
function show_upcoming_events( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_post_type_archive( 'events' )) {