Skip to content

Instantly share code, notes, and snippets.

View PauliusKrutkis's full-sized avatar

Pako PauliusKrutkis

  • Panevėžys
View GitHub Profile
@PauliusKrutkis
PauliusKrutkis / cf-dynamic-values.php
Last active March 22, 2017 19:26
Contact form 7 dynamic value populations
<?php
// contact form 7 dynamic value populations
// usage [select field-name values:post-list] (checkbox, radio works as well)
// for adding custom Form-Tag Check https://contactform7.com/2015/01/10/adding-a-custom-form-tag/
function cf7DynamicListExample($tag)
{
$options = $tag['options'];
@PauliusKrutkis
PauliusKrutkis / attachment-html.php
Last active March 7, 2017 20:51
Wordpress - change content attachment html
<?php
function editMediaTemplate($html)
{
$class = 'attachment-link';
return str_ireplace('<a href', sprintf('<a class="%s" href', $class), $html);
}
add_filter('media_send_to_editor', 'editMediaTemplate', 20, 3);
@PauliusKrutkis
PauliusKrutkis / time-ago.php
Last active March 7, 2017 20:50
Time ago script
<?php
// usage: echo timeAgo(get_the_time('U'));
function timeAgo($time)
{
$out = '';
$now = time();
$diff = $now - $time;
@PauliusKrutkis
PauliusKrutkis / wp-ajax-get-posts.php
Last active March 7, 2017 20:53
Wordpress ajax response snippet
<?php
function getPosts()
{
$args = [
'post_type' => 'post',
'posts_per_page' => 5,
'paged' => $_GET['page'],
];
@PauliusKrutkis
PauliusKrutkis / remove-slug.php
Last active March 7, 2017 20:51
Wordpress custom post types - remove slug
<?php
function queryPareRequest($query)
{
if (
!$query->is_main_query()
|| 2 != count($query->query)
|| !isset($query->query['page'])
) {
return;
@PauliusKrutkis
PauliusKrutkis / widget.php
Last active March 7, 2017 20:30
Wordpress widget with acf snippet
<?php
function registerThemeWidgets()
{
register_widget('Example_widget');
}
add_action('widgets_init', 'registerThemeWidgets');
class Example_widget extends WP_Widget
@PauliusKrutkis
PauliusKrutkis / truncate.php
Last active March 7, 2017 20:25
PHP split text into 2 parts based on length
<?php
function truncate($full, $length)
{
$length = abs((int)$length);
if (strlen($full) > $length) {
$first = preg_replace("/^(.{1,$length})(\s.*|$)/s", '\\1', $full);
$second = substr($full, strlen($first));
}
@PauliusKrutkis
PauliusKrutkis / sticky.js
Last active March 12, 2017 20:22
jQuery sticky element script
var sticky = (function($) {
var content = '.page-content';
var element = '.sticky-block';
if (!$(element).length || !(content).length)
return;
var barHeight = $(element).height();
var barOffset = $(element).offset().top;
@PauliusKrutkis
PauliusKrutkis / first-word.js
Created March 12, 2017 20:21
jQuery first word selector script
var firstWord = (function($) {
var selector = '.first-word';
$(selector).each(addWrap);
function addWrap() {
var word = $(this).html();
var index = word.indexOf(' ');
@PauliusKrutkis
PauliusKrutkis / file-input-label.js
Last active March 12, 2017 20:28
jQuery dynamic file input label
var fileInput = (function($) {
var selector = 'input[type="file"]';
$(selector).on('change', modifyLabel);
function modifyLabel(event) {
var fileName = '';
var id = $(this).attr('id');
var label = $('label[for="'+ id +'"]');