Skip to content

Instantly share code, notes, and snippets.

View kamalahmed's full-sized avatar
💻
Learning & working in a silent mode

Kamal Ahmed kamalahmed

💻
Learning & working in a silent mode
View GitHub Profile
<?php
function balkan_update_user_role( $user_id, $role ) {
// Site 1
// Change value if needed
$prefix_1 = 'main_';
// Site 2 prefix
// Change value if needed
@kamalahmed
kamalahmed / remove-extra-junk-from-wp-head.php
Last active March 6, 2020 18:03
You can increase your WordPress Site speed by removing extra information from wp head that you may not need.
<?php
/**
* It removes unnecessary actions and filters to clean up the WP head to speed up
*/
function prefix_clean_wp_head() {
// https://scotch.io/tutorials/removing-wordpress-header-junk
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wp_generator' );
remove_action( 'wp_head', 'feed_links', 2 );
@kamalahmed
kamalahmed / singleton-example.php
Last active March 6, 2020 18:06
Singleton Design Pattern
<?php
final class Sample_Plugin {
private static $instance;
// prevent direct instantiation
private function __construct() {
}
@kamalahmed
kamalahmed / fix-svg-upload-error-in-wordpress.php
Last active October 3, 2023 13:16
Fix the “Sorry, this file type is not permitted for security reasons.” issue on WordPress while uploading an SVG file
<?php
/**
* A workaround for upload validation which relies on a PHP extension (fileinfo) with inconsistent reporting behaviour.
* ref: https://core.trac.wordpress.org/ticket/39550
* ref: https://core.trac.wordpress.org/ticket/40175
*/
function prefix_filter_fix_wp_check_filetype_and_ext( $data, $file, $filename, $mimes ) {
if ( ! empty( $data['ext'] ) && ! empty( $data['type'] ) ) {
return $data;
}
@kamalahmed
kamalahmed / set-default-fonts-in-elementor-from-theme-or-plugin.php
Last active February 6, 2024 14:54
Update or Set new default fonts in Elementor from your plugin or theme
<?php
add_action('elementor/loaded', 'prefix_register_custom_default_fonts');
// register custom fonts only when elementor is ready
function prefix_register_custom_default_fonts(){
$new_default_fonts = [
//Primary Headline
[
@kamalahmed
kamalahmed / detect-new-wordpress-post.php
Last active February 5, 2020 16:52
Detect when a new WordPress post is created or published. You can do some work ONLY when a NEW post/page/custom is created using the following gist.
<?php
/**
* @param string $new_status the new status of the post eg. publish
* @param string $old_status the old status of the post eg. draft, publish etc
* @param WP_Post $post
*/
function prefix_do_something_when_a_new_post_published($new_status, $old_status, $post) {
// Do something On first publish
if ($new_status == 'publish' && $old_status != 'publish' && isset($post->post_type)) {
@kamalahmed
kamalahmed / wp-attachment-url-to-path.php
Created February 14, 2019 17:23
You can convert an attachment url to its absolute path in WordPress using this function.
<?php
/**
* Get the attachment absolute path from its url
*
* @param string $url the attachment url to get its absolute path
*
* @return bool|string It returns the absolute path of an attachment
*/
function attachment_url_to_path( $url )
<?php
/**
* Functions
*
* @package amgeddotme2017
*/
/**
Updating the below article to work with Bootstrap 4
@kamalahmed
kamalahmed / gist:ed55e4cd8a226c6ffcb8c88b1ce70b58
Created July 30, 2017 08:37 — forked from toddlahman/gist:6302280
Save Plugin Error Messages in the database for The Plugin Generated x Characters of Unexpected Output During Activation errors
<?php
function tl_save_error() {
update_option( 'plugin_error', ob_get_contents() );
}
add_action( 'activated_plugin', 'tl_save_error' );
/* Then to display the error message: */
@kamalahmed
kamalahmed / font-awesome.php
Created July 25, 2017 09:34 — forked from justintadlock/font-awesome.php
PHP array of Font Awesome icons.
<?php
// Font Awesome v. 4.6.
function jt_get_font_icons() {
return array(
'fa-glass' => 'f000',
'fa-music' => 'f001',
'fa-search' => 'f002',
'fa-envelope-o' => 'f003',