Skip to content

Instantly share code, notes, and snippets.

@alphasider
alphasider / concat_rows.sql
Last active September 22, 2021 06:42
Concat cells (rows) of one column by another column
# column_name - column name filter by
# column_name_2 - column name to concat
# table_name - table name :)
SELECT column_name,
GROUP_CONCAT(DISTINCT column_name_2 SEPARATOR ',')
FROM table_name
GROUP BY column_name;
@alphasider
alphasider / get-svg-dimensions.php
Created May 3, 2021 09:56
Returns dimensions (width and height) of a given svg (by url)
/**
* Returns dimensions (width and height) of a given svg (by url)
*
* @param string $image_url
* @source https://stackoverflow.com/a/6532357/9214537
*
* @return string
*/
function get_svg_dimensions( string $image_url ): string {
$parsed_xml = simplexml_load_string( file_get_contents( $image_url ) );
@alphasider
alphasider / recalculate-acf-locations.php
Created January 19, 2021 11:54 — forked from RadGH/recalculate-acf-locations.php
Get latitude and longitude for addresses saved in Advanced Custom Fields, using Google's GeoLocation API
<?php
global $acf_recalc_settings;
// IMPORTANT: Customize these settings for your website.
$acf_recalc_settings = array(
// How many updates to do each page load. As of November 2018, Google's GeoLocation API limit is 100 per second.
'posts_per_run' => 16,
@alphasider
alphasider / file-upload-handler.php
Created December 27, 2020 11:55 — forked from daltonrooney/file-upload-handler.php
Multi-file WordPress uploads from the front-end
<?php /* This function attaches the image to the post in the database, add it to functions.php */
function insert_attachment($file_handler,$post_id,$setthumb='false') {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
@alphasider
alphasider / disable-theme-editor.php
Created November 11, 2020 05:30
Snippet for disabe editing files in WordPress
<?php
/**
* Disable Theme Editor
* Source: http://guides.beanstalkapp.com/deployments/deploying-wordpress.html
*/
function remove_editor_menu() {
remove_action('admin_menu', '_add_themes_utility_last', 101);
}
<?php
/**
* This template used in regular posts pages.
* $post->ID will taken from those posts
*
* @var $post
* TODO: Explain what does this code
*/
$tags = wp_get_post_tags($post->ID);
<?php
// TODO: ecplain what does this code
$tags = wp_get_post_terms($product_id, 'product_tag');
$tag_slug = [];
?>
<?php if ($tags) :
foreach ($tags as $tag) {
$tag_slug[] = $tag->slug;
}
<?php
/**
* Gets all products by category ID
* Original source: https://wordpress.stackexchange.com/questions/143582/get-the-product-list-of-a-given-category-id
*/
<ul class="relatedlinks">
<?php
$args = [
'post_type' => 'product',
@alphasider
alphasider / woo_add_product_programmatically.php
Created September 21, 2020 11:23
Add woocommerce product programmaticaly (with attributes)
<?php
/**
* SOURCE: https://stackoverflow.com/questions/52937409/create-programmatically-a-product-using-crud-methods-in-woocommerce-3
*/
/* THE CODE FUNCTION */
// Custom function for product creation (For Woocommerce 3+ only)
function create_product( $args ){
global $woocommerce;
@alphasider
alphasider / multidimensional-array.js
Created June 23, 2020 18:56
Makes multidimensional array from an existing array
function makeMultidimensionalArray(inputArray, arraysCount = 20, arrayLength = 1) {
return [...Array(arraysCount)] // Create an array with number of elements (default = 20)
.map((item, index) => {
return Array(arrayLength).fill(inputArray[index]) // Make an inner array with the length of "index" (default = 1)
})
}