Skip to content

Instantly share code, notes, and snippets.

View tyxla's full-sized avatar
🏔️
Away this week

Marin Atanasov tyxla

🏔️
Away this week
View GitHub Profile
@tyxla
tyxla / gist:a1539561803de4d03c11
Last active August 29, 2015 14:08
Get a page by its template, and optionally by additional criteria
<?php
# Get a page by its template, and optionally by additional criteria
function crb_get_page_by_template($template, $additional_meta = array()) {
// the query for the page template
$meta_query = array(
array(
'key' => '_wp_page_template',
'value' => $template,
@tyxla
tyxla / functions.php
Created February 3, 2015 10:17
get_posts() - recommended usage
<?php
# Recommended usage for get_posts() and other functions with similar querystring parameters
$args = array(
'post_type' => 'attachment',
'posts_per_page' => 5,
'post_status' => 'inherit',
'suppress_filters' => false,
);
$attachments = get_posts($args);
?>
@tyxla
tyxla / functions.php
Created February 3, 2015 10:20
get_posts() - DEPRECATED usage
<?php
# DEPRECATED usage for get_posts() and other functions with similar querystring parameters
$args = 'post_type=attachment&posts_per_page=5&post_status=inherit&suppress_filters=false';
$attachments = get_posts($args);
?>
@tyxla
tyxla / gist:d289a3f1a48c7e58d34c
Created June 8, 2015 08:20
Function that returns the largest array of consecutive numbers. Uses numerical keys.
<?php
function get_largest_consecutive_number_array($array) {
$result = array();
$total_elements = count($array);
foreach ($array as $key => $element) {
$temp = array($element);
$next_key = $key + 1;
while($next_key < $total_elements) {
if ($array[$next_key] === $array[$next_key - 1] + 1) {
@tyxla
tyxla / functions.php
Created August 14, 2015 11:28
Very ugly fix for Carbon_Field_Association together with WPML on the Widgets page in the administration.
<?php
# Resolving WPML and Carbon_Field_Association incompatibilities
add_filter('get_terms', 'crb_fix_get_terms_wpml', 0);
function crb_fix_get_terms_wpml($terms) {
global $wp_filter, $sitepress;
if ( is_admin() && !empty($sitepress) && !empty( $wp_filter['get_terms'] ) ) {
foreach ($wp_filter['get_terms'] as $priority => $callbacks) {
foreach ($callbacks as $callback_key => $callback) {
@tyxla
tyxla / comment-rating.php
Created September 2, 2015 07:56
Comments rating: total comments and average rating
<?php
global $wpdb;
// total comments
$post_id = 10;
$total_comments = $wpdb->get_var( "
SELECT COUNT(meta_value) FROM $wpdb->commentmeta
WHERE comment_id = $post_id
AND meta_key = '_comment_rating'
@tyxla
tyxla / association-custom-values.php
Created November 16, 2015 16:18
Association field with custom values
<?php
# Our available options
function crb_test_options() {
return array(
123 => array(
'id' => 123, // ID of the entry, value that is saved in the DB, should be the same as the array key.
'title' => 'Test Title', // title of the item, used for displaying the item in the admin
'type' => 'test', // type of the item, used for identifying this type of item
'subtype' => 'test item', // subtype of the item, in this solution it will be used only for presentation purposes
@tyxla
tyxla / increment.js
Created November 23, 2015 15:14
incrementNumberAnimation
function incrementNumberAnimation($element) {
var from = $element.text();
var to = $element.data('increment-to');
$({number: from}).animate({number: to}, {
duration: $element.data('increment-duration'),
easing: 'swing',
step: function() {
$element.text(Math.ceil(this.number));
}
});
@tyxla
tyxla / iselementinviewport.js
Created November 23, 2015 15:46
isElementInViewport
function isElementInViewport(el) {
if ( document.documentElement.clientWidth <= 767 && document.documentElement.clientHeight < document.documentElement.clientWidth ) {
return $(el).is(':visible');
}
if (typeof jQuery === "function" && el instanceof jQuery) {
el = el[0];
}
@tyxla
tyxla / carbon-fields-setup.php
Created February 2, 2016 21:53
Registering Carbon Fields at the right hook.
<?php
use Carbon_Fields\Container\Container;
use Carbon_Fields\Field\Field;
add_action('carbon_register_fields', 'my_carbon_fields_setup');
function my_carbon_fields_setup() {
Container::make('post_meta', 'test1')
->show_on_post_type('post')
->add_fields(array(
Field::make('complex', 'crb_test1')->add_fields(array(