Skip to content

Instantly share code, notes, and snippets.

@rochow
rochow / snippet.php
Last active October 12, 2020 21:55
WordPress - Reset User Password from Code
<?php
// Usage: Put in functions.php. Load site once. Remove code. Now login to admin
// Change ID based on relevant user & put a more secure password!
wp_update_user(array(
'ID' => 1,
'user_pass' =>'admin'
));
@rochow
rochow / snippet.php
Last active October 12, 2020 21:56
WordPress - Redirect Archive to Most Recent Post
<?php
// In this example if trying to access /work/ it'll 302 redirect to the most recent post instead
add_action('template_redirect','redirect_to_latest');
function redirect_to_latest() {
if( is_post_type_archive('work') ) {
$latest = get_posts( 'post_type=work&posts_per_page=1' );
if($latest) {
wp_redirect( get_permalink( $latest[0]->ID ) );
exit;
}
@rochow
rochow / snippet.php
Last active October 12, 2020 21:57
WordPress - Remove Related Videos from Youtube Embeds
<?php
// Should almost be core, every client that embeds a YouTube video has always asked for them to be removed!
function remove_youtube_controls($code){
if(strpos($code, 'youtu.be') !== false || strpos($code, 'youtube.com') !== false){
$return = preg_replace("@src=(['\"])?([^'\">\s]*)@", "src=$1$2&showinfo=0&rel=0", $code);
return $return;
}
return $code;
}
@rochow
rochow / snippet.php
Last active October 12, 2020 21:57
WordPress - Common Page Heading
<?php
// Sometimes due to the design you get stuck putting the <h1> in header.php
// This is a basic function I expand to output the relevant heading
function page_heading() {
if( is_page() ) {
the_title();
} elseif( is_home() || is_single() || is_archive() ) {
echo 'News';
} elseif( is_search() ) {
@rochow
rochow / snippet.php
Last active October 12, 2020 21:57
Gravity Forms - Automatic User Login after Registration
<?php
add_action( 'gform_user_registered', 'login_new_user', 10, 4 );
function login_new_user( $user_id, $config, $entry, $user_pass ) {
wp_set_current_user( $user_id, $user_login );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $user_login );
//do_action( 'tml_new_user_registered', $user_id, $user_pass ); IF USING THEME MY LOGIN, ELSE DELETE
}
@rochow
rochow / snippet.php
Last active October 12, 2020 21:57
WordPress - Add New User via Code
<?php
// Usage: Put in functions.php. Load /wp-login.php once. Remove code. Can now login
// Disable the admin notifications
if ( ! function_exists( 'wp_new_user_notification' ) ) :
function wp_new_user_notification( $user_id, $plaintext_pass = '' ) {
return;
}
endif;
@rochow
rochow / snippet.php
Last active October 12, 2020 21:58
WooCommerce - Test Checkouts/Special Gateways for Specific Users Only
<?php
// Perfect for testing. Setup "Cheque" as an option, put your user ID in below, now only you can do full checkouts without needing CC or paypal
add_filter( 'woocommerce_available_payment_gateways', 'mr_filter_gateways' );
function mr_filter_gateways( $gateways ){
$current_user = wp_get_current_user();
if( 123 != $current_user->ID) {
unset( $args['cheque'] );
}
return $args;
}
@rochow
rochow / snippet.php
Last active October 12, 2020 22:00
WooCommerce - Gateways for Particular Users Only
<?php
// Assuming you already have the user meta setup
// In this example we unset Bank Transfer if the user doesn't have "pay via account" meta set to true
add_filter( 'woocommerce_available_payment_gateways', 'mr_filter_gateways');
function mr_filter_gateways( $available_gateways ){
$pay_via_account = ( is_user_logged_in() ) ? get_user_meta( get_current_user_id(), '_pay_via_account', true ) : false;
if( 1 != $pay_via_account ) {
unset( $available_gateways['bacs'] );
}
@rochow
rochow / snippet.php
Last active October 12, 2020 22:00
PHP - Get Youtube ID From URL
<?php
function get_youtube_id($url) {
preg_match('#(?<=(?:v|i)=)[a-zA-Z0-9-]+(?=&)|(?<=(?:v|i)\/)[^&\n]+|(?<=embed\/)[^"&\n]+|(?<=(?:v|i)=)[^&\n]+|(?<=youtu.be\/)[^&\n]+#',$url,$matches);
$id = $matches[0];
$id = str_replace('?rel=0','',$id);
if($id) return $id;
}
@rochow
rochow / snippet.php
Last active October 12, 2020 22:00
PHP - Sort Array Recursively
<?php
// Sort items of all internal arrays by key
function ksortRecursive($array) {
foreach ($array as $key => $nestedArray) {
if (is_array($nestedArray) && !empty($nestedArray)) {
$array[$key] = ksortRecursive($nestedArray);
}
}
ksort($array);