[WordPress]テーブル接頭辞を変更

・wp-config.php

$table_prefix = 'dev_wp_';

$table_prefix = 'wp_';

へ変更

・下記を実行

ALTER TABLE dev_wp_commentmeta RENAME TO wp_commentmeta;
ALTER TABLE dev_wp_comments RENAME TO wp_comments;
ALTER TABLE dev_wp_links RENAME TO wp_links;
ALTER TABLE dev_wp_options RENAME TO wp_options;
ALTER TABLE dev_wp_postmeta RENAME TO wp_postmeta;
ALTER TABLE dev_wp_posts RENAME TO wp_posts;
ALTER TABLE dev_wp_term_relationships RENAME TO wp_term_relationships;
ALTER TABLE dev_wp_term_taxonomy RENAME TO wp_term_taxonomy;
ALTER TABLE dev_wp_usermeta RENAME TO wp_usermeta;
ALTER TABLE dev_wp_users RENAME TO wp_users;
ALTER TABLE dev_wp_terms RENAME TO wp_terms;
UPDATE wp_options SET option_name = 'wp_user_roles' WHERE option_name = 'dev_wp_user_roles';
UPDATE wp_usermeta SET meta_key = 'wp_capabilities' WHERE meta_key = 'dev_wp_capabilities';
UPDATE wp_usermeta SET meta_key = 'wp_user_level' WHERE meta_key = 'dev_wp_user_level';

[WordPress]Advanced Custom Fields: DB Field

Advanced Custom Fields: DB Field をリリースしました。
https://wordpress.org/plugins/acf-db-field/
https://github.com/ktyrnet/acf-db

ACFのアドオンなのでACFがインストールされている必要があります。
・任意のDB,テーブル,カラムを選択させるカスタムフィールドを追加出来ます。
・現在はWordPressと同じDBアカウントのDBにしか対応していません。

カスタムフィールド設定画面

投稿画面

[WordPress]EC2で現地時間が+9時間になる

AWS EC2上のWordPressにおいて
設定 > 一般 > タイムゾーン : 東京 の時、下記の症状が出る。
・世界標準時 (UTC) は “東京の時間” です。 現地時間は “東京の時間+9時間” です。
・記事を新規作成して「すぐに公開する」で公開しても、「予約済み」になる。

対処法
・SSHでサーバへ接続
・コマンド「sudo yum reinstall tzdata」
・コマンド「sudo service httpd restart」

[WordPress]タクソノミー管理画面にID列追加

タクソノミー:xxxxx

function add_columns_to_edit_category( $columns ){
    $new_columns = array();
    foreach ( $columns as $column_name => $column_display_name ) {
        if( $column_name == 'name' ) {
            $new_columns['ID'] = 'ID';
            echo '<style>#ID{width:70px;}</style>';
        }
        $new_columns[ $column_name ] = $column_display_name;
    }
    return $new_columns;
}
add_filter( "manage_edit-xxxxx_columns", 'add_columns_to_edit_category', 10);

function output_column_value_category( $value, $column_name, $term_id )
{
    $t = get_term($term_id);
    switch ($column_name){
        case 'ID':
            echo $t->term_id;
            break;
        default:
            break;
    }
}
add_action( "manage_xxxxx_custom_column", 'output_column_value_category', 10, 3);

[WordPress]TransientAPI#3

    public function delete_transient($transient)
    {
        delete_transient($transient);
    }
    public function delete_all_transient()
    {
        global $wpdb;
        $sql = "DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE ('_transient_%');";
        $result = $wpdb->get_results($sql);
        $sql = "DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE ('_site_transient_%');";
        $result = $wpdb->get_results($sql);
    }

[WordPress]TransientAPI#2

public function get_transient_by_query($query)
{
    if(!$this->use_transient)
        return false;
    return $this->get_transient(md5($query));
}
public function get_transient($transient)
{
    if(!$this->use_transient)
        return false;
    return get_transient($transient);
}

[WordPress]TransientAPI#1

public function set_transient_by_query($query, $value, $expiration = -1)
{
    if(!$this->use_transient)
        return;
    $this->set_transient(md5($query),$value,$expiration);
}
public function set_transient($transient, $value, $expiration = -1)
{
    if(!$this->use_transient)
        return;
    if($expiration < 0)
        $expiration = $this->transient_expiration;
    set_transient($transient, $value, $expiration );
}

[WordPress]カスタム投稿タイプxxxx一覧にカラム追加

function add_columns_to_xxxxx( $columns )
{
    $new_columns = array();
    foreach ( $columns as $column_name => $column_display_name ) {
        if( $column_name == 'title' ) {
            $new_columns['ID'] = 'ID';
        }
        $new_columns[ $column_name ] = $column_display_name;
    }
    return $new_columns;
}
add_filter('manage_xxxxx_posts_columns' , 'add_columns_to_xxxxx');

function output_column_value_xxxxx( $column, $post_id )
{
    switch ( $column ) {
        case 'ID':
            echo $post_id;
            break;
    }
}
add_action( 'manage_xxxxx_posts_custom_column' , 'output_column_value_xxxxx', 10, 2 );

[WordPress]IDからACFProのカスタムフィールドの値を取得

$metas = $this->get_metas_by_post_id($ids, $cf_names);

public function get_metas_by_post_id($post_id, $cf_names = null)
{
    global $wpdb;
    $sql = array();
    $sql[] = "SELECT";

    $fields = array();
    $fields[] = "pm.post_id AS ID";
    $fields[] = "pm.meta_key";
    $fields[] = "pm.meta_value";
    $fields[] = "(CASE WHEN p2.guid IS NULL THEN '' ELSE p2.guid END) AS guid";
    $sql[] = implode(',', $fields);

    $sql[] = "FROM $wpdb->postmeta AS pm";

    $sql[] = "LEFT JOIN $wpdb->posts AS p ON p.ID = pm.post_id";
    $sql[] = "LEFT JOIN $wpdb->posts AS p2 ON pm.meta_value = p2.ID AND p2.post_type = 'attachment'";
    $sql[] = "WHERE pm.post_id " . $this->_get_sql_for_in($post_id, true);
    if (!empty($cf_names))
        $sql[] = "AND pm.meta_key " . $this->_get_sql_for_in($cf_names);
    $sql[] = "ORDER BY pm.post_id ASC";
    
    $result = $wpdb->get_results(implode(' ', $sql));
    $this->unserialize_by_keys($result);

    return $result;
}
public function unserialize_by_keys(&$post, $keys = null)
{
    if (is_array($post)) {
        foreach ($post AS $elm)
            $this->unserialize_by_keys($elm, $keys);
        return;
    }
    $target = !empty($keys) ? $keys : $post;
    foreach ($target as $key => $value)
        $post->$key = maybe_unserialize($post->$key);
}

[htaccess]BASIC認証とIP一部IPのみ制限

/var/www/xxxxx/public_htmlはBASIC認証又はIPが192.168.xxx.xxxならば閲覧可能
/var/www/xxxxx/public_html/secureはIPが192.168.xxx.xxxならば閲覧可能。それ以外のIPは403

/var/www/xxxxx/public_html/.htaccess

AuthUserFile /var/www/xxxxx/public_html/.htpasswd
AuthGroupFile /dev/null
AuthName "Input ID and Password."
AuthType Basic
require valid-user
SetEnvIf Request_URI "^/secure/.*$" secure_dir
Order Deny,Allow
Deny from all
allow from env=secure_dir
Allow from 192.168.xxx.xxx
<Files ~ "^\.(htaccess|htpasswd)$">
deny from all
</Files>
Satisfy Any

/var/www/xxxxx/public_html/secure/.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !^192.168.xxx.xxx
RewriteRule ^(.*)$ - [R=403,L]
</IfModule>