Skip to content

Instantly share code, notes, and snippets.

View corypina's full-sized avatar

Cory Piña corypina

View GitHub Profile
@corypina
corypina / auto-year-shortcode.php
Created January 6, 2020 23:59
Auto Year WordPress shortcode
<?php
// Generates the current year, for use in copyright notices
// Call with [auto-year]
// e.g., © [auto-year] Your Name
add_shortcode('auto-year', function(){
$year = date('Y');
return $year;
});
@corypina
corypina / show-post-id.php
Created September 9, 2019 20:51
Show the current post ID when in the WordPress admin edit screen
<?php
// Prominently display the post ID in as an admin notice
add_action( 'admin_notices', function(){
global $post;
if ( isset($_GET['post']) ) {
$class = 'notice notice-error';
$id = $post->ID;
$message = __( 'Post ID: ' . $id );
@corypina
corypina / get-post-id.php
Created August 21, 2019 20:26
Get the ID of a WordPress post regardless of whether it's passed as an object or an array
<?php
function getPostID( $post ) {
if ( is_object( $post ) ) :
$id = $post->ID;
elseif ( is_array( $post ) ) :
$id = $post['ID'];
endif;
return $id;
}
@corypina
corypina / block-login-registration-page.php
Created August 15, 2019 16:54
Prevent all users from accessing the native Login and Registration page.
<?php
// Prevent users from accessing the native Login and Registration page
// NOTE: This will prevent *everyone* from logging in through the standard channel.
// Even you.
add_action( 'login_init', function () {
if( ! is_user_logged_in() ) {
// redirects to homepage
@corypina
corypina / get-url-vars.js
Created July 31, 2019 00:14
Get URL variables with javascript
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
/* Short background behind text */
.element::after {
display: block;
content: "";
border-top: 18px solid #COLOR;
border-radius: 4px;
margin-top: -22px;
margin-left: -15px;
}
@corypina
corypina / pushover-push.php
Created June 3, 2019 16:56
Send notification through Pushover via PHP
<?php
curl_setopt_array($ch = curl_init(), array(
CURLOPT_URL => "https://api.pushover.net/1/messages.json",
CURLOPT_POSTFIELDS => array(
"token" => "TOKEN", // Insert App Token
"user" => "TOKEN", // Insert User token
"title" => "TITLE",
"message" => "MESSAGE",
),
CURLOPT_SAFE_UPLOAD => true,
@corypina
corypina / auto-add-woocommerce-coupon.php
Last active May 14, 2019 21:28
Programmatically add coupon code to WooCommerce checkout
<?php
// Automatically add a WooCommerce coupon code
// Currently added when headed to checkout, but can be connected to different hook.
add_action('woocommerce_before_checkout_form', function() {
global $woocommerce;
$coupon_code = 'COUPON_CODE'; // Replace with your code
// Add additional logic if necessary
@corypina
corypina / bb-draft-warning.php
Last active May 13, 2019 21:46
Display alert that current page has drafted changes
<?php
// Avoid losing drafted changes by alerting other users with editing roles
// that the current page has a draft.
// Place the code below in a plugin file or in functions.php
// NOTES:
// The alert will only display for users with `edit_posts` capability
// when the builder is open. Triggered by the 'fl_body_open' hook, which is part
@corypina
corypina / message.html
Last active April 3, 2019 21:36
Throw a message in a window via the URL parameter 'msg', e.g., `http://this-file.com?msg=Your+message`
<!DOCTYPE html>
<!--
Throw a message in a window via the URL parameter 'msg'
e.g. http://this-file.com?msg=Your+message
-->
<html>
<head>
<title>Your Message</title>