Skip to content

Instantly share code, notes, and snippets.

Avatar

Sabrina Zeidan sabrina-zeidan

View GitHub Profile
@sabrina-zeidan
sabrina-zeidan / wordpress_multisite_compatible_site_options_actions.php
Created Oct 16, 2018
To use one single function for stand-alone WordPress install and Multisite (in case options are network wide). Get/Update/Delete options with the same function with no need to check whether it's Multisite every time
View wordpress_multisite_compatible_site_options_actions.php
//Check if plugin is Network activated
if (!function_exists('is_plugin_active_for_network')) require_once( ABSPATH . '/wp-admin/includes/plugin.php');
$plugins = get_plugins();
foreach( $plugins as $plugin_file => $plugin_info ) {
if ( $plugin_info['TextDomain'] === $this->plugin_name ){
$plugin = $plugin_file;
break;
}
}
//And set constant
@sabrina-zeidan
sabrina-zeidan / wordpress_multisite_global_menu_anywhere.php
Created Sep 10, 2018
Get shared menu from the main site displayed anywhere in the WordPress Multisite network
View wordpress_multisite_global_menu_anywhere.php
//Get $blog_id to remember where we currently are
global $blog_id;
$current_blog_id = $blog_id;
//switch to the main site which has id of 1
switch_to_blog(1);
//Check if menu exists (regardless location)
if (wp_nav_menu(array('menu' => 'social-links'))):
//output the WordPress navigation menu, use slug/name/id
wp_nav_menu( array( 'menu' => 'social-links') );
endif;
@sabrina-zeidan
sabrina-zeidan / leave_good_tails.php
Created May 16, 2018
This function will clean your list of keywords from partially duplicated lines and return just good long-tailed keyword phrases
View leave_good_tails.php
<?php
/*
* This function will clean your list of keywords from partially duplicated lines and return just good long-tailed keyword phrases
* To learn more about about long-tails in SEO check http://sabrinazeidan.com/long-tail-keywords-for-seo-definition-examples-free-tool/
* Input file format: file.txt, UTF-8 without BOM, one keyword phrase per line
*
*/
function leave_good_tails($txt_file_url) {
$file_headers = @get_headers($txt_file_url);
if ($file_headers[0] == 'HTTP/1.0 404 Not Found'){ // or "HTTP/1.1 404 Not Found" etc.
@sabrina-zeidan
sabrina-zeidan / query_meta_without_value.php
Last active May 27, 2020
meta_query to search post without custom field (no matter whether its empty or doesn't exist at all [WordPress]
View query_meta_without_value.php
$getposts = get_posts( array(
'numberposts' => -1,
'post_type' => 'post',
'post_status' => 'any',
'fields' => 'ids',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'custom_field',
'compare' => 'NOT EXISTS',
@sabrina-zeidan
sabrina-zeidan / defer_javascript_parsing.php
Created Oct 12, 2017
This function will defer parsing of JavaScript for optimized page load [WordPress]
View defer_javascript_parsing.php
function add_async_and_defer_attribute($tag, $handle) {
//if ( 'adsense' == $handle ) return str_replace( 'src', 'async src', $tag );
//if ( strpos( $tag, 'jquery.js' ) ) return $tag;
return str_replace( 'src', 'defer onload="" src', $tag );
}
add_filter('script_loader_tag', 'add_async_and_defer_attribute', 10, 2);
@sabrina-zeidan
sabrina-zeidan / add_multiple_googleads.js
Created Oct 8, 2017
jQuery junction for correct inserting multiple Google Adsense blocks on the site. It will generate as many ad pushes as there are GoogleAds blocks on the current page. Automatic, convenient and lean
View add_multiple_googleads.js
jQuery(function ($) {
$(".adsbygoogle").each(function () { (adsbygoogle = window.adsbygoogle || []).push({}); })
});
@sabrina-zeidan
sabrina-zeidan / my_custom_image_size.php
Last active May 18, 2020
This function will create additional custom size of images in WordPress and prevent creating 2 default sizes. Second snippet controls correct name display in Media Library [WordPress]
View my_custom_image_size.php
function sz_posts_image_sizes() {
add_image_size('sz-post-desktop', 740); //for regular images in posts: 740px wide, height accordingly
add_image_size('sz-post-desktop-preview', 227, 200, true); //for post thumbnails preview for archives: 227px wide and 200px tall, hard crop mode
add_image_size('sz-post-mobile', 345);
add_image_size('sz-post-mobile-preview', 350, 242 , true);
//to remove default sizes 'thumbnail' and 'medium'
remove_image_size('thumbnail');
remove_image_size('medium');
}
@sabrina-zeidan
sabrina-zeidan / resize_images_gd_image_editor.php
Last active May 18, 2020
Resize images that are NOT attachments [WordPress]
View resize_images_gd_image_editor.php
/**
* In this example book covers are stored in custom fields.
* The max size used on site for book cover 300px *450px, so there is no point to store image large than this.
* We'll resize all existing book covers to this size using GD library.
* This is popular PHP image processor widely supported by hosting providers out of box.
* Also WordPress itself has got in-built support for that through WP_Image_Editor_GD class. Great for development!
**/
require_once( ABSPATH . 'wp-includes/class-wp-image-editor.php' );
require_once( ABSPATH . 'wp-includes/class-wp-image-editor-gd.php' );
@sabrina-zeidan
sabrina-zeidan / trigger.php
Last active Apr 18, 2020
Trigger for WordPress Multisite CRON [WordPress]
View trigger.php
if ( is_multisite()) {
$sites = get_sites();
foreach ($sites as $site ) {
$blog_id = $site->blog_id;
//if ($blog_id < 6 ){ //if we need just certain blogs
switch_to_blog( $blog_id );
wp_suspend_cache_addition(true);
//here's what we're gonna do...
$site_url = get_site_url($blog_id);
$command = $site_url.'/wp-cron.php?doing_wp_cron';
@sabrina-zeidan
sabrina-zeidan / bulk_actions_wrapper_wp_mu.php
Last active Apr 18, 2020
Wrapper for bulk actions in WP MU install [Wordpress]
View bulk_actions_wrapper_wp_mu.php
if ( is_multisite()) {
$sites = get_sites();
foreach ($sites as $site ) {
$blog_id = $site->blog_id;
//if ($blog_id < 6 ){ //if we need just certain blogs
switch_to_blog( $blog_id );
wp_suspend_cache_addition(true);
//here's what we're gonna do...
wp_suspend_cache_addition(false);
You can’t perform that action at this time.