Skip to content

Instantly share code, notes, and snippets.

View lenivene's full-sized avatar
🖖
Full-stack dev

Lenivene Bezerra lenivene

🖖
Full-stack dev
View GitHub Profile
@lenivene
lenivene / functions.min.php
Last active March 11, 2016 18:11
Escaping variables
<?php
/**
* I used on a project.
* Author: Lenivene Bezerra
*/
function esc_var( $t ){
$e = 'UTF-8';
return mb_convert_encoding( htmlentities( $t, ENT_QUOTES, $e ), $e, $e );
}
@lenivene
lenivene / select.option.years.php
Created March 15, 2016 18:34
Generate listing of years
<select name="year">
<?php
$max_years = 100;
for ( $id = 1, $year = date('Y'); $year > ( date('Y') - $max_years ); $year-- ) {
if( date('Y') - 2 == $year ){
?>
<option selected>Selecione o Ano</option>
<?php } ?>
<option value="<?php echo $year; ?>" id="<?php echo $id; ?>"><?php echo $year; ?></option>
<?php $id++;} ?>
@lenivene
lenivene / functions.php
Last active April 15, 2016 15:04
Test if the current browser runs on a mobile device.
<?php
/**
* Test if the current browser runs on a mobile device.
*/
function is_mobile() {
$argent = $_SERVER['HTTP_USER_AGENT'] ? $_SERVER['HTTP_USER_AGENT'] : null;
$mobile = false;
if ( empty( $argent ) ) {
$is_mobile = $mobile;
} elseif ( strpos( $argent, 'Android' ) !== false
@lenivene
lenivene / functions.php
Last active August 17, 2016 02:40
Insert variable script ajax url
<?php
add_action( 'wp_head', 'wp_set_ajaxurl' );
function wp_set_ajaxurl() {
$html = '<script>';
$html .= 'var ajaxurl = "' . esc_url( self_admin_url( 'admin-ajax.php' ) ) . '";';
$html .= '</script>' . PHP_EOL;
echo $html;
}
@lenivene
lenivene / limit_number_posts_for_mobile.php
Last active May 13, 2016 09:54
Limite number of posts for mobile WordPress
<?php
add_action( 'pre_get_posts', 'change_limit_mobile' );
function change_limit_mobile( $query ){
$limit = 4;
if ( wp_is_mobile() && $query->is_main_query() ){
set_query_var( 'posts_per_page', $limit );
}
}
@lenivene
lenivene / .htaccess
Created May 17, 2016 03:01
Folder ajax xhr example.com/ajax
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ajax /admin/ajax.php [QSA,L]
</IfModule>
@lenivene
lenivene / functions.php
Last active May 25, 2016 19:37
Check if a "JSON" is a valid JSON
<?php
function wp_json_error( $json ){
$json_error = false;
$json_decode = json_decode( $json, true );
if ( json_last_error() === JSON_ERROR_NONE ) {
if ( !empty( $json ) ){
$json = $json_decode;
} else {
$json = $json;
@lenivene
lenivene / Coca-Cola.FM - Sinta o Sabor.asx
Created May 27, 2016 16:15
Play sound in WMP ( Windows Media Player )
<asx Version="3.0">
<entry>
<ref href="http://ic.imusicaradios.com.br:8000/brasil.stream.http" />
</entry>
<title>Coca-Cola.FM: Sinta o Sabor</title>
</asx>
@lenivene
lenivene / javascript.js
Created May 31, 2016 16:39
Defines that JavaScript code should be executed in "strict mode".
"use strict";
@lenivene
lenivene / functions.php
Last active September 3, 2021 12:52
Insert thumbnail (attachment) post by url
<?php
/**
* Insert thumbnail by URL
* Is very good to execute wp_insert_post
*
* @author Lenivene Bezerra
* @param string $url Image url external or no
* @param int $post_ID Post id to insert thumbnail
* @return ID Thumbnail | WP_Error
*/