Skip to content

Instantly share code, notes, and snippets.

View alandbh's full-sized avatar

Alan Vasconcelos alandbh

View GitHub Profile
@alandbh
alandbh / shortcode_youtube.php
Last active September 17, 2017 03:31
shortcode for embeding youtube videos
/*
---------- Shortcode for Youtube videos
Alan
*/
class short_youtube {
function shortcode($atts, $content = null)
{
extract(shortcode_atts(array(
'alinhamento' => '',
@alandbh
alandbh / activates-menu-item.php
Last active September 17, 2017 03:33
This code sets de current menu item for single CPT
/* ----------------
It applies the class "active" or "current-menu-item" on menu item of a parent page
Alan
----------------- */
add_action('nav_menu_css_class', 'add_current_nav_class', 10, 2 );
function add_current_nav_class($classes, $item) {
@alandbh
alandbh / get_users_loop.php
Created April 30, 2017 14:13
A complete query go get all users in Wordpress loop with custom fields and date/time.// Uma query completa para pegar todos os usuários cadastrados e montar um loop com data e hora.
<?php
$args = array(
'blog_id' => $GLOBALS['blog_id'],
'role' => 'author',
'role__in' => array(),
'role__not_in' => array(),
'meta_key' => '',
'meta_value' => '',
'meta_compare' => '',
'meta_query' => array(),
@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';
}