Instantly share code, notes, and snippets.

View wordpress_multisite_unfiltered_html.php
//Let any user who can edit page insert unfiltered html in text editor
add_action( 'init', 'reply_unfiltered_html' );
function reply_unfiltered_html() {
$user = wp_get_current_user();
if ( current_user_can('edit_pages') )
kses_remove_filters();
}
//Let users with certain roles insert unfiltered html in text editor
add_action( 'init', 'reply_unfiltered_html_for_roles' );
function reply_unfiltered_html_for_roles() {
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
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;
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.
View query_meta_without_value.php
$getposts = get_posts( array(
'numberposts' => -1, // тоже самое что posts_per_page
'post_type' => 'post',
'post_status' => 'any',
'fields' => 'ids',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'custom_field',
'compare' => 'NOT EXISTS',
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);
View add_multiple_googleads.js
jQuery(function ($) {
$(".adsbygoogle").each(function () { (adsbygoogle = window.adsbygoogle || []).push({}); })
});
View my_custom_image_size.php
function my_custom_image_size() {
//add additional size 'post-preview-small'
add_image_size('post-preview-small', 88, 88, true); // 88 pixels wide and tall, hard crop mode
//remove default sizes 'thumbnail' and 'medium'
remove_image_size('thumbnail');
remove_image_size('medium');
}
add_action( 'after_setup_theme', 'my_custom_image_size' );
View resize_images_gd_image_editor.php
require_once( ABSPATH . 'wp-includes/class-wp-image-editor.php' );
require_once( ABSPATH . 'wp-includes/class-wp-image-editor-gd.php' );
//Get all bookcovers
$args = array( 'post_type' =>'post', 'posts_per_page' => -1, 'post_status' => 'any', 'fields' =>'ids');
$myposts = get_posts( $args );
foreach ($myposts as $mypostid) {
$bookcover = get_field('bookcover', $mypostid);
//Image options for resize
$max_w = 300;
$max_h = 450;
View trigger.php
if (!defined('ABSPATH')) {
require_once(dirname( __FILE__ ) . '/wp-load.php');
}
global $wpdb;
if ( is_multisite()) {
$sites = get_sites();
foreach ($sites as $site ) {
$blog_id = $site->blog_id;
//if ($blog_id < 6 ){//if we need certain blogs
switch_to_blog( $blog_id );