投稿者: admin Page 26 of 47

WordPressのサブディレクトリへの移動方法 /wp/などへ

いつも忘れてるのでメモ

1.管理画面で設定変更 ([設定] > [一般設定] )

(この時点で管理画面が表示できなくなるけど落ち着く。)

2.ファイルの移動

WordPressのファイル群を/wp/以下などに一式移動します。

3./index.phpの最後の方の wp-blog-header.phpへのパスの記述を変更

CakePHP2 Schema,Migrations関連メモ

個人的にわすれるので。

スキーマ作成(※要-fオプション ModelクラスがなくてもSchemaへ反映してくれる)

$ ./Console/cake schema generate -f

Migration作成

$ ./Console/cake Migrations.migration run all -p Migrations

‘schema_migrations’テーブルができる。

変更を加える

テーブル追加など

Migrations実行

$ ./Console/cake Migrations.migration generate -f

実行すると

Cake Migration Shell
---------------------------------------------------------------
Do you want to compare the schema.php file to the database? (y/n)
[y] >

schema.phpと比較するかな? > y

---------------------------------------------------------------
Comparing schema.php to the database...
Do you want to preview the file before generation? (y/n)
[y] >

previewする? y

Please enter the descriptive name of the migration to generate:  
> add_newtable_and_newfields
Generating Migration...

説明する名前つけてなので変更した内容を記述 > add_newtable_and_newfields

Do you want to update the schema.php file? (y/n)
[y] >

スキーマupdate ? yes.

Welcome to CakePHP v2.6.12 Console
---------------------------------------------------------------
App : app
Path: /Users/taka/htdocs/bizmatchocvb/html/businessmatching/app/
---------------------------------------------------------------
Cake Schema Shell
---------------------------------------------------------------
Generating Schema...
Schema file exists.
 [O]verwrite
 [S]napshot
 [Q]uit
Would you like to do? (o/s/q)
[s] > o
Schema file: schema.php generated

上書き? yes で完了。

次回以降は、

$  ./Console/cake Migrations.migration generate -f

で良さそう。

変更内容の適用方法

$ ./Console/cake Migrations.migration run

とすると

Cake Migration Shell
---------------------------------------------------------------
Available migrations:
  [xxxxxxxxxxx] xxxxxxxxxxx_add_newtable_and_newfields
        not applied
---------------------------------------------------------------
Please choose which version you want to migrate to. [q]uit or [c]lean.  
> xxxxxxxxxxx

バージョンを聞いてくる。ので該当の xxxxxxxxxxx を入力して

---------------------------------------------------------------
Running migrations:
  [xxxxxxxxxxx] xxxxxxxxxxx_add_newtable_and_newfields
      > Creating table "bookmarks".
      > Creating table "likes".
      > Creating table "messages".
      > Creating table "notices".
      > Creating table "projects".
      > Adding field "newstype_id" to table "news".

---------------------------------------------------------------
All migrations have completed.

マスター系のテーブルに追加がある場合。

/Config/Migrations/以下のMigrationのプログラムに追記
‘up’した時にどうするかを追記。(‘down’のときも必要)

/**
 * After migration callback
 *
 * @param string $direction Direction of migration process (up or down)
 * @return bool Should process continue
 */
    public function after($direction) {
        if ($direction === 'up') {
            $Group = $this->generateModel('Group');
            $Group->save(array(
                'Group'=>array('name'=>'海外エージェント','key'=>'agent')
            ));
        }
        return true;
    }
}

MAMPでPHPからRedisを簡単につかう。

とりいそぎ機能だけ試してみたい、アプリケーション側へ集中したいということでMAMPでPHPからRedisを簡単につかう方法。

こちらからコンパイル済のredis.soを取得して
(使いたいPHPのバージョン ※マイクロバージョンが違う環境でも動作できました。)

panxianhai/php-redis-mamp
https://github.com/panxianhai/php-redis-mamp

MAMPアプリケーションの以下のディレクトリに配置

/Applications/MAMP/bin/php/php5.x.x/lib/php/extensions/no-debug-non-zts-200xxxxx

メニューの [File] / [Edit Template] / [PHP] [PHP(自分のバージョン) php.ini を選択し以下を追記(最後でもどこでも)

extension=redis.so

MAMPを再起動,phpinfo()してみる。(redisが出てたらOK)

brewでredisをインストール

$ brew install redis

redisを起動

$ redis-server

サンプル実行

<?php
$redis = new Redis();
$redis->connect("127.0.0.1",6379);
$redis->set("name","yamaaaaa");
echo $redis->get("name");

ブラウザでアクセスして出力確認

yamaaaaa

WordPressの投稿にカスタムフィールド(wp-postmeta)の値を手軽にJOINして取得する。&JSONへ変換

WordPressで投稿のデータにカスタムフィールドを加えて取得する。(全文)

<?php
$posts_join_keys = array();
$posts_join_prefix = 'tmp_join_table_name_';
add_filter('posts_join', 'posts_more_join' );
add_filter('posts_fields','posts_more_fields');
function posts_more_join( $join )
{
 global $posts_join_keys,$posts_join_prefix;
 foreach($posts_join_keys as $key => $k){
 $alias = $posts_join_prefix.$k;
 $join .= sprintf(" INNER JOIN wp_postmeta as %s ON ( wp_posts.ID = %s.post_id AND %s.meta_key = '%s' )",$alias,$alias,$alias,$k);
 }
 return $join;
}
function posts_more_fields($fields){
 global $posts_join_keys,$posts_join_prefix;
 foreach($posts_join_keys as $key => $k){
 $alias = $posts_join_prefix.$k;
 $fields .= sprintf(',%s.meta_value as %s',$alias,$k);
 }
 return $fields;
}
$posts_join_keys[] = 'locate';
$posts_join_keys[] = 'description';
$args = array(
 'posts_per_page' => -1,
 'fields'=> 'wp_posts.ID,wp_posts.post_title',
 'meta_query' => array(array(
 'key' => 'locate',
 'value' => '',
 'compare' => '!='
 ))
);
$query = new WP_Query( $args );
$posts = $query->get_posts();
wp_reset_postdata();
$json = '{data:'.json_encode($query->get_posts()).'}';
?>

上のプログラムの $posts_join_keysに取得したいカスタムフィールドの’meta_key’を追加(push)していけばその分取得できます。

$posts_join_keys[] = 'locate';
$posts_join_keys[] = 'description';

WordPressのシンプルなページネーション(ページ番号)リンク

プラグインなどをつかわずにできるシンプルなページネーション(ページ番号)のリンクのテンプレートです。

WordPressテンプレート

<div class="pagenation">
    <ul>
        <?php
        $page_current_num = (!empty($paged))? $paged:1;
        $page_last_num = $wp_query->max_num_pages;
        if($page_current_num > 1){
            echo sprintf('<li class="prev"><a href="%s">%s</a></li>',get_pagenum_link($page_current_num-1),'PREV');
        }
        $page_start_num = $page_current_num - NUM_PAGENATION_BEFORE_AFTER;
        if($page_start_num < 1) $page_start_num = 1;
        $page_end_num = $page_current_num + NUM_PAGENATION_BEFORE_AFTER;
        if($page_end_num > $page_last_num) $page_end_num = $page_last_num;
        for($i = $page_start_num;$i<=$page_end_num;$i++){
            if($page_current_num==$i){
                echo sprintf('<li class="active">%s</li>',$i);
            }else{
                echo sprintf('<li><a href="%s">%s</a></li>',get_pagenum_link($i),$i);
            }
        }
        if($page_current_num < $page_last_num){
            echo sprintf('<li class="next"><a href="%s">%s</a></li>',get_pagenum_link($page_current_num+1),'NEXT');
        }
        ?>
    </ul>
</div>

CSS(SASS)

//---------------------------------------------------pagination
.pagenation
  padding: 40px 0
  margin: auto
  display: table
  &:after
    clear: both
    content: "."
    display: block
    height: 0
    visibility: hidden
  ul
    &:after
      clear: both
      content: "."
      display: block
      height: 0
      visibility: hidden
    text-align: center
    margin: auto
  li
    float: left
    list-style: none outside none
    margin-left: 3px
    &:first-child
      margin-left: 0
    &.active
      background: #336699
      color: #FFF
      cursor: not-allowed
      padding: 10px 20px
    a
      background: #F2F2F2
      color: #336699
      display: block
      padding: 10px 20px
      text-decoration: none
      &:hover
        background-color: #A1AABA
        color: #FFF
        opacity: 0.8
        transition-duration: 500ms
        transition-property: all
        transition-timing-function: ease

WordPressでカスタムフィールドの値の抜粋を取得する

function get_sub_fields_excerpt_more($key,$limit=30){
    $str = get_field($key);
    $len = mb_strlen($str,ENCODING);
    if($len > NUM_EXCERPT_SLIDER){
        return mb_substr($str,0,NUM_EXCERPT_SLIDER,ENCODING).'...';
    }else{
        return $str;
    }
}

NUM_EXCERPT_SLIDER,ENCODINGは、文字数,文字コードの定数

WordPressで特定のカスタムフィールドが入力されている記事をランダムに取得

WordPressで特定のカスタムフィールドが入力されている記事をランダムに取得する方法です。WP_Query関数を利用

ACF(Advanced Custom Fields ver 4.4.11) ローカル管理画面などでGoogleMapが表示されない。

WordPressプラグイン ACFをローカル管理画面でGoogleMapが表示されない。

/wp-content/plugins/advanced-custom-fields/core/fields/google-map.php の293行目当たりの ‘key’にGoogle API Consoleで取得したAPIKey(制限なし)をいれて解決(とりあえず!
※制限なしだと危険なのであくまで一時的。
※プラグインのバージョンアップでソースが上書きされることへの対策が必要

##エラー発生の環境など

– ローカル
– ドメインは http://hogehoge.dev を /etc/hosts にてローカル(127.0.0.1)に向けている。
– ACFのバージョンは 4.4.11
– 一瞬ちらっと地図がでるがエラー表示に切り替わる

WordPressのqTranslateXプラグインでサブドメインモード(Pre Domain Mode)にすると、CSSのicomoonの@font-face読み込みでクロスドメイン誓約(Access-Control-Allow-Origin)でエラー

言語切替時にドメインが http://example.com  >> http://en.example.com などに切り替わったときにCSSでしている@font-faceの読み込みがエラーになってしまった。
フォント関係ファイルへのクロスドメイン制約を外して対応。

<FilesMatch "\.(ttf|eot|woff|svg)$">
 <IfModule mod_headers.c>
 Header set Access-Control-Allow-Origin "*"
 </IfModule>
</FilesMatch>

(ローカルにhostsで割り当てた際に発生、運用のサーバーで確認し追記よてい、取り急ぎメモ)

 

qTranslate-X(バージョン 3.4.6.8) のサブドメインでの切替設定でデフォルトの言語に戻れない現象

WordPressの多言語プラグイン ‘qTranslate-X’  (バージョン 3.4.6.8) で言語(デフォルト)へと切り替わらない現象があり解決できたのでメモ。

上手く言った方法 (URL変更モード : User Per-Domain mode )

screen-shot-2016-12-14-at-2-31-24-am

×上手できなかった方法 (URL変更モード : 事前ドメインモード )

bd8b40d8-5aef-41e0-95a3-afc790f98975

Page 26 of 47

Powered by WordPress & Theme by Anders Norén