Skip to content

Instantly share code, notes, and snippets.

@ricardobrg
ricardobrg / functions.php
Created May 4, 2016 01:09
Estados e cidades brasileiras populados em uma taxonomia do wordpress.
<?php
// É aconselhável criar um backup do banco antes
// crie a taxonomia cidade na functions do tema
add_action('init', 'register_locations');
function register_locations() {
register_taxonomy( 'cidade',array (
0 => 'locais',
),
array( 'hierarchical' => true,
#!/bin/bash
# Script to lauch WordPress Dev Site
NOME="$(date +%s%N | cut -b1-13)"
for i in "$@"
do
case $i in
-n=*|--nome=*)
NOME="${i#*=}"
shift #past argument=value
;;
@ricardobrg
ricardobrg / brg_login_redirect.php
Created May 30, 2016 11:52
Simple WordPress login redirect. Can be used as a mu-plugin or in functions.php
<?php
function brg_login_redirect( $redirect_to, $request, $user ) {
global $user;
//if there's a user with defined roles
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
//check if it's admin
if ( in_array( 'administrator', $user->roles ) ) {
//admins can go anywhere
return $redirect_to;
<?php
define ( 'BP_ENABLE_ROOT_PROFILES', true );
define( 'BP_DEFAULT_COMPONENT', 'settings' );
//dropzone
define( 'DROPZONEJS_PLUGIN_URL', get_stylesheet_directory_uri() );
define( 'DROPZONEJS_PLUGIN_VERSION', '0.0.1' );
add_action( 'init', 'dropzonejs_init' );
function dropzonejs_init() {
@ricardobrg
ricardobrg / brg_add_multiples_cats.php
Created June 6, 2016 02:30
How to set multiple taxonomies to a post in WordPress using an array or terms.
//With term IDs
$categories = array('1','2','3','4','5');
$cats_to_add = array_map ('intval', $categorias); //ALWAYS CHECK IF IDs ARE INTEGERS!!
//With term names
//$cats_to_add = ('categoria 1', 'categoria 2', 'categoria 3', 'Categoria 4','categoria Cinco');
if(wp_exist_post_by_title($string['title'])){
echo 'post exists';
} else {
$my_post = array(
@ricardobrg
ricardobrg / sql_import.php
Last active January 15, 2019 15:43 — forked from b4oshany/sql_import.php
WPDB SQL File import
<?php
/* NOTICE
* This script imports SQL commands "as is".
* Double check the SQL commands before using it
* and BACKUP YOUR DATABASE.
* It needs some improvement to use wpdb->prepare.
*/
class WPSQLImporter{
/**
* Loads an SQL stream into the WordPress database one command at a time.
@ricardobrg
ricardobrg / cotacao_dolar.php
Last active November 3, 2016 12:04
Atualiza a cotação do dólar com YQL e salva no wp_options
//cria o wp_cron para checar a cotação a cada hora
add_action('wp','brg_agenda_cotacao');
function brg_agenda_cotacao() {
if (! wp_next_scheduled ( 'brg_atualiza_cotacao' )) {
wp_schedule_event(time(), 'hourly', 'brg_atualiza_cotacao');
}
}
//função para atualizar a cotação no wp_options
add_action('brg_atualiza_cotacao', 'brg_cotacao_dolar');
@ricardobrg
ricardobrg / Utils.java
Last active May 7, 2017 13:47
Helper methods for Android Apps
package br.com.brgweb.utilsexample;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
@ricardobrg
ricardobrg / phoneValidator.java
Created May 10, 2017 16:36
Java Phone Validation BR
public boolean isTelefone(String numeroTelefone) {
return numeroTelefone.matches(".((10)|([1-9][1-9]).)\\s[9][6-9][0-9]{3}-[0-9]{4}") ||
numeroTelefone.matches(".((10)|([1-9][1-9]).)\\s[2-5][0-9]{3}-[0-9]{4}");
}
@ricardobrg
ricardobrg / block-admin.php
Created November 1, 2017 01:15
WP block wp-admin based in user role
<?php
add_action( 'init', 'blockusers_init' );
function blockusers_init() {
//check if user is administrator
if (! current_user_can( 'administrator' ) ){
//check if it's wp-admin and not ajax calls
if ( is_admin() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
//redirect to home
wp_redirect( home_url() );
//exit after redirect is required