Skip to content

Instantly share code, notes, and snippets.

View annalinneajohansson's full-sized avatar
🐱

Anna annalinneajohansson

🐱
  • Frontwalker Group AB
  • Sweden
  • 21:57 (UTC +02:00)
View GitHub Profile
<?php
function hip_toolbar_items() {
global $wp_admin_bar;
$args = array(
'id' => 'id',
'title' => __( 'Title', 'textdomain' ),
'href' => admin_url(),
);
$wp_admin_bar->add_menu( $args );
}
@annalinneajohansson
annalinneajohansson / attachment_fields_to_edit.php
Last active August 29, 2015 13:56
Add custom fields to media upload + append field values to attachment <title>
<?php
add_filter( 'attachment_fields_to_edit', 'hip_edit_media_custom_field', 11, 2 );
add_filter( 'attachment_fields_to_save', 'hip_save_media_custom_field', 11, 2 );
function hip_edit_media_custom_field( $form_fields, $post ) {
$form_fields['image_source'] = array(
'label' => __( 'Bildkälla', 'ds' ),
'input' => 'text',
'value' => get_post_meta( $post->ID, 'image_source', true ),
@annalinneajohansson
annalinneajohansson / manage_{taxonomy-slug}_custom_column.php
Created February 16, 2014 07:09
Custom columns on custom taxonomy terms in admin
<?php
/*
* Custom columns on edit terms screens i admin
* */
add_filter( 'manage_edit-{taxonomy-slug}_columns', 'hip_{taxonomy-slug}_columns_head' );
add_filter( 'manage_{taxonomy-slug}_custom_column', 'hip_{taxonomy-slug}_columns_content_taxonomy', 10, 3 );
function hip_{taxonomy-slug}_columns_head( $columns ) {
unset( $columns['posts'] ); // unset the posts column
unset( $columns['slug'] ); // unset the slug column
@annalinneajohansson
annalinneajohansson / template_include.php
Created February 11, 2014 12:40
WordPress: Add theme template from plugin if it doesn't exist in the theme folder.
<?php
add_filter( 'template_include', 'my_prefix_template' );
function my_prefix_template( $template ) {
$template_filename = "foobar.php";
if ( !file_exists( get_stylesheet_directory() . "/" . $template_filename ) ) {
$template = plugin_dir_path( __FILE__ ) . '/' . $template_filename;
}
@annalinneajohansson
annalinneajohansson / new_gist_file_0
Last active August 29, 2015 13:56
SQL SELECT to match a searchterm in wp_terms but only from a specific taxonomy
SELECT * FROM $termstable AS terms
JOIN ".$wpdb->prefix."term_taxonomy AS termtax ON terms.term_id = termtax.term_id
WHERE termtax.taxonomy = '$taxonomy'
AND terms.name LIKE '%$searchterm%' COLLATE utf8_swedish_ci
ORDER BY termtax.count DESC
@annalinneajohansson
annalinneajohansson / new_gist_file_0.php
Created February 7, 2014 15:51
Download and attach an external file. Skip if an attachment with that name already exists.
<?php
$url = "url to file";
$tmp = download_url( $url );
/* Does a file with this name already exist in the media library? */
$filename = remove_accents( preg_replace("/\\.[^.\\s]{3,4}$/", "", basename( $url ) ) );
global $wpdb;
$poststable = $wpdb->prefix . "posts";
$filename_exists = $wpdb->get_col( "SELECT post_name FROM $poststable WHERE post_name LIKE '$filename' AND post_type = 'attachment'" );
@annalinneajohansson
annalinneajohansson / hip_post_gallery.php
Created December 19, 2013 06:02
Make WP native gallery always link directly to the file, not the attachment page. Also removes that irritating grey border css...
<?php
add_filter( 'post_gallery', 'hip_post_gallery', 10, 2 );
function hip_post_gallery( $output, $attr) {
global $post, $wp_locale;
static $instance = 0;
$instance++;
// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
if ( isset( $attr['orderby'] ) ) {
@annalinneajohansson
annalinneajohansson / hip_has_shortcode.php
Created December 6, 2013 18:18
Allow array check for shortcode (based on has_shortcode)
<?php
function hip_has_shortcode( $content, $tags ) {
if( is_array( $tags ) ) {
foreach ( $tags as $tag ) {
preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER );
if ( empty( $matches ) )
return false;
foreach ( $matches as $shortcode ) {
if ( $tag === $shortcode[2] )
@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' );
@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");
}
});