Skip to content

Instantly share code, notes, and snippets.

@adamrosloniec
adamrosloniec / gist:c3f49412db53f7255d4b126b8a025b71
Created January 29, 2021 12:55
PHP - Convert/Format string apostrophes at json_encode
function formattedString($string) {
return str_replace([''', '"'], "'", filter_var($string, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH));
}
@adamrosloniec
adamrosloniec / gist:82607ce38d235fa172c5b396f9f8a249
Last active December 3, 2020 22:47
CSS - Grid-template-columns - with wrapping
.grid-tiles {
width: 100%;
display: grid;
// grid-template-columns: repeat(4, 1fr); // <- it doesn't wrap elements
grid-template-columns: repeat(4, minmax(calc(#{percentage(1/4)} - #{pxToRem(16)}), 1fr)); // <- it wrap!
grid-column-gap: pxToRem(16);
grid-row-gap: pxToRem(16);
}
@adamrosloniec
adamrosloniec / gist:9abc8ca032563f7094557d76931b3f7e
Created October 28, 2020 17:52
macos - how to fix error: gyp: No Xcode or CLT version detected!
# How to fix error - gyp: No Xcode or CLT version detected!
# source: https://stackoverflow.com/questions/60573595/npm-install-fails-on-node-gyp-rebuild-with-gyp-no-xcode-or-clt-version-detec
# 1.
xcode-select --print-path
# in my case /Library/Developer/CommandLineTools
# 2.
# the next line deletes the path returned by the command above
sudo rm -rf $(xcode-select --print-path)
@adamrosloniec
adamrosloniec / gist:59bd772829a6b1064c6fba48f634ffdf
Created November 15, 2019 05:01
WordPress - Eliminate render-blocking resources
function addRelPreloadTagToEnqueueStyles($html, $handle) {
// Put to array all styles which you want to add tag rel="preload"
$styles = ['app', 'wp-block-library', 'contact-form-7', 'cf7cf-style'];
foreach ($styles as $singleStyle) {
$html = preg_replace("/id='" . $singleStyle . "-css'/", " id='" . $singleStyle . "-css' rel='preload' as='style' ", $html);
}
return $html;
}
add_filter('style_loader_tag', 'addRelPreloadTagToEnqueueStyles');
@adamrosloniec
adamrosloniec / gist:d606d68f652151242307005ae0cb44cf
Created November 15, 2019 04:15
WordPress - The best way to get_the_excerpt with character limit
// Source: https://stackoverflow.com/a/10173268
function get_the_excerpt_with_characters_limit($text, $length) {
// Don't cut if too short
if (strlen($text) < $length + 10) {
return $text;
}
// Find next space after desired length
$break_pos = strpos($text, ' ', $length);
$visible = substr($text, 0, $break_pos);
@adamrosloniec
adamrosloniec / react-hooks.js
Created October 31, 2019 00:57
React Hooks - useEffect as componentDidMount & componentWillUnmount
useEffect(() => {
// componentDidMount
InitAnyFunctionAfterDidMount();
// componentWillUnmount
return () => {
InitAnyFunctionAfterUnmount();
};
}, []);
@adamrosloniec
adamrosloniec / gist:9cd5d5135ec7e94ce10d177d28fefb94
Created September 26, 2019 08:00
JS - Convert (replace) or Map - key to value from object
function convertState(state) {
const states = {
'new-south-wales': 'NSW',
'queensland': 'QLD',
'south-australia': 'SA',
'western-australia': 'WA',
'northern-territory': 'NT',
'victoria': 'VIC',
'tasmania': 'TAS',
'australian-capital-territory': 'ACT',
@adamrosloniec
adamrosloniec / gist:54f2ab0427cef7f86b439dce624d87a0
Last active August 9, 2019 03:08
WordPress - Hide Login Page with custom hidden $_GET and show website only via Rest API endpoints
<?php
function hide_login_page_and_show_only_rest_api_endpoints() {
// work only on productions sites
if (
!stristr($_SERVER['SERVER_NAME'], 'local')
&& !is_user_logged_in()
) {
if (
(
@adamrosloniec
adamrosloniec / gist:c8d7264dc228bec78c538653c0166679
Last active August 9, 2019 01:42
WordPress - Automatically disallow indexing on staging sites
function theme_disallow_indexing_for_staging() {
if (
str_ireplace(array('staging-subdomain-1.com', 'dev-subdomain-2.net'), '', $_SERVER['SERVER_NAME']) != $_SERVER['SERVER_NAME']
&& get_option('blog_public') == '1'
) {
add_action('pre_option_blog_public', '__return_zero');
}
}
add_action('init', 'theme_disallow_indexing_for_staging');
@adamrosloniec
adamrosloniec / gist:7449ec85245460d65bd3d967f6a02f6d
Created July 24, 2019 03:02
WordPress - Get all posts with taxonomy
get_posts(array(
'posts_per_page' => -1,
'post_parent' => 0,
'post_type' => 'sanctuary', // post type slug
'tax_query' => array(
array(
'taxonomy' => 'territory', // taxonomy slug
'operator' => 'EXISTS'
),
),