Skip to content

Instantly share code, notes, and snippets.

@esedic
esedic / gist:8f50ddfb2497961434bf891ac2c6395d
Created April 17, 2024 12:29 — forked from vdrover/gist:ebdbf65f5f4e9ee5545255e9c36bd422
Wordpress - Backdate scheduled posts
// Adds a meta box to the post editing screen for changing the post's publication
// date and time after it has initially been published.
// Only visible to administrators.
// Place in functions.php file.
function cdap_add_meta_box() {
// Check if the current user is an administrator
if (current_user_can('administrator')) {
add_meta_box(
'cdap_meta_box', // ID of the meta box
@esedic
esedic / removeel.js
Created March 8, 2024 18:19
Find and remove DOM elements with JavaScript
/**
* Find all the elements, that match any of the classes that are specified in the array
* and remove all the matching elements from the DOM
*/
// Step 1: Define the array of class names
const classesToRemove = ['elem1', 'elem2'];
// Step 2: Create the selector string
// This will create a string like ".class1,.class2,.class3"
@esedic
esedic / wpcli.txt
Created March 7, 2024 12:23
Install WP-CLI and check if WP-CRON works
1. Download wp-cli.phar using wget or curl:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
2. Check if it works:
php wp-cli.phar --info
3. To be able to type just wp, instead of php wp-cli.phar, you need to make the file executable and move it to somewhere in your PATH. For example:
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
@esedic
esedic / sameday.sql
Created February 29, 2024 11:08
Select items/articles that were created on the same day
SELECT * FROM mytable WHERE MONTH(created) = MONTH(CURDATE()) AND DAY(created) = DAY(CURDATE()));
@esedic
esedic / map.js
Created February 9, 2024 11:01
Javascirpt if/else vs switch vs object mapping
// Scenario, wher we need to determine a discount based on a customer's loyalty level
// 1. Traditional if-else approach:
function calculateDiscount(loyaltyLevel) {
console.time('if else time');
if (loyaltyLevel === 'GOLD') {
applyGoldDiscount();
} else if (loyaltyLevel === 'SILVER') {
applySilverDiscount();
} else if (loyaltyLevel === 'BRONZE') {
@esedic
esedic / mysqlpw.txt
Created February 7, 2024 13:30
Change MySQL root password
sudo mysql
// get MySQL user accounts
SELECT user,authentication_string,plugin,host FROM mysql.user;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
FLUSH PRIVILEGES;
// you should see the change
@esedic
esedic / wphook.php
Created January 30, 2024 16:32
WordPress hook inside another hook
/*
To use one hook inside another hook in WordPress, you can simply add the second hook as a callback function to the first hook
For example, let's say you want to add a custom function to the init hook, which in turn uses the wp_enqueue_scripts
hook to enqueue some scripts. You can do it like this:
*/
<?php
function my_init_function() {
// do some initialization here
add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts_function' );
/*
I would like to get a plugin code for Wordpress which goes trough all posts, foreach trough each of them,
get an short meta description from main description using Microsoft OpenAI API access.
This plugin / code should be run trough CRON commands. Can you generate code for me?
-------------------------------------------------------------------------------------
Certainly, I can help you with a basic outline for a WordPress plugin that does what you've described.
Please note that you'll need to have the Microsoft OpenAI API credentials and the appropriate library installed on your server.
Also, you should handle error cases and edge cases based on your specific needs.
Here's a basic example using PHP for the plugin:
@esedic
esedic / git.txt
Last active January 22, 2024 09:26
Git clone repo at certain commit
Step 1: clone the main repo:
git clone sample.com/component.git
Get inside the cloned repo cd folder_name. Now you have a working directory with latest version pulled
and you want to move back to a specific commit id, here's how you can do it:
Step 2:
git reset --hard 9a36b9e79tbb9132c7 # (takes you back to that commit)
Step 3:
git clean -df # (cleans any untracked files/folders)
@esedic
esedic / url.js
Created January 17, 2024 15:14
Remove URL parameters and reload page
function removeParamAndRefresh(paramToRemove) {
const url = new URL(window.location.href);
if (url.searchParams.has(paramToRemove)) {
url.searchParams.delete(paramToRemove);
// Update the URL in the address bar without reloading
window.history.pushState({}, '', url);
// Now, reload the page
window.location.reload();
}
}