Skip to content

Instantly share code, notes, and snippets.

@ville6000
ville6000 / functions.php
Created October 29, 2015 07:56
Add localized JavaScript variables to WordPress theme
<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
wp_localize_script(
'script',
'i18n',
array(
'download' => __( 'Download file', 'mytheme' ),
)
);
@ville6000
ville6000 / functions.php
Last active April 18, 2016 07:12
Add siblings field to WP REST API data
<?php
add_action( 'rest_api_init', 'slug_register_siglings' );
function slug_register_siglings() {
register_api_field( 'post',
'siblings',
array(
'get_callback' => 'slug_get_siglings',
'update_callback' => null,
'schema' => null,
@ville6000
ville6000 / index.php
Last active October 22, 2015 08:27
Variable demo 2
<?php
$post_idx = 0;
if ( have_posts()) {
while ( have_posts()) {
the_post();
// $post_idx is available in content-page.php
include( locate_template( 'content-page.php' ) )
$post_idx += 1;
}
@ville6000
ville6000 / index.php
Last active October 22, 2015 08:27
Variable demo
<?php
$post_idx = 0;
if ( have_posts()) {
while ( have_posts()) {
the_post();
// $post_idx is not available in content-page.php
get_template_part('content', 'page');
$post_idx += 1;
}
@ville6000
ville6000 / script.js
Created May 13, 2015 06:22
Disable scroll wheel zoom on Google Maps
var map;
var mapOptions = {
zoom: 15,
scrollwheel: false,
center: new google.maps.LatLng( latitude, longitude )
};
map = new google.maps.Map(
document.getElementById( 'map-canvas' ),
mapOptions
@ville6000
ville6000 / functions.php
Last active April 14, 2017 05:18
Change post per page
<?php
function change_posts_per_page( $query ) {
if ( is_main_query() && is_archive() ) {
$query->set( 'posts_per_page', -1 );
}
}
add_action( 'pre_get_posts', 'change_posts_per_page' );
@ville6000
ville6000 / functions.php
Last active October 22, 2015 08:26
Get current url in WordPress
<?php
function get_current_url() {
global $wp;
return add_query_arg( $_SERVER['QUERY_STRING'], '', home_url( $wp->request ) );
}
@ville6000
ville6000 / index.php
Last active October 22, 2015 08:26
Get post thumbnail URL
<?php
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'image-size' );
$url = $thumb['0'];
@ville6000
ville6000 / template.php
Last active October 22, 2015 08:18
Get translated permalink with WPML
<?php $icl_object_id = icl_object_id( get_the_ID(), 'post', true ); ?>
<a href="<?php echo get_permalink( $icl_object_id ); ?>">
<?php _e( 'Read more', 'your_domain' ); ?>
</a>
@ville6000
ville6000 / template.php
Created March 26, 2015 12:38
WP_Query with a date filter on a custom field
<?php
$the_query = new WP_Query( array(
'post_type' => 'trip',
'post_per_page' => $limit,
'meta_key' => 'wpcf-start-date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'key' => 'wpcf-start-date',
'value' => current_time( 'timestamp' ),