Skip to content

Instantly share code, notes, and snippets.

@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();
}
}
// VALUE UNIT STRIPPING AND CONVERSION FUNCTION TOOLS
// By Nicolas Giethlen (aka Chmood)
// VARIABLES
$browser-fontsize-base: 16 !default; // Default browser font-size in px (unitless)
$pixels-per-rem: 10 !default; // Root font-size in px (unitless)
// FUNCTIONS
@esedic
esedic / extractHexBackgroundColorFromElement.js
Created December 5, 2023 11:45
Extract computed background color hex value from given element object
var el = document.getElementById('target');
function extractHexBackgroundColorFromElement(element) {
return getComputedStyle(element)['background-color'].split('(')[1].split(')')[0].split(',').map((x) => x.trim()).map((x) => { x = parseInt(x).toString(16); return (x.length === 1) ? '0' + x : x }).join('');
}
console.log(extractHexBackgroundColorFromElement(el));
@esedic
esedic / em2px.js
Created December 5, 2023 11:42
Calculate em to pixel value with JavaScript
function em2px(em) {
return Number(em) * Number(window.getComputedStyle(document.body).getPropertyValue('font-size').match(/\d+/)[0]);
}
console.log(em2px(2));
@esedic
esedic / bots.php
Created November 20, 2023 23:34
Prevent PHP execution if USER_AGENT is bot
<?php
// While not foolproof, you can check the USER_AGENT string and only run that code if 'bot' does not exist.
if(stripos($_SERVER['HTTP_USER_AGENT'],'bot') === false){ }
// This would stop any bot that actually has bot in the user agent string that also does not pay attention to robots.txt.
// Source: https://stackoverflow.com/a/17600047/1090042
@esedic
esedic / wp_comments.php
Created November 2, 2023 10:20
Disable WordPress comments
<?php
/***********************************
*
* ! DISABLE COMMENTS
*
* *********************************/
function df_disable_comments_post_types_support()
{
@esedic
esedic / wpsvg.php
Created November 2, 2023 10:17
Add SVG support to WordPress
<?php
class WPSVG{
function __construct(){
add_action( 'admin_init', array( $this, 'add_svg_support' ) );
add_action( 'admin_footer', array( $this, 'fix_svg_thumbnail_size' ) );
add_filter( 'upload_mimes', array( $this, 'add_svg_mime' ) );
add_filter( 'wp_check_filetype_and_ext', array( $this, 'wp_check_filetype_and_ext' ), 100, 4 );
add_filter( 'wp_generate_attachment_metadata', array( $this, 'wp_generate_attachment_metadata' ), 10, 2 );
add_filter( 'fl_module_upload_regex', array( $this, 'fl_module_upload_regex' ), 10, 4 );