Skip to content

Instantly share code, notes, and snippets.

View avillegasn's full-sized avatar

Antonio Villegas avillegasn

View GitHub Profile
<?php
function my_hourly_task() {
// Do something every hour. Anything.
}//end my_hourly_task()
add_action( 'my_hourly_event', 'my_hourly_task' );
if ( ! wp_next_scheduled ( 'my_hourly_event' ) ) {
wp_schedule_event( time(), 'hourly', 'my_hourly_event' );
@avillegasn
avillegasn / one-time-task.php
Last active April 3, 2017 13:01
Schedule a task that executes once in the future at the specified time
<?php
function my_task() {
// Do something. Anything.
}//end my_task()
add_action( 'my_action', 'my_task' );
// Execute my_task one hour from now...
wp_schedule_single_event( time() + 3600, 'my_action' );
@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;
<?php // no copies esta línea
add_action( 'transition_post_status', 'comprueba_publicacion', 10, 3 );
function comprueba_publicacion( $new_status, $old_status, $post ) {
if ( 'publish' === $new_status ) {
// Comprueba que existe una imagen destacada
if ( !tiene_imagen_destacada( $post ) ) {
wp_die( 'Has olvidado incluir una imagen destacada.' );
<?php
function wprin_buscador_shortcode( $atts, $content=null ) {
ob_start();
extract( shortcode_atts( array( 'nombre' => '' ), $atts ) );
$string = $atts['nombre'];
$args = array( 's' => $string );
$the_query = new WP_Query( $args );
@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 / 00-Extender la WP REST API.md
Last active February 18, 2016 20:39
Código para extender la WP REST API
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// contenido de la entrada E (template tags, html, etc)
?><h2>
<a href="<?php the_permalink(); ?>"><?php the_title();?></a>
</h2><?php
the_content();
// cogemos las etiquetas de la entrada E actual