Skip to content

Instantly share code, notes, and snippets.

View gzuzkstro's full-sized avatar

Jesús Castro gzuzkstro

View GitHub Profile
@gzuzkstro
gzuzkstro / shortcode-example.php
Last active August 27, 2023 04:51
#WordPress shortcode example that loops through a post type
<?php
function loop_shortcode($atts) {
//Query the posts, the parameters can be edited
$queried_posts = new WP_Query(array(
'post_type' => 'post-type',
'posts_per_page' => -1,
'order' => 'ASC'
));
@gzuzkstro
gzuzkstro / indexed-content.php
Last active March 13, 2019 15:51
#WordPress template part that extracts the heading anchors using #regex in order to build an index to navigate through the content
<?php
$content = get_the_content();
$matches = array();
preg_match_all('/\<(h[123456]).*?\sid+\=\"([0-9a-zA-Z\-]*?)\"\s?.*?\>(.+?)\<\/\1\>/', $content, $matches, PREG_SET_ORDER);
$links = array();
foreach($matches as $match){
$links[] = array(
'id' => $match[2],
'content' => $match[3]
@gzuzkstro
gzuzkstro / AnalyticsEventTracking.js
Last active March 15, 2020 22:48
Snippet for event tracking of Google Analytics given a CSS Selector
var buttons = document.querySelectorAll('.button-class');
if(buttons.length){
buttons.forEach(function(btn){
btn.addEventListener('click',function(){
//console.log('Event label:category was sent');
gtag('event', 'click', {'event_category': 'category', 'event_label': 'label'});
});
});
}