Skip to content

Instantly share code, notes, and snippets.

View odil-io's full-sized avatar
🪐

Odilio Witteveen odil-io

🪐
View GitHub Profile
@odil-io
odil-io / functions.php
Last active August 11, 2020 07:56
WordPress: display a notice while on localhost
<?php
function localhost_admin_notice(){
$whitelist = array('127.0.0.1', "::1");
if( in_array( $_SERVER['REMOTE_ADDR'], $whitelist )):
echo '<div class="notice notice-warning"><p>Let op: dit is een local site: '.$_SERVER['REMOTE_ADDR'].'</p></div>';
@odil-io
odil-io / functions.php
Created August 5, 2020 08:16
WordPress: generate a Database type password hash. Usefull for when you locked yourself out but still have SFTP and Database access
<?php
$password = 'password';
$hash_password = wp_hash_password($password);
echo $hash_password;
@odil-io
odil-io / javascript.js
Created July 16, 2020 07:58
JavaScript: count from Int to Int in X amount of time
// This function counts the integers/strings only containing numbers to the target integer
function animateValue(className, start, end, duration) {
// Get target element
var obj = document.getElementById(className);
// Set range
var range = end - start;
// Set minimum timer. Shorter than 50ms is not really visible any way
@odil-io
odil-io / upload-a-file.MD
Created January 2, 2020 11:06 — forked from ahmadawais/upload-a-file.MD
Upload a file using the WordPress REST API

Upload files

Using the REST API to upload a file to WordPress is quite simple. All you need is to send the file in a POST-Request to the wp/v2/media route.

There are two ways of sending a file. The first method simply sends the file in the body of the request. The following PHP script shows the basic principle:

@odil-io
odil-io / functions.php
Last active August 2, 2022 07:17
WordPress: Disable Emoji scripts
<?php
// Disable Emoji stylesheets and scripts
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
?>
@odil-io
odil-io / functions.php
Created November 8, 2019 09:51
Meta Tag for Google site verification in WordPress
add_action( 'wp_head', function() {
echo '<meta name="google-site-verification" content="INSERT KEY HERE" />';
});
@odil-io
odil-io / functions.php
Created October 14, 2019 09:25
Very simple maintenance mode for WordPress
<?php
function maintenance_mode() {
if ( !current_user_can( 'administrator' ) ) {
wp_die('<div style="text-align:center;"><img src="'.get_stylesheet_directory_uri().'/logo.png" width="180"/><p>Binnenkort is deze website online.</p></div>');
}
}
add_action('get_header', 'maintenance_mode');
@odil-io
odil-io / functions.php
Created October 14, 2019 09:24
Disable comments on WordPress
<?php
add_action('admin_init', function () {
// Redirect any user trying to access comments page
global $pagenow;
if ($pagenow === 'edit-comments.php') {
wp_redirect(admin_url());
exit;
}
@odil-io
odil-io / functions.php
Last active November 14, 2019 09:31
WordPress: Easily 'White Label' /wp-admin/
<?php
add_action('login_head', function() {
$custom_logo_id = get_theme_mod( 'custom_logo' );
$logo = wp_get_attachment_image_src( $custom_logo_id , 'full' );
if ( has_custom_logo() ) {
echo '<style type="text/css">
.login h1 a {
background-image:url(' . esc_url( $logo[0] ) . ') !important;
background-size:contain;
@odil-io
odil-io / functions.php
Last active January 15, 2019 08:32
WordPress: List taxonomies and seperator
<?php
// Get the tags.
$tags = get_the_tags();
// Count the total of tags.
$total_tags = count($tags);
// Set integer to 0.
// This is used to track at which item the loop is.
$i=0;