Skip to content

Instantly share code, notes, and snippets.

View someguy9's full-sized avatar
🏠
Working from home

Andy Feliciotti someguy9

🏠
Working from home
View GitHub Profile
@someguy9
someguy9 / wordpress-url-to-id.php
Created April 29, 2022 19:28
Get a post's ID based on URL in WordPress
<?php
echo 'Returning a specific post ID from a url: '.url_to_postid( 'https://smartwp.com/my-page-url' );
@someguy9
someguy9 / wordpress-get-id-based-on-title.php
Created April 29, 2022 19:18
Get a post ID based on its title in WordPress
<?php
$post_details = get_page_by_title( 'My Post Title', '', 'post' );
echo $post_details->ID;
$page_details = get_page_by_title( 'My Page Title', '', 'page' );
echo $page_details->ID;
@someguy9
someguy9 / wordpress-get-user-details-by-username.php
Created April 29, 2022 18:45
Get a user's details by their username in WordPress
<?php
$user_data = get_user_by('login', 'someguy');
echo 'The user ID is '.$user_data->ID;
@someguy9
someguy9 / wordpress-current-logged-in-user-information.php
Created April 29, 2022 18:43
Retrieve current logged in user's ID, Email, Name
<?php
$current_user = wp_get_current_user();
echo 'Username: '.$current_user->user_login;
echo 'User ID: '.$current_user->ID;
echo 'User Email: '.$current_user->user_email;
echo 'User First Name: '.$current_user->user_firstname;
echo 'User Last Name: '.$current_user->user_lastname;
echo 'User Display Name: '.$current_user->display_name;
<?php
@ini_set( 'upload_max_size' , '128M' );
@ini_set( 'post_max_size', '128M');
@ini_set( 'max_execution_time', '300' );
//URLBOX
function urlbox_url_generate($url, $options){
$options['url'] = $url;
$options['url'] = preg_replace("(^https?://)", "", $options['url'] );
$format = isset($options['format']) ? $options['format'] : 'png';
unset($options['format']);
$_parts = [];
foreach ($options as $key => $values) {
$values = is_array($values) ? $values : [$values];
foreach ($values as $value) {
@someguy9
someguy9 / user-last-login-wordpress.php
Last active March 14, 2022 15:15
Saves a WordPress user's last login and puts it in a sortable column in the WordPress dashboard
<?php
//Record user's last login to custom meta
add_action( 'wp_login', 'smartwp_capture_login_time', 10, 2 );
function smartwp_capture_login_time( $user_login, $user ) {
update_user_meta( $user->ID, 'last_login', time() );
}
//Register new custom column with last login time
add_filter( 'manage_users_columns', 'smartwp_user_last_login_column' );
<?php
function smartwp_defer_js_parsing( $url ){
if(is_admin()) return $url; //Skip admin JS files
if(is_user_logged_in()) return $url; //Skip if user is logged in
if(false === strpos($url, '.js')) return $url; //If it's not a JS file skip
if(strpos($url, 'jquery.js')) return $url; //Don't defer jQuery
return str_replace(' src', ' defer src', $url); //defer JS file
}
add_filter( 'script_loader_tag', 'smartwp_defer_js_parsing', 10 );
@someguy9
someguy9 / example-usage-wp_enqueue_script.php
Created February 19, 2021 19:50
Example usage of wp_enqueue_script
<?php
function wp_smartwp_scripts_method() {
wp_enqueue_script( 'my-custom-script', get_stylesheet_directory_uri() . '/js/your_custom_script.js', array( 'jquery' ), true );
}
add_action( 'wp_enqueue_scripts', 'wp_smartwp_scripts_method' );
@someguy9
someguy9 / disable-core-update-emails-wordpress.php
Created February 17, 2021 14:18
Disable automatic "Your Site Has Been Updated..." emails
<?php
//Disable automatic "Your Site Has Been Updated..." emails
add_filter( 'auto_core_update_send_email', 'smartwp_disable_core_update_emails', 10, 4 );
function smartwp_disable_core_update_emails( $send, $type, $core_update, $result ) {
if ( !empty($type) && $type == 'success' ) {
return false;
}
return true;
}