Skip to content

Instantly share code, notes, and snippets.

@wpjess
wpjess / functions.php
Created June 17, 2021 22:02
HTTP Auth for single WordPress page
function http_auth_headers(){
header('WWW-Authenticate: Basic realm="Web Page Restricted"');
header('HTTP/1.0 401 Unauthorized');
echo 'You must enter the correct credentials to access this page';
exit;
}
add_action('template_redirect', 'add_http_auth_basic', 0);
function add_http_auth_basic(){
$uri = 'contact';
if(!is_admin()){
@wpjess
wpjess / functions.php
Created December 10, 2020 05:40
Hide a WordPress site unless logged in
function hide_from_everyone() {
if( !is_admin() && !is_user_logged_in() ){
die();
}
}
add_action( 'template_redirect', 'hide_from_everyone' );
@wpjess
wpjess / utils.min.js
Created December 9, 2020 19:24
Fix Learnpress JS error with WordPress 5.6. File located at /wp-content/plugins/learnpress/assets/js/dist/utils.min.js
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
@wpjess
wpjess / password-generator.html
Created October 21, 2020 16:30
Password Generator
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<!-- created by Jess - jessnunez.dev -->
<meta charset="UTF-8" />
<title>Simple Password Generator</title>
<meta name="description" content="A quick and easy password generator. No muss, no fuss."/>
<meta name="viewport" content="width=device-width" />
<link rel="icon" href="icon.png" />
<link href="https://fonts.googleapis.com/css?family=Cutive+Mono&display=swap" rel="stylesheet">
@wpjess
wpjess / disable-site-health.php
Created October 20, 2020 16:58
Disable Site Health Notifications: PHP extentions and background updates
<?php
function prefix_remove_php_test( $tests ) {
unset( $tests['direct']['php_extensions'] );
unset( $tests['async']['background_updates'] );
return $tests;
}
add_filter( 'site_status_tests', 'prefix_remove_php_test' );
?>
@wpjess
wpjess / device-redirect.php
Created September 26, 2020 05:01
Redirect page based on device type
<?php
$iPod = stripos($_SERVER['HTTP_USER_AGENT'],"iPod");
$iPhone = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$iPad = stripos($_SERVER['HTTP_USER_AGENT'],"iPad");
$Android = stripos($_SERVER['HTTP_USER_AGENT'],"Android");
$webOS = stripos($_SERVER['HTTP_USER_AGENT'],"webOS");
if( $iPod || $iPhone || $iPad){
header( 'Location: https://facebook.com' );
die();
@wpjess
wpjess / functions.php
Created June 25, 2020 21:49
Apply query var to a targeted query automatically
// APPLY QUERY VAR TO SPECIFIC QUERY AUTOMATICALLY
function jess_apply_filter_automatically($query) {
if ( is_front_page() && in_array ( $query->get('post_type'), array('events') ) ) {
$query->set('jess_random_posts', 3);
$query->set('posts_per_page', -1);
}
}
add_action( 'pre_get_posts', 'jess_apply_filter_automatically' );
@wpjess
wpjess / functions.php
Created June 25, 2020 21:47
Create a filter to randomize posts in a query
// RANDOMIZE A SPECIFIC NUMBER OF POSTS
add_filter( 'the_posts', function( $posts, \WP_Query $query )
{
if( $random = $query->get( 'jess_random_posts' ) )
{
shuffle( $posts );
$posts = array_slice( $posts, 0, (int) $random );
}
return $posts;
}, 10, 2 );
@wpjess
wpjess / counts.php
Created June 12, 2020 22:26
Recalculate WordPress term counts
<?php
require 'wp-load.php';
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
@wpjess
wpjess / script.sh
Created June 10, 2020 21:50
Update WordPress post terms for group of IDs
#!/bin/bash
# Store the file post_ids as a variable
POST_IDS=$(<post_ids)
# Loop through each of these IDs
for POST_ID in $POST_IDS
do
wp-cli post term set "$POST_ID" category uncategorized --path=/htdocs/__wp__