第一次練習開發 WordPress 外掛,心得紀錄如下:
Goal
- 提交 github 修改
- 整合 cmb2 到 plugin
- 開後台設定欄位
- 上架 wordpress.org svn
如何修改別人 Repo 的流程
先 fork 到自己的帳號,clone 到本機,remote 增加原始專案的 upstream,然後開分支處理,本機開發隨時 pull upstream,然後 push 到分支,完成後再開 pull request
$ git clone myfork
$ git remote add upstream <others repo>
$ git checkout -b <branch name>
$ git commit -am "message"
$ git pull upstream <branch name>
$ git push origin <branch name>
CMB2 的一些雷
- 放在 theme or plugin 的資料夾直接引入if ( file_exists( dirname( FILE) . ‘/cmb2/init.php’ ) ) { require_once dirname( FILE) . ‘/cmb2/init.php’; } elseif ( file_exists( dirname( FILE) . ‘/CMB2/init.php’ ) ) { require_once dirname( FILE) . ‘/CMB2/init.php’; }
- 取得 option page 的值使用 cmb2_get_option,至少要帶兩個參數 key 與 idecho cmb2_get_option(‘wfm_options’,’wfm_port’)
cmb2 API → https://cmb2.io/api/index.html
本地化外掛流程
- 主程式註解宣告的地方要有 Text domain 跟 Domain Path/* Plugin Name: Wp-ftp-media-library Plugin URI: http://wordpress.stackexchange.com/questions/74180/upload-images-to-remote-serverDescription: Let’s you upload images to ftp-server and remove the upload on the local machine. Version: 0.1 Author: Pontus Abrahamsson Author URI: http://pontusab.seText Domain: wp-ftp-media-library Domain Path: /languages */
- 用 wp cli 建立 pot 檔,進到外掛目錄,跑下面這段 cli,在寫 __e 的時候第二個參數 text domain 不能用變數,要寫死才行,不然 wp cli 會爬不到需要翻譯的字串$ wp i18n make-pot . languages/my-plugin.pot
- mo 檔一定要跟 Text Domain 一樣,{text-domain}-{locale}.mo
- 外掛主程式載入翻譯檔function wfm_load_text_domain() { load_plugin_textdomain( ‘wp-ftp-media-library’, false , basename( dirname( FILE) ) . ‘/languages’ ); } add_action(‘plugins_loaded’, ‘wfm_load_text_domain’);
FTP 刪除遠端圖片
- 使用 ftp_delete,使用流程一樣先 connect 再 login,然後利用 wp_get_attachment_url 取得原始圖路徑,再用 wp_get_attachment_metadata 取得縮圖路徑,hook 用 delete_attachment
- 開發外掛時檢查變數值的小技巧,用 error_log 把變數印在 error.log 訊息裡面
- 較大張的圖片會產生一張 wp_get_attachment_metadat 抓不到的縮圖圖片,
$file_year = substr(wp_get_attachment_metadata($args)['file'],0,8); $file_original = str_replace($settings['cdn'].'/',"",wp_get_attachment_url($args)); $file_thumb = $file_year.wp_get_attachment_metadata($args)['sizes']['thumbnail']['file']; $file_medium = $file_year.wp_get_attachment_metadata($args)['sizes']['medium']['file']; $file_large = $file_year.wp_get_attachment_metadata($args)['sizes']['large']['file']; $file_post = $file_year.wp_get_attachment_metadata($args)['sizes']['post-thumbnail']['file']; error_log($file_year, 0); ftp_delete($connection,$file_original); ftp_delete($connection,$file_thumb); ftp_delete($connection,$file_medium); ftp_delete($connection,$file_large); ftp_delete($connection,$file_post); ftp_close($connection);
外掛上架流程
- 外掛命名
- header comment
- text domain 命名
- readme.txt
Composer 安裝管理
- 使用 composer init 初始化
- composer install 安裝套件
- 要引入別人寫的套件寫在 compser.json 裡面的 require 定義,然後 require_once vendor/autoload.php
您好請問一下您還有再接Wordpress網站案子嗎
你好! 该插件不工作! 当您上传文件时,它是一个预加载器,没有任何其他反应
感謝告知,我再檢查看看!
当你将获得通知请!
Warning: Illegal string offset ‘file’ in / /wp-content/plugins/media-with-ftp-master/admin/class-media-with-ftp-admin.php on line 285
Warning: Illegal string offset ‘sizes’ in /wp-content/plugins/media-with-ftp-master/admin/class-media-with-ftp-admin.php on line 288
Warning: Illegal string offset ‘sizes’ in /wp-content/plugins/media-with-ftp-master/admin/class-media-with-ftp-admin.php on line 289
Warning: ftp_delete(): /wp-content/plugins/media-with-ftp-master/admin/class-media-with-ftp-admin.php on line 306