Skip to content

Instantly share code, notes, and snippets.

@allysonsouza
Created December 2, 2017 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allysonsouza/547a4c0e749d4835b5c0749182afeab2 to your computer and use it in GitHub Desktop.
Save allysonsouza/547a4c0e749d4835b5c0749182afeab2 to your computer and use it in GitHub Desktop.
Workshop: Desenvolvendo seu primeiro plugin
<?php
/*
Plugin Name: Banana Replace
Plugin URI: 2017.saopaulo.wordcamp.org
Description: Substitui a palavra banana por um gif
Version: 1.0
Author: Eu
Author URI: https://wordpress.org/
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: banana-replace
Domain Path: /languages
*/
add_filter( 'the_content', 'banana_replace' );
function banana_replace( $content ) {
$content = str_replace(
'banana',
'<iframe src="https://giphy.com/embed/bh4jzePjmd9iE" width="480" height="249" frameBorder="0" class="giphy-embed" allowFullScreen></iframe><p><a href="https://giphy.com/gifs/funny-minions-banana-bh4jzePjmd9iE">via GIPHY</a></p>',
$content
);
return $content;
}
body {
animation: flip 3s forwards;
}
@keyframes flip {
to {
transform: rotateZ(180deg);
}
}<?php
/*
Plugin Name: Related Posts
Plugin URI: 2017.saopaulo.wordcamp.org
Description: Exibe posts relacionados após conteúdo de um post.
Version: 1.0
Author: Eu
Author URI: https://wordpress.org/
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: related-posts
Domain Path: /languages
*/
add_filter( 'the_content', 'related_posts_display_posts' );
function related_posts_display_posts( $content ) {
return $content . related_posts_query();
}
/**
* Related Posts Query.
*
* @since 1.0
*
* @global array $post WP global post.
*
* @return string Related Posts.
*/
function related_posts_query() {
global $post; // Utiliza a variável global do post
$after_content = '';
$categories = get_the_category( $post->ID ); // Pega as categorias associadas ao post
if ( $categories ) { // Verifica se há alguma categoria associada ao post
foreach ( $categories as $category ) { // Percorre cada uma das categorias
$category_ids[] = $category->term_id; // Armazena o ID das categorias em um Array
}
// Construção do array de parâmetros da consulta, utilizando os id's armazenados
// Nestes parâmetros estamos definindo que queremos um post que esteja em uma das categorias de $category_ids
// um post que não seja este mesmo (post__not_in), 3 posts, do tipo de conteúdo post, ignorando os posts que
// foram marcados como sticky.
$query_args = array(
'category__in' => $category_ids,
'post__not_in' => array( $post->ID ),
'posts_per_page' => 3,
'post_type' => 'post',
'ignore_sticky_posts' => true
);
// Realiza a query utilizando os parâmetros anteriores
$related = new WP_Query( $query_args );
if ( $related->have_posts() ) { // Verifica se foi retornado algum post com os critérios que foram passados em $query_args
while ( $related->have_posts() ) {
$related->the_post();
$after_content .= '<h4><a href="' . get_the_permalink( get_the_ID() ) . '">' . get_the_title() . '</a></h4>';
}
}
wp_reset_postdata();
}
return $after_content;
}
<?php
/*
Plugin Name: Related Posts
Plugin URI: 2017.saopaulo.wordcamp.org
Description: Exibe posts relacionados após conteúdo de um post.
Version: 1.0
Author: Eu
Author URI: https://wordpress.org/
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: related-posts
Domain Path: /languages
*/
add_filter( 'the_content', 'related_posts_display_posts' );
function related_posts_display_posts( $content ) {
return $content . related_posts_query();
}
/**
* Related Posts Query.
*
* @since 1.0
*
* @global array $post WP global post.
*
* @return string Related Posts.
*/
function related_posts_query() {
global $post; // Utiliza a variável global do post
$after_content = '';
$categories = get_the_category( $post->ID ); // Pega as categorias associadas ao post
if ( $categories ) { // Verifica se há alguma categoria associada ao post
foreach ( $categories as $category ) { // Percorre cada uma das categorias
$category_ids[] = $category->term_id; // Armazena o ID das categorias em um Array
}
// Construção do array de parâmetros da consulta, utilizando os id's armazenados
// Nestes parâmetros estamos definindo que queremos um post que esteja em uma das categorias de $category_ids
// um post que não seja este mesmo (post__not_in), 3 posts, do tipo de conteúdo post, ignorando os posts que
// foram marcados como sticky.
$query_args = array(
'category__in' => $category_ids,
'post__not_in' => array( $post->ID ),
'posts_per_page' => 3,
'post_type' => 'post',
'ignore_sticky_posts' => true
);
// Realiza a query utilizando os parâmetros anteriores
$related = new WP_Query( $query_args );
if ( $related->have_posts() ) { // Verifica se foi retornado algum post com os critérios que foram passados em $query_args
while ( $related->have_posts() ) {
$related->the_post();
$after_content .= '<h4><a href="' . get_the_permalink( get_the_ID() ) . '">' . get_the_title() . '</a></h4>';
}
}
wp_reset_postdata();
}
return $after_content;
}
<?php
/*
Plugin Name: Upside Down
Plugin URI: 2017.saopaulo.wordcamp.org
Description: Um plugin que quando ativo coisas estranhas acontecem...
Version: 1.0
Author: Eu
Author URI: https://wordpress.org/
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: updsidedown
Domain Path: /languages
*/
// Prevents direct access
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'wp_enqueue_scripts', 'upsidedown_carrega_estilos', 10 );
function upsidedown_carrega_estilos() {
wp_enqueue_style( 'upsidedown', plugins_url( '/assets/css/upside-down.css', __FILE__ ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment