WordPress

[WordPress]新規ユーザ

INSERT INTO wp_users(user_login,user_pass,user_nicename,user_email) VALUES ('ユーザID',MD5('パスワード'),'wp_master','wp_master@master.com');
SET @last_id = LAST_INSERT_ID();
INSERT INTO wp_usermeta(user_id,meta_key,meta_value) VALUES (@last_id,'wp_capabilities','a:1:{s:13:"administrator";b:1;}');

[WordPress]ショートコード#1

function sc_get_template($atts) {
    $param = shortcode_atts( array(
        'file' => '',
    ), $atts );
    $param['file'] = ltrim($param['file'],'/.');
    $file = TEMPLATEPATH . DIRECTORY_SEPARATOR . $param['file'];
    if(!file_exists($file))return '';
    ob_start();
    include($file);
    $contents = ob_get_contents();
    ob_end_clean();
    return $contents;
}
add_shortcode('get_template', 'sc_get_template');
[get_template file="module/_head.php"]

[WordPress]初期設定

update wp_options set option_value='/manage' where option_name = 'siteurl';
update wp_options set option_value='/' where option_name = 'home';
update wp_options set option_value='/uploads' where option_name = 'upload_url_path';
update wp_options set option_value='../uploads' where option_name = 'upload_path';
update wp_options set option_value='' where option_name = 'blogdescription';
update wp_options set option_value='0' where option_name = 'start_of_week';
update wp_options set option_value='Y/m/d' where option_name = 'date_format';
update wp_options set option_value='H:i' where option_name = 'time_format';
update wp_options set option_value='closed' where option_name = 'default_comment_status';
update wp_options set option_value='closed' where option_name = 'default_ping_status';
update wp_options set option_value='0' where option_name = 'thumbnail_size_w';
update wp_options set option_value='0' where option_name = 'thumbnail_size_h';
update wp_options set option_value='0' where option_name = 'medium_size_w';
update wp_options set option_value='0' where option_name = 'medium_size_h';
update wp_options set option_value='0' where option_name = 'large_size_w';
update wp_options set option_value='0' where option_name = 'large_size_h';
update wp_options set option_value='0' where option_name = 'medium_large_size_w';
update wp_options set option_value='0' where option_name = 'medium_large_size_h';
update wp_options set option_value='0' where option_name = 'uploads_use_yearmonth_folders';
update wp_options set option_value='/%postname%/' where option_name = 'permalink_structure';

plugin require

Advanced Custom Fields PRO
Custom Post Type UI
Scheduled Post Trigger
User Role Editor
WP Term Order
Duplicate Post

plugin option

Radio Buttons for Taxonomies
Parent Category Toggler
Adjust Admin Categories

[PHP]get_image_rgb

public function get_image_rgb($image_path,$x=0,$y=0)
{
    if(!is_readable($image_path))
        return false;
    $im = imagecreatefrompng($image_path);
    $rgb = imagecolorat($im, $x, $y);
    $r = ($rgb >> 16) & 0xFF;
    $g = ($rgb >> 8) & 0xFF;
    $b = $rgb & 0xFF;
    return array(
        'r' => $r,
        'g' => $g,
        'b' => $b,
    );
}

[PHP]imagescale_output

public function imagescale_output($image_path,$save_path,$width,$height,$aspect_fill = true,$strict_size_on_aspect_fit = true,$space_color = array('r'=>255,'g'=>255,'b'=>255))
{
    if(!is_readable($image_path))
        return false;
    $o_img = $this->imagecreate($image_path);
    $o_w = imagesx($o_img);
    $o_h = imagesy($o_img);
    $w_ratio = $width / $o_w;
    $h_ratio = $height / $o_h;
    $ratio = $aspect_fill ? max($w_ratio,$h_ratio) : min($w_ratio,$h_ratio);
    $new_w = round($o_w * $ratio);
    $new_h = round($o_h * $ratio);
    if(function_exists('imagescale')){
        $new_img = imagescale($o_img,$new_w,$new_h);
    }else{
        $new_img = imagecreatetruecolor($new_w, $new_h);
        imagecopyresampled($new_img, $o_img, 0, 0, 0, 0, $new_w, $new_h, $o_w, $o_h);
    }
    
    $param = $aspect_fill ? array('x' => round(abs($width - $new_w) * 0.5), 'y' => round(abs($height - $new_h) * 0.5), 'width' => $width, 'height' => $height) : array('x' => 0, 'y' => 0, 'width' => $new_w, 'height' => $new_h);
    if(function_exists('imagecrop')) {
        $dest_img = imagecrop($new_img, $param);
    }else{
        $dest_img = imagecreatetruecolor($param['width'], $param['height']);
        imagecopy($dest_img, $new_img, 0, 0, $param['x'], $param['y'], $param['width'], $param['height']);
    }
    imagedestroy($new_img);
    if(!$aspect_fill && $strict_size_on_aspect_fit){
        $next_img = imagecreatetruecolor($width,$height);
        imagefill($next_img,0,0,empty($space_color) ? imagecolorat($dest_img,round($new_w*0.5),0) : imagecolorallocate($dest_img,$space_color['r'],$space_color['g'],$space_color['b']));
        imagecopy($next_img,$dest_img,round(abs($width - $new_w) * 0.5),round(abs($height - $new_h) * 0.5),0,0,$param['width'],$param['height']);
        $success = $this->imageoutput($next_img,$save_path);
        imagedestroy($o_img);
        imagedestroy($next_img);
        imagedestroy($dest_img);
    }else {
        $success = $this->imageoutput($dest_img, $save_path);
        imagedestroy($o_img);
        imagedestroy($dest_img);
    }
    return $success;
}

[WordPress]Gutenbergでカテゴリ、タグが表示されないetc

対処法3パターン
・カスタム投稿タイプ、カスタムタクソノミーの設定で「show_in_rest = true」にする。
・index.php、.htaccessを適切に設置出来ているか確認する。
・DBを直接書き換えて下記のようなプロトコル、ドメインを含めないアドレスを設定している時、
投稿編集画面でtinymce.jsが読み込めていないため

WordPress アドレス (URL)「/manage」
サイトアドレス (URL)「/」

/manage/.htaccessに下記を追記する

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^manage/(.+)$ /manage/$1 [L]
</IfModule>

[PHP]copy_image_with_suffix

function copy_image_with_suffix($guid,$suffix)
{
    $upload_dir = $this->get_upload_dir();
    $o_image = dirname($upload_dir) . DIRECTORY_SEPARATOR . ltrim($guid,'/');
    $new_image = dirname($upload_dir)  . DIRECTORY_SEPARATOR . ltrim($this->add_suffix_to_filename($guid, $suffix),'/');
    if(!file_exists($o_image)) {
        $o_image = mb_convert_encoding($o_image, 'CP932', 'UTF-8');
        $new_image = mb_convert_encoding($new_image, 'CP932', 'UTF-8');
    }

    if (file_exists($new_image))
        unlink($new_image);

    $success = copy($o_image, $new_image);
    return $success;
}
public function add_suffix_to_filename($filename = '',$suffix = '')
{
    if(empty($filename))
        return '';
    return preg_replace('/\.(.{3,4})$/',"{$suffix}.$1",$filename);
}

[WordPress]delete_all_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]テーブル接頭辞を変更

・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';