LoginSignup
6
7

More than 5 years have passed since last update.

自分なりのWordPressの修正・改造方法のやり方

Last updated at Posted at 2016-11-02

WordPressを触り始めて2、3ヶ月経過して、いろんな人から「あんな機能いれてほしい」「こんな機能があったらいいな」と要望を頂き、それに沿う形で実装をしてきて自分なりのWordPressのいじり方が確立してきたので共有したいなと思います。

例:welcartの受注リスト詳細画面の管理者メモの下にフリーテキスト項目を追加

  1. 何かをしたい画面のHTML要素をデベロッパーツールなどで調べる。
  2. 要素のidもしくはclassをWordPress全体から調べる(管理者メモのinput要素のclass「order_memo」を検索)
  3. 該当要素を出力している関数を見つける(usces_order_memo_form_detail_top関数にて管理者メモ要素を出力している)
  4. 該当関数をさらに全検索にかける
  5. add_actionやadd_filterしている部分を見つける
default_filters.php
add_action('usces_action_order_edit_form_detail_top', 'usces_order_memo_form_detail_top', 10, 2 );
  1. functions.phpに機能追加したいことを実装する

functions.php
// フリーテキスト項目のinput要素を出力
add_action('usces_action_order_edit_form_detail_top', 'my_usces_action_order_edit_form_detail_top', 10, 2 );
function my_usces_action_order_edit_form_detail_top($data, $csod_meta) {
    global $usces;
    $order_freetext = '';
    if( !empty($data['ID']) ){
        $order_freetext = $usces->get_order_meta_value('order_freetext', $data['ID']);
    }

    $res = '<tr>
                <td class="label border">フリーテキスト</td>
                <td colspan="5" class="col1 border memo">
                    <textarea name="order_freetext" class="order_memo">'.esc_html($order_freetext).'</textarea>
                </td>
            </tr>';
    echo $res;
}
// フリーテキストの更新処理
add_action('usces_action_update_orderdata', 'my_second_usces_action_update_orderdata', 10, 5);
function my_second_usces_action_update_orderdata($new_orderdata, $old_status, $old_orderdata, $new_cart, $old_cart) {
    global $usces;
    if( isset($_POST['order_freetext'])) {
        // filter_inputにてPOST値をチェック
        $text = (string)filter_input(INPUT_POST, 'order_freetext');
        $usces->set_order_meta_value('order_freetext', $text, $new_orderdata->ID);
    }
}

とまあ、こんな感じの考え方で私はWordPress、Welcartを改造しまくっています。
例として受注リストにフリーテキスト項目を追加するを書きましたが、今後時間があれば他に実装したことも共有していきたいと思います。

6
7
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7