Skip to content

Instantly share code, notes, and snippets.

View alandbh's full-sized avatar

Alan Vasconcelos alandbh

View GitHub Profile
@alandbh
alandbh / get_user_info.php
Created April 30, 2017 13:58
Get user logged info. // Pega informações do usuário logado
/* Get user info. */
global $current_user, $wp_roles;
//get_currentuserinfo(); //deprecated since 3.1
$post_id = $_GET['post_id'];
$user_id = get_current_user_id();
$current_user = wp_get_current_user();
$avatarArr = get_field('avatar', 'user_'. $user_id ); //Using ACF
@alandbh
alandbh / datetime-greetings.php
Created April 30, 2017 13:51
Print greetings to user, based in current date / time
/*
-------------------------------
Imprime uma mensagem de cumprimento ao usuário baseado na hora local
Alan
*/
date_default_timezone_set("America/Sao_Paulo"); // Seta o timezone para o nosso fuso-horário
$time = date("H"); // pega a hora atual no formato 24h
if ($time < "8") {
@alandbh
alandbh / Custom_Walker_Nav_Menu.php
Last active September 9, 2017 19:40
The way WordPress goes through the menu pages to display the items, is using a walker object. In this case the specific class for this object is called Walker_Nav_Menu. You can find it in wp-includes\nav-menu-template.php. The Walker_Nav_Menu is a pretty simple class. You are able to see, how the links and the menu structure are built there. The…
/*
In your functions.php create a class, to extend Walker_Nav_Menu with pretty similar methods to the parent class:
*/
class Custom_Walker_Nav_Menu extends Walker_Nav_Menu {
function start_el ( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
// Copy all the start_el code from source, and modify
}
function end_el( &$output, $item, $depth = 0, $args = array() ) {
@alandbh
alandbh / get-terms-inside-loop.php
Created April 21, 2017 00:51
This functions gets the terms of a taxonomy inside the loop and concatenates in a variable. // Pega os termos de uma texonomia dentro do loop e concatena em uma variável.
<?php
$id = get_the_ID();
$tipos = wp_get_post_terms( $id, 'tipo', '' );
//print_r($tipos);
$tipoSlug = '';
foreach ($tipos as $tipo) {
$tipoSlug .= $tipo->slug . ' '; //concatenate the terms with space between
}
@alandbh
alandbh / get_term_by_taxonomy.php
Created April 20, 2017 23:12
It returns an array of terms objects given a taxonomy; // Retorna um array de objetos de termos de uma taxonomia
<pre>
<?php
$terms = get_terms([
'taxonomy' => 'tipo',
'hide_empty' => false,
]);
print_r($terms);
?>
</pre>
@alandbh
alandbh / php-if-else-shorthand.php
Created April 20, 2017 14:28
Shorthand for IF / ELSE statement. (or ternary)
/* echo, inline */
echo 'Based on your score, you are a ',($score > 10 ? 'genius' : 'nobody'); //harsh!
/* shorthand usage */
$message = 'Hello '.($user->get('first_name') ?: 'Guest');
@alandbh
alandbh / get_post_ancestors.php
Created April 18, 2017 16:04
It gets the parents ID's // Pega os ID's das páginas mães
$ancestor = get_post_ancestors($post)[0];
if ($ancestor == 16349) { // se for filha de "os distritos"
echo '<h2>Localização</h2>';
include 'includes/mapa-local-atrativo.php';
}
@alandbh
alandbh / get-posts-by-term.php
Last active April 18, 2017 15:21
It get posts by taxomony terms. // Pega posts pela taxonomia
function feed_cidades($categoria) {
if ($categoria == '') {
$args = array (
'post_type' => 'cidades', //it can be other
'posts_per_page' => '-1'
//'post__not_in' => array($exclude)
);
@alandbh
alandbh / admin_style.php
Created December 7, 2016 13:00
Inject style tag in admin header in order to apply custom styles // Insere estilos no painel de administração com a tag style
/* --------------------------------
Insere estilos no painel de administração
Alan
------------------------------------ */
add_action('admin_head', 'custom_admin_styles');
@alandbh
alandbh / altera-galeria.php
Created August 5, 2016 14:26
Altera o HTML de saída das Galerias
/* ----------------
Altera o HTML de saída das galerias
Alan
----------------- */
add_filter('post_gallery', 'my_post_gallery', 10, 2);