Skip to content

Instantly share code, notes, and snippets.

View gioiliop7's full-sized avatar
:octocat:
Hello

Giorgos Iliopoulos gioiliop7

:octocat:
Hello
View GitHub Profile
@gioiliop7
gioiliop7 / api.js
Created January 5, 2023 15:42
Fetch api call [JS]
export async function apiCall(body, method, url) {
let headers = new Headers();
headers.append("Content-Type", "application/json");
const requestOptions = {
method: method,
headers: headers,
body: JSON.stringify(body),
redirect: "follow",
};
try {
@gioiliop7
gioiliop7 / .htaccess
Created January 5, 2023 15:47
[WORDPRESS] [PHP] Allow file download only from admins and their authors
# Protect all files within the uploads folder
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteCond %{HTTP_COOKIE} !.*wordpress_logged_in.*$ [NC]
#RewriteCond %{REQUEST_URI} ^(.*?/?)wp-content/uploads/sites/[0-9]+/user_uploads|answers/.* [NC]
#RewriteRule . http://%{HTTP_HOST}%1/wp-login.php?redirect_to=%{REQUEST_URI} [L,QSA]
RewriteCond %{REQUEST_FILENAME} -f
# RewriteRule needs the path of folder that we have our files. As an example the folder is on uploads folder on wp and named cv_uploads
RewriteRule "^wp-content/uploads/cv_uploads/(.*)$" singledl-file.php?file=$1 [QSA,L]
</IfModule>
@gioiliop7
gioiliop7 / functions.php
Created January 5, 2023 15:48
[PHP][WORDPRESS] Get users email by role
<?php
function get_users_email_by_role($role)
{
$args = array(
'role' => $role,
);
$users = get_users($args);
$emails = array();
foreach ($users as $user) {
@gioiliop7
gioiliop7 / functions.php
Created January 5, 2023 15:49
[PHP][WORDPRESS] Upload files from external url to media gallery
<?php
function uploadToAttachmentsFromURL($url)
{
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($url);
$filename = basename($url);
@gioiliop7
gioiliop7 / functions.php
Created January 5, 2023 15:50
[PHP][WORDPRESS] Get comments count of specific post type
<?php
function getPostTypeCommentsCount($cpt){
$args = array(
'post_type' => $cpt,
'posts_per_page' => -1
);
$commentCount = 0;
@gioiliop7
gioiliop7 / afm.js
Created January 5, 2023 15:52
[JS] Validate Greek Vat Num
function checkAFM(afm) {
afm = afm.split("").reverse().join("");
let Num1 = 0;
for (var iDigit = 1; iDigit <= 8; iDigit++) {
Num1 += afm.charAt(iDigit) << iDigit;
}
return (Num1 % 11) % 10 == afm.charAt(0);
}
@gioiliop7
gioiliop7 / selectRandom.js
Created January 5, 2023 15:52
[JAVASCRIPT] Select random element from an array
function selectRandom(array) {
return array[Math.floor(Math.random() * array.length)];
}
@gioiliop7
gioiliop7 / functions.php
Last active January 5, 2023 15:54
[PHP][WORDPRESS] Donwload a file from external url and attach it to mail and remove it (help with email function send_email_attachment)
<?php
function send_email_attachment($to, $subject, $template,$render_object,$attachments)
{
$headers = array('Content-Type: text/html; charset=UTF-8', 'From: SiteName <noreply@siteDomain>');
$GLOBALS["use_html_content_type"] = TRUE;
ob_start();
require(locate_template($template));
$body = ob_get_contents();
ob_end_clean();
@gioiliop7
gioiliop7 / functions.php
Created January 5, 2023 15:59
[PHP][WORDPRESS]Get post of specific date (Today's date example)
<?php
function getPostOfSpecificDate($date){
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'any',
'date_query' => array(
array(
'year' => $date['year'],
@gioiliop7
gioiliop7 / timezone.php
Created January 5, 2023 16:00
[PHP] Get current date or time of specific timezone
// If our server has a timezone out of Greece as an example , we can use this function to get the time of that timezone
// Supported timezones https://www.php.net/manual/en/timezones.php
// Supported time & date formats https://www.php.net/manual/en/datetime.format.php
function GetCityTimeDate($format,$timezone)
{
$tz = $timezone;
$timestamp = time();
$dt = new DateTime("now", new DateTimeZone($tz)); //first argument "must" be a string
$dt->setTimestamp($timestamp); //adjust the object to correct timestamp