【WordPress】使用VSCode的SFTP來掛載WordPress的wp-content目錄
在這裏,我做了一個動作,為了方便測試開發WordPress的外掛與主題,我設置了sftp的遠端目錄至:
程式碼:
存檔(或按下ctrls+S)就可以在遠端自動建立file-upload-example資料夾,並且將file-upload-example.php上傳至主機。(請自行透過ftp或後台檔案管理員檢查,資料夾與檔案是否已經在主機上了。)
然後到WordPress後台的已安裝外掛中,啟用file-upload-examp這個外掛:
"remotePath": "/home/u1085100/public_html/wp2/wp-content",
在本地端,我則建立三個資料夾:
- languages,語系檔目錄
- plugins,外掛目錄
- themes,主題目錄
建好後,我要測試file-upload-example這個外掛範例,那麼我就在本地端的plugins建立file-upload-example資料夾,並在該資料夾下建立file-upload-example.php,
程式碼:
<?php /* Plugin Name: File Upload Example Description: demo plugin that demonstrates how to do file upload from admin page. Author: Richard Dinh //http://wordpress.stackexchange.com/questions/98375/how-to-upload-image-with-simple-form this code is a demo only. You really should do more validation and stuff */ // add the admin options page add_action('admin_menu', 'file_upload_example_admin_add_page'); function file_upload_example_admin_add_page() { add_options_page('File Upload Example', 'File Upload Example', 'manage_options', 'file-upload-example', 'file_upload_example_options_page'); } function file_upload_example_options_page() { if ( empty( $_FILES ) ){ ?> <div> <h2>Upload a file here</h2> <form action="" method="post" enctype="multipart/form-data"> <?php wp_nonce_field('csv-import'); ?> <label for="file">Filename:</label> <input type="file" name="file" id="file"><br> <input type="submit" name="save" value="save"> </form> </div> <?php } else{ if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' ); $uploadedfile = $_FILES['file']; $upload_overrides = array( 'test_form' => false ); $movefile = wp_handle_upload( $uploadedfile, $upload_overrides ); if ( $movefile ) { echo "File is valid, and was successfully uploaded.n"; var_dump( $movefile ); // here you can do some stuff with this } else { echo "Possible file upload attack!n"; } } }
啟用後,這個測試外掛已經在設定下增加了一個功能選項"File Upload Example":
最後,這個https://github.com/flacoman91/wordpress-demos放置了許多不錯簡短的WordPress外掛範例供我們使用與學習。