Skip to content

Instantly share code, notes, and snippets.

View tevashov's full-sized avatar

Teff tevashov

View GitHub Profile
@tevashov
tevashov / Asset.php
Last active October 27, 2022 21:56
Get posts from custom tax grouped by terms (WP API) #WP
$myterms = get_terms('retouch_types', 'orderby=none&hide_empty');
foreach ($myterms as $term) {
setup_postdata($post);
$term->name;
//select posts in this category, and of a specified content type
$posts = get_posts( array(
'post_type' => 'retouch',
'tax_query' => array(
@tevashov
tevashov / Asset.php
Last active October 27, 2022 21:57
Define host-sensitive variables (DB configs, paths etc.) #WP
if (stripos($_SERVER['SERVER_NAME'], '.local')) {
// localhost
} elseif (stripos($_SERVER['SERVER_NAME'], 'dev.tevashov.ru')) {
// development
} else {
// production
}
@tevashov
tevashov / Asset.js
Last active October 27, 2022 21:54
How to use logical operators for flow control #JS
// do something with foo if foo is truthy
foo && doSomething(foo);
// set bar to baz if baz is truthy;
// otherwise, set it to the return
// value of createBar()
var bar = baz || createBar();
@tevashov
tevashov / Asset.php
Last active October 27, 2022 21:21
Apply custom CSS to Admin Area #WP
function my_custom_fonts() {
echo '<style>
body, td, textarea, input, select {
font-family: "Lucida Grande";
font-size: 12px;
}
</style>';
}
add_action('admin_head', 'my_custom_fonts');
@tevashov
tevashov / Asset.js
Last active October 27, 2022 21:53
How to force a string to act as a number #JS
var foo = 1;
var bar = '2';
console.log(foo + bar); // 12. uh oh
// coerce the string to a number (see also parseInt())
console.log(foo + Number(bar));
// Forcing a string to act as a number (using the unary-plus operator)
console.log(foo + +bar);
@tevashov
tevashov / Asset.js
Last active October 27, 2022 21:52
How to use callbacks in jQuery #jQuery
function runAlertNow () {
$(this).html('I just ran this function!');
}
// both of these lines do the same thing
$('#test').slideUp(400, runAlertNow);
$('#test').slideUp(400, function ()
{
$(this).html('I just ran the anonymous function!');
});
@tevashov
tevashov / Asset.js
Last active October 27, 2022 21:53
How to: TRUE and FALSE evaluating #JS
// TRUE
'0';
'any string';
[]; // an empty array
{}; // an empty object
1; // any non-zero number
// FALSE
@tevashov
tevashov / Asset.js
Last active October 28, 2022 07:26
Check some element for existing #jQuery
if ($(selector).length) {
// your code here
}
@tevashov
tevashov / Asset.js
Last active October 27, 2022 21:52
Access elements with IDs containing special characters #jQuery
$("$some[id]").show(); // won't work for this type of ID
$("$some\\[id\\]").show() // works fine for the ID: some[id]
@tevashov
tevashov / Asset.js
Last active October 27, 2022 21:52
Combine Slide and Fade Functions #jQuery
$.fn.slideFadeToggle  = function(speed, easing, callback) {
        return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
};
// Usage:
$("#something").click(function() {
   $(this).slideFadeToggle();
});