Skip to content

Instantly share code, notes, and snippets.

View annalinneajohansson's full-sized avatar
🐱

Anna annalinneajohansson

🐱
  • Frontwalker Group AB
  • Sweden
  • 20:13 (UTC +02:00)
View GitHub Profile
@annalinneajohansson
annalinneajohansson / detect_shortcode.php
Last active December 20, 2015 09:59
Detect if a specific shortcode is being used in post_content. Returns a boolean.
<?php
function hip_detect_shortcode( $shortcode = false ) {
$shortcode_found = false;
if( $shortcode && is_singular() ) {
global $post;
$pattern = get_shortcode_regex();
@annalinneajohansson
annalinneajohansson / hip_is_mobile.php
Created August 27, 2013 15:02
Tweaked wp_is_mobile that does a separate check for iPad
<?php
function hip_is_mobile() {
if( strpos( $_SERVER['HTTP_USER_AGENT'], 'iPad' ) ) {
$cookie = $_COOKIE['is_mobile'];
if( $cookie == 'yes' )
return true;
else
return false;
} else {
@annalinneajohansson
annalinneajohansson / sanitize-filenames.php
Last active December 21, 2015 23:59
Replace and/or remove accents and other special characters in filenames on upload
<?php
/*
Plugin Name: Extended sanitize filename
Plugin URI:
Description: Replace and/or remove accents and other special characters in filenames on upload
Version: 1
Author: Hippies
Author URI: http://hippies.se
*/
add_filter( 'sanitize_file_name', 'extended_sanitize_file_name', 10, 2 );
@annalinneajohansson
annalinneajohansson / check_for_thumb.php
Last active December 23, 2015 03:58
Check for featured image on post save
<?php
function check_for_thumb( $post ){
global $post;
$post_type = get_post_type( $post->ID );
$post_thumb = get_post_meta( $post->ID, '_thumbnail_id', true );
if( $post_type === 'bildspel' && isset( $_GET['post'] ) && $_GET['action'] === 'edit' && empty( $post_thumb ) ) {
echo "<div class='error'><p>Posten saknar utvald bild</p></div>";
}
@annalinneajohansson
annalinneajohansson / add_generic_image_class.php
Created September 19, 2013 04:18
GENERIC CLASS ON WORDPRESS POST IMAGES
<?php
add_filter( 'get_image_tag_class', 'add_generic_image_class');
function add_generic_image_class( $class ) {
$class .= ' my-image-class';
return $class;
}
<?php
function list_hooked_functions($tag=false){
global $wp_filter;
if ($tag) {
$hook[$tag]=$wp_filter[$tag];
if (!is_array($hook[$tag])) {
trigger_error("Nothing found for '$tag' hook", E_USER_WARNING);
return;
}
}
@annalinneajohansson
annalinneajohansson / hip_enumerate_nav_menu_items.php
Last active December 25, 2015 06:59
Enumerate wp nav menu items. Inomplete ancestry detection; only detects parent <-> child, so 1 level.
<?php
add_filter( 'wp_get_nav_menu_items', 'hip_enumerate_nav_menu_items', 10, 3 );
function hip_enumerate_nav_menu_items( $items, $menu, $args ){
if( !is_admin() ) :
$i = 1;
foreach ( $items as $item ) {
if( $item->menu_item_parent == 0 ) {
$child_i = 0;
$item->classes[] = "parent-menu-item-$i";
if( $i == 1 ) $item->classes[] = "parent-menu-item-first";
@annalinneajohansson
annalinneajohansson / add_meta_box.php
Created October 24, 2013 07:28
Example that adds a custom meta box to the post and page editing screens. From http://codex.wordpress.org/Function_Reference/add_meta_box
<?php
/**
* Adds a box to the main column on the Post and Page edit screens.
*/
function myplugin_add_custom_box() {
$screens = array( 'post', 'page' );
foreach ( $screens as $screen ) {
@annalinneajohansson
annalinneajohansson / hip-notices.js
Last active December 29, 2015 21:19
A collection of helpful notices shown in WordPress admin.
jQuery(document).ready(function($){
var status = notices.post_status,
savePostBtn = $("#save-post"),
publishPostBtn = $("#publishing-action #publish");
if( status != "publish" && status != 0 ) {
publishPostBtn.removeClass("button-primary");
savePostBtn.addClass("button-primary");
}
});
@annalinneajohansson
annalinneajohansson / hip_attachments_noindex.php
Last active December 30, 2015 09:29
Do not index attachment pages in WordPress
<?php
/*
Plugin Name: Hippies attachments noindex
Plugin URI:
Description: Do not index attachment pages and set default image link type to "none" on plugin activation.
Version: 1
Author: Hippies
Author URI: http://hippies.se/
*/
add_action( 'wp_head', 'hip_attachments_noindex_wp_head' );