Skip to content

Instantly share code, notes, and snippets.

@maugelves
maugelves / get-fisrt-paragraph.php
Last active October 28, 2016 19:15 — forked from banago/get-fisrt-paragraph.php
Get first paragraph from a WordPress post.
<?php
/**
* Get first paragraph from a WordPress post. Use inside the Loop.
*
* @param $class string optional class name to include in the paragraph
* @return string
*/
function get_first_paragraph( $class = '' ){
global $post;
@maugelves
maugelves / get-all-but-first-paragraph.php
Last active October 20, 2016 22:37
Get all but the first paragraph from a WordPress post. Use inside the Loop.
<?php
/**
* Get all but the first paragraph from a WordPress post. Use inside the Loop.
*
* @author Mauricio Gelves <yo@maugelves.com>
* @return string
*/
function get_all_but_first_paragraph( ) {
global $post;
@maugelves
maugelves / Smooth Scrolling with jQuery
Created October 24, 2016 21:34
smooth-scrolling.js
$(function() {
$('a[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
@maugelves
maugelves / get-last-children-taxonomies-for-post.php
Last active October 28, 2016 19:14
Get the latest child category/ies (those who aren't parent) for a specific post
<?php
/**
* Get the latest hierarchical taxonomy children of a post
*
* @author Mauricio Gelves <yo@maugelves.com>
* @param $post_id int Post ID
* @return array
*/
function get_last_children_taxonomy_in_post( $post_id, $taxonomy = 'category'){
@maugelves
maugelves / text-cropper.php
Last active November 2, 2016 10:02
Crop a text to a specific word count.
<?php
/**
* Crop a text to a specific word count
*
* @author Mauricio Gelves <yo@maugelves.com>
* @param $text_to_trim string
* @param $wordcount int
* @param $final string Which string do you want to add at the final of the trimmed text?
* @return string
*/
Estando aquí en Santander,
yo mi poema os relato,
Y quisiera en este rato,
expresar mi admiración,
por tan gran contribución,
de anónimos usuarios,
que sin lucro desarrollan,
WordPress y su base de datos.
La conforman doce tablas,
@maugelves
maugelves / url_origin.php
Created December 8, 2016 14:14
Función que devuelve la URL de una petición HTTP a partir de las variables de $_SERVER.
<?php
function url_origin( $s, $use_forwarded_host = false )
{
$ssl = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
$sp = strtolower( $s['SERVER_PROTOCOL'] );
$protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
$port = $s['SERVER_PORT'];
$port = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
$host = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
$host = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
@maugelves
maugelves / mg_acf_rimage.php
Last active August 5, 2017 19:47
Create an HTML responsive image tag from an ACF Image array.
<?php
/**
* ACF Responsive Image function
* Set your image field to return a WordPress array and call this function
* to generate a responsive <img> tag with srcset attributes
* You can also override the 'alt', 'class', 'size' and 'style' attributes.
*
*
* Example of use:
*
<?php
// 1. Configurar el PATH hacia la nueva carpeta de ACF
function configurar_path_a_acf( $path ) {
// actualizar el path
$path = get_stylesheet_directory() . '/acf/';
return $path;
<?php
// 1. Declaro el include a la librería TGM Plugin Activation
include_once __DIR__ . "/inc/lib/class-tgm-plugin-activation.php";
/**
* 2. En la acción 'tgmpa_register' declaro las dependencias del plugin
* siguiendo las pautas de la documentación oficial:
* http://tgmpluginactivation.com/configuration/
*/