Skip to content

Instantly share code, notes, and snippets.

View annalinneajohansson's full-sized avatar
🐱

Anna annalinneajohansson

🐱
  • Frontwalker Group AB
  • Sweden
  • 22:53 (UTC +02:00)
View GitHub Profile
@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 / 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 / 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 / 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 / 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 ),
<?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 / hip_exclude_from_wp_list_pages.php
Created April 4, 2014 08:10
Exclude pages with meta value 'hide_page_in_menus' set to 1 from wp_list_pages
add_filter( 'get_pages', 'hip_exclude_from_wp_list_pages', 10, 2 );
/**
* Exclude pages with meta value 'hide_page_in_menus' set to 1 from wp_list_pages
* @param array $pages
* @param array $args
* @return array $pages
*/
function hip_exclude_from_wp_list_pages( $pages, $args ) {
// Check for 'walker' key to see if this is being used by wp_list_pages()
<?php
$args = array(
'post_type' => 'product',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE'
),
@annalinneajohansson
annalinneajohansson / hip_mc_move_settings_box.php
Created May 11, 2014 03:49
Move advanced meta boxes to below after the title
<?php
/**
* Move advanced meta boxes to below after the title
*/
function hip_mc_move_settings_box() {
# Get the globals:
global $post, $wp_meta_boxes;
$posttype = "post";
// Autosave, do nothing
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// AJAX
if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
return;
// Check user permissions
if ( ! current_user_can( 'edit_post', $post_id ) )
return;
// Return if it's a post revision