Skip to content

Instantly share code, notes, and snippets.

@wokamoto
wokamoto / nginx_flush_cache.php
Created October 30, 2012 04:03
Nginx Cache Controller で複数サーバのキャッシュ消す奴
<?php
function nginx_flush_cache( $url ) {
global $nginx_servers;
static $urls = array();
if ( !$url )
return;
if ( isset($urls[md5($url)]) )
return;
$urls[md5($url)] = $url;
$log_file = '/tmp/nginx_flush_cache.log';
@wokamoto
wokamoto / example.php
Created November 15, 2012 11:06
class-wp_post_helper の使い方
<?php
require_once('/path/to/wordpress/wp-load.php');
require_once('class-wp_post_helper.php');
// 初期化
$post = new wp_post_helper(array(
'post_name' => 'slug' , // スラッグ
'post_author' => 1 , // 投稿者のID
'post_date' => '2012/11/15 20:00:00' , // 投稿時刻
'post_type' => 'posts' , // 投稿タイプ(カスタム投稿タイプも指定できるよ)
@wokamoto
wokamoto / thumbnail_noimage.php
Created November 27, 2012 10:28
[WordPress] サムネイルが無い場合に、メディアライブラリに登録されている特定の画像を出す奴
<?php
function thumbnail_noimage($html, $post_id, $post_thumbnail_id, $size, $attr) {
if (!empty($html))
return $html;
$post_thumbnail_id = 1234; // default thumbnail image id
$html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
return $html;
}
@wokamoto
wokamoto / replace-post_type.sql
Created November 28, 2012 04:47
[WordPress] 特定のカテゴリに属する投稿を任意のカスタム投稿タイプに変換する SQL
-- カテゴリー 4 の投稿を、カスタム投稿タイプ photo に
update wp_posts
set post_type = 'photo'
where post_type = 'post'
and exists
(select object_id
from wp_term_taxonomy as t
inner join wp_relationships as r on t.term_taxonomy_id = r.term_taxonomy_id
where t.term_id = 4
and r.object_id = wp_posts.ID);
@wokamoto
wokamoto / attachment_link_convert.php
Created November 28, 2012 06:20
[WordPress] 画像のリンク先を強制的に添付ファイル投稿URLに変更する奴
@wokamoto
wokamoto / replace-tableprefix.sh
Created December 3, 2012 07:03
[WordPress] ダンプファイルのテーブルプレフィックスを変更するシェルスクリプト
#!/bin/sh
sed -e 's/\(EXISTS\|TABLES\?\|INSERT INTO\) `wp_/\1 `wp_other_/ig' dump.sql > dump_new.sql
@wokamoto
wokamoto / wp-delete-revision.sql
Created December 5, 2012 02:16
[WordPress] revision とか auto-draft とか trash な post を全部削除する SQL
delete from wp_posts
where post_status in ('auto-draft', 'trash')
or post_type = 'revision';
delete from wp_postmeta
where not exists (select 'x' from wp_posts where wp_posts.ID = wp_postmeta.post_id);
@wokamoto
wokamoto / wp-add-restrict-manage-posts.php
Last active October 13, 2015 17:47
[WordPress] ダッシュボードの投稿一覧にカスタムフィールドの値での絞り込み検索を追加する
<?php
add_filter('query_vars', function($vars){
array_push($vars, 'my_meta_key');
return $vars;
});
add_action('restrict_manage_posts', function(){
printf(
'<input type="text" id="%1$s" name="%1$s" value="%2$s" />',
'my_meta_key',
<?php
function wp_embed_handler_image( $matches, $attr, $url, $rawattr ) {
$embed = sprintf(
'<img src="%1$s" alt="" />',
esc_attr($matches[0])
);
return apply_filters( 'embed_image', $embed, $matches, $attr, $url, $rawattr );
}
wp_embed_register_handler( 'image', '/^https?(:\/\/[-_\.!~*\'()a-zA-Z0-9;\/:\@=+\$,%#]+)(\.jpe?g|\.gif|\.png)$/', 'wp_embed_handler_image', 10, 4 );
@wokamoto
wokamoto / wp-my_attachment_title.php
Created December 13, 2012 08:31
[WordPress] 添付画像表示ページのタイトルを親投稿のタイトルに合わせる。
<?php
function my_attachment_title($title, $id) {
if ( is_admin() )
return $title;
$post = is_object($id)
? $id
: $post = get_post($id);
if ( !$post || $post->post_type !== 'attachment' )