Skip to content

Instantly share code, notes, and snippets.

View avillegasn's full-sized avatar

Antonio Villegas avillegasn

View GitHub Profile
@avillegasn
avillegasn / custom-admin-logo.php
Last active February 19, 2019 21:21
How to add a custom logo in WordPress login page
function my_login_logo() { ?>
<style type="text/css">
#login h1 a, .login h1 a {
background-image: url(<?php echo get_stylesheet_directory_uri(); ?>/img/nelio-icon.png);
height: 84px;
width: 84px;
background-size: cover;
background-repeat: no-repeat;
}
</style>
@avillegasn
avillegasn / only-for-admins.php
Last active February 27, 2019 14:01
Code snippets to hide/manage routes in WordPress REST API
<?php
add_filter( 'rest_authentication_errors', function( $result ) {
if ( ! empty( $result ) ) {
return $result;
}
if ( ! is_user_logged_in() ) {
return new WP_Error( 'rest_not_logged_in', 'You are not currently logged in.', array( 'status' => 401 ) );
}
if ( ! current_user_can( 'administrator' ) ) {
@avillegasn
avillegasn / package.json
Created June 3, 2019 08:36
Content of package.json file to use @wordpress/scripts package
{
"name": "my-wp-plugin",
"version": "1.0.0",
"description": "My WP plugin description",
"keywords": [
"my",
"wordpress",
"plugin"
],
"author": "avillegasn",
@avillegasn
avillegasn / my-component.js
Last active June 28, 2019 07:55
Extending @wordpress/scripts to load SVG files inside JSX files.
import {
Component,
Fragment,
} from '@wordpress/element';
import Icon from './icon.svg';
class MyComponent extends Component {
render() {
return (
@avillegasn
avillegasn / detect-unused-images.sql
Last active September 23, 2019 12:58
Detect and remove unused images in WordPress
SELECT
*
FROM
wp_posts i
WHERE
i.post_type = 'attachment'
AND
NOT EXISTS (SELECT * FROM wp_posts p WHERE p.ID = i.post_parent)
AND
NOT EXISTS (SELECT * FROM wp_postmeta pm WHERE pm.meta_key = '_thumbnail_id' AND pm.meta_value = i.ID)
@avillegasn
avillegasn / gutenberg.js
Last active October 9, 2019 07:36
Fragments of code to add a custom button to Gutenberg rich text blocks
import ElementIcon from '../images/logo.svg';
const { Fragment } = wp.element;
const { __ } = window.wp.i18n;
const { registerFormatType, unregisterFormatType } = window.wp.richText;
const { RichTextToolbarButton } = window.wp.blockEditor;
unregisterFormatType( 'nelio/button' );
registerFormatType( 'nelio/button', {
@avillegasn
avillegasn / function-documentation.php
Last active December 2, 2019 11:31
Creating a function
<?php
/**
* Sends and email when a post is published.
*
* Sends and email using wp_mail standard WordPress function including data about
* the post being published.
*/
@avillegasn
avillegasn / estimar-tiempo-lectura-entrada-wordpress.php
Last active October 13, 2021 22:54
Código para calcular la estimación de tiempo necesario para leer una entrada en WordPress
<?php
/**
* Estima el tiempo necesario para leer una entrada en WordPress
*
* @return string
*/
function wpr_estima_tiempo_lectura() {
$entrada = get_post();
@avillegasn
avillegasn / wordpress-additional-files.php
Created September 29, 2016 07:18
How to allow uploading additional file extensions in WordPress
<?php
add_filter( 'upload_mimes', 'my_myme_types', 1, 1 );
function my_myme_types( $mime_types ) {
$mime_types['svg'] = 'image/svg+xml'; // Adding .svg extension
$mime_types['json'] = 'application/json'; // Adding .json extension
unset( $mime_types['xls'] ); // Remove .xls extension
unset( $mime_types['xlsx'] ); // Remove .xlsx extension
return $mime_types;
@avillegasn
avillegasn / settings-link.php
Last active August 22, 2022 08:55
Add a settings link under your plugin title in the list of plugins