Skip to content

Instantly share code, notes, and snippets.

@marko-stimac
marko-stimac / wpml-create-translated-posts.php
Created December 13, 2019 13:14
Create translated posts in WPML
// This is not planned for hierarchical posts
add_action('wp_footer', 'create_wpml_translated_posts');
function create_wpml_translated_posts() {
$post_type = 'post';
$new_language = 'en';
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1
@marko-stimac
marko-stimac / .htaccess
Last active May 31, 2019 06:47
Htaccess - redirect old domain pages
# Catch 404
ErrorDocument 404 https://oldsite.com/404
RewriteEngine on
# Single page redirect
Redirect 301 /team.php https://newsite.com/our-team/
# Redirect all query variables with url something like https://oldsite.com/blog-list?p=10 to https://newsite.com/en/ (question mark in the end removes query parameter on new page)
RewriteCond %{QUERY_STRING} ^p=(.*)
@marko-stimac
marko-stimac / .htaccess
Created November 20, 2018 10:17
Htaccess - redirect all to root except folder
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/_skip-this-folder$
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$
RewriteRule .* / [L,R=302]
RewriteCond %{HTTP_HOST} ^www.domain.hr [NC]
RewriteRule ^(.*) https://domain.hr/$1 [L,R=301]
@marko-stimac
marko-stimac / a11y-event-handler.js
Last active October 17, 2018 19:32
JS - Accessibility event handler
// Accessibility compliant event handler
// Taken from http://www.karlgroves.com/2014/11/24/ridiculously-easy-trick-for-keyboard-accessibility/
function a11yClick(event) {
if(event.type === 'click'){
return true;
}
else if(event.type === 'keypress') {
var code = event.charCode || event.keyCode;
if((code === 32)|| (code === 13)){
@marko-stimac
marko-stimac / wordpress-get-the-date.php
Created September 24, 2018 09:37
Wordpress - get_the_date() to locale
<?php
if (ICL_LANGUAGE_CODE == 'hr') {
setlocale(LC_TIME, 'hr_HR.UTF-8');
echo strtolower(strftime("%e. %B %Y.", get_post_time('U')));
}
else if (ICL_LANGUAGE_CODE == 'en') {
setlocale(LC_TIME, 'en_US.UTF-8');
echo strftime("%A %e %B %Y", get_post_time('U'));
}
?>
@marko-stimac
marko-stimac / animations.js
Last active September 16, 2018 00:21
JS - Animate methods
jQuery(document).ready(function ($) {
'use strict';
// Add class ('appeared') to each element that should be animated when visible on viewport
// Uses scrollmagic.js
var controller = new ScrollMagic.Controller();
$('.animate').each(function () {
$(this).addClass('disappear');
new ScrollMagic.Scene({
@marko-stimac
marko-stimac / comment-form.php
Created August 26, 2018 13:46
Wordpress - show comments and comment form
<!-- Be sure to allow commenting in Wordpress settings -->
<ol class="commentlist">
<?php
$comments = get_comments(array(
'post_id' => get_the_ID(),
'status' => 'approve'
));
wp_list_comments(array(
'per_page' => 30,
@marko-stimac
marko-stimac / wordpress-unit-tests-tutorial.txt
Created August 15, 2018 11:02
Wordpress - How to run unit tests
How to run plugin unit tests on Windows 10, currently I can get it to work only with PHP 5
- composer require --dev phpunit/phpunit ^5
- wp scaffold plugin-tests {name-of-plugin-folder}
- rm -rf /tmp/wordpress-tests-lib; bash bin/install-wp-tests.sh wordpress_db_test root '' localhost latest
- edit file C:/Users/User/AppData/Local/Temp/wordpress-tests-lib/wp-tests-config.php and change path so that instead of relative path there is absolute path to correct folder
define('ABSPATH', 'C:/Users/User/AppData/Local/Temp/wordpress/');
- vendor/bin/phpunit
@marko-stimac
marko-stimac / create-project-backup
Last active May 31, 2019 06:50
VPS - Create backup for projects
#!/bin/bash
# Create backup for projects while skipping unimportant folders
TIME=`date +%b-%d-%y`
FILENAME=html-$TIME.tar.gz
SRCDIR=/var/www/html
DESDIR=/mnt/ftp/backups
tar -cpzf $DESDIR/$FILENAME --exclude=**/node_modules/* $SRCDIR
@marko-stimac
marko-stimac / remove-old-backup-files
Created July 27, 2018 19:41
VPS - Remove old backup files
#!/bin/sh
# Remove old backup files
FILENAME="$(ls /mnt/ftp/backups -rt | head -1)"
rm /mnt/ftp/backups/$FILENAME
FILENAME2="$(ls /mnt/ftp/dbbackups -rt | head -1)"
rm /mnt/ftp/dbbackups/$FILENAME2