2014-02-28 91 views
0

我已經兩三週大了Wordpress,我正在爲一個基於wordpress的網站編寫一個小小的自定義系統插件。這個系統讀取一組json編碼文件,並將它們顯示爲表格列表。在插件設置頁面中編輯Wordpress遠程文件

加上我已經保存的一些基本設置,我想爲這些json文件包含一個簡單的文本編輯器,我想知道是否有一種方法可以爲options.php腳本「附加」自定義動作處理表單提交,以便發送文件路徑和內容。

我目前正在考慮編寫一些ajax,但我更喜歡在這裏問一下,也許有一個簡單的方法來實現這一點。

myplugin.php

function myplugins_menu(){ 
add_options_page('myplugin configuration','myplugin','manage_options', _ 
'myplugin_menu','myplugin_options'); 
//call register settings function 
add_action('admin_init', 'register_mysettings'); 
} 

add_action('admin_menu','myplugin_menu'); 

function myplugin_options(){ 
    include('admin/myplugin-admin.php'); 
} 


function register_mysettings() { 
    //register our settings 
    $setting_list=array('mail_title','mail_from','mail_to','recipient'); 
    foreach ($setting_list as $setting){ 
     register_setting('myplugin-settings-group', $setting); 
    } 
} 

爲myplugin-admin.php的

<div class="wrap"> 
<h2>My plugin</h2> 
<h3>My plugin Options</h3> 

<form method="post" action="options.php"> 
<?php settings_fields('myplugin-settings-group'); ?> 
<?php do_settings_sections('myplugin-settings-group'); ?> 
<table class="form-table"> 
    <?php 
    print_option('Mail Subject','mail_title'); 
    print_option('Sender address','mail_from'); 
    print_option('Recipient address','mail_to'); 
    print_option('Recipient name','recipient'); 
    ?> 
</table> 
<?php submit_button(); ?> 
</form> 

</div> 

<?php 
    echo "<select name=\"filename\" size=\"1\">"; 
    $dir = plugin_dir_path(__FILE__); 
    $files = glob($dir."../services/*.json"); 
    foreach ($files as $filename){ 
     echo "<option value=\"".basename($filename)."\">".basename($filename)."</option>"; 
    } 
    echo "</select>"; 

    $dir = plugin_dir_path(__FILE__); 
    $file = file(WP_PLUGIN_DIR."/myplugin/services/010.Luftansa.json"); 
    echo "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">"; 
    echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">"; 
    foreach($file as $text) { 
     echo $text; 
    } 
    echo "</textarea>"; 
    echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n 
    </form>"; 
} 

在上面的代碼片段, 我把_SERVER['PHP_SELF']爲形式的行動,但這變成/wp/wp-admin/options-general.php;

如果我把一個路徑給我的管理文件,我不能回到管理界面,除非通過http-refresh或類似的東西。

感謝您的閱讀:)

+0

您還沒有發佈[最小工作示例](http://stackoverflow.com/help/mcve)。測試你的代碼是不可能的:'myplugin_menu'有一個錯字,'print_option'函數丟失,沒有'010.Luftansa.json'樣例。 。 。 。是的,處理表單很容易,您是否檢查了[Settings API](https://codex.wordpress.org/Settings_API)文檔? 。 。 。是的,使用Ajax是一個不錯的選擇。 – brasofilo

+0

是的,我很抱歉,它有一個錯字,並不是一個最小的工作示例。 通過查看其他插件,我想出了一個解決方案,確實很簡單: '

?page = my_plugin_menu&tab = services」 >' 無論如何,我會發布完整的代碼,以防萬一它幫助其他人。 – Keber

回答

0

爲myplugin-admin.php的

<?php 

/** 
* @package my_plugin 
* @version 1.6 
*/ 
/* 
Plugin Name: my plugin 
Plugin URI: 
Description: 
Author: 
Version: 
Author URI: 
*/ 

include_once(WP_PLUGIN_DIR."/my_plugin/model.php"); 

function my_plugin_admin_scripts(){ 
?> 
      <script type='text/javascript'> 
       jQuery('select') 
        .change(function() { 
        var filename = ''; 
        jQuery('select option:selected').each(function() { 
         filename += jQuery(this).text() + ' '; 
        }); 
        jQuery.ajax({ 
         type: 'GET', 
         url: '<?php echo WP_PLUGIN_URL;?>/my_plugin/action.php', 
         data: 'action=' + 'getJsonFile'+'&file='+filename, 
         success: function(data) { 
          jQuery('#services_file').val(data); 
          }, 
         error: function(data) { 
          jQuery('#services_file').val('Something went wrong while loading '+filename+' file content'); 
          } 
         }); 
        }) 
        .trigger('change'); 
      </script> 
<?php 
} 
add_action('admin_footer', 'my_plugin_admin_scripts'); 

function print_option($label,$opt_name){ 

    $str = " 
    <tr valign=\"top\"> 
     <th scope=\"row\">{$label}</th> 
     <td><input type=\"text\" name=\"{$opt_name}\" value=\"".get_option($opt_name)."\" /></td> 
    </tr>"; 
    echo $str; 

} 


function my_plugin_admin_tabs($current = 'general') { 
    $tabs = array('general' => 'General', 'services' => 'Services'); 
    echo "<h3>My plugin Options</h3>"; 
    echo '<div id="icon-themes" class="icon32"><br></div>'; 
    echo '<h2 class="nav-tab-wrapper">'; 
    foreach($tabs as $tab => $name){ 
     $class = ($tab == $current) ? ' nav-tab-active' : ''; 
     echo "<a class='nav-tab$class' href='?page=my_plugin_menu&tab=$tab'>$name</a>"; 
    } 
    echo '</h2>'; 
} 

function tab_content_general() { 
?> 
    <form method="post" action="options.php"> 
    <?php settings_fields('my_plugin-settings-group'); ?> 
    <?php do_settings_sections('my_plugin-settings-group'); ?> 
    <table class="form-table"> 
     <?php 
     print_option('Mail Subject','mail_title'); 
     print_option('Sender address','mail_from'); 
     print_option('Recipient address','mail_to'); 
     print_option('Recipient name','recipient'); 
     ?> 
    </table> 
    <?php submit_button(); ?> 
    </form> 
<?php 
} 



function tab_content_services(){ 
    $files = glob(WP_PLUGIN_DIR."/my_plugin/services/*.json"); 
?> 
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?page=my_plugin_menu&tab=services"> 
    <table class="form-table"> 
    <tr><td><select name="filename" size="1" style="width: 180px"> 
<?php 
    foreach ($files as $filename){ 
     echo "<option value=\"".basename($filename)."\">".basename($filename)."</option>"; 
    } 
?> 
    </select> 
    <input name="Submit" type="submit" value="Update" />  
    </td></tr> 
    <tr><td><textarea id="services_file" name="update" cols="80" rows="18" style="font-family: monospace"> 
    </textarea></td></tr> 
    </table> 
    </form> 
<?php 
} 

$tab = (isset ($_GET['tab']))?$_GET['tab']:"general"; 
my_plugin_admin_tabs($tab); 

?> 
<div class="wrap"> 
<?php 
    switch ($tab) { 
     case "general": 
      tab_content_general(); 
      break; 
     case "services": 
      tab_content_services(); 
      break; 
     default: 
      break; 
    } 
?> 
</div> 

<?php 
if($_POST['Submit']){ 
    save_json_file($_POST['filename'],$_POST['update']); 
} 
?> 

action.php的

<?php 
define('WP_USE_THEMES', false); 
require_once('../../../wp-load.php'); 
require_once('model.php');  

//main 

if (isset($_GET['action'])){ 
    $action = $_GET['action']; 
} 
if (isset($_POST['action'])){ 
    $action = $_POST['action']; 
} 

if (isset($action)) { 
    switch ($action){ 
     case 'getJsonFile': 
      getJsonFile($_GET['file']); 
      break; 
     default: 
      echo "action unknown"; 
      break; 
    } 
} 
?> 

model.php

<?php 
function isJson($string) { 
    json_decode($string); 
    return (json_last_error() == JSON_ERROR_NONE); 
} 

function read_plain_json($file){ 
    $file = file(WP_PLUGIN_DIR."/my_plugin/services/".$file); 
    return implode("",$file); 
} 

function getJsonFile($file){ 
    echo read_plain_json($file); 
    die(); 
} 

function save_json_file($file,$content){ 
    $open = fopen(WP_PLUGIN_DIR."/my_plugin/services/".$file,"w+"); 
    fwrite($open, stripslashes($content)); 
    fclose($open); 
}  

?> 

my_plugin /服務/ 010 .Luftansa.json

[ 
{ 
    "subcat": "Natural Nail Care", 
    "column": ["service","Mani","Pedi","Mani &amp; Pedi","info","book"], 
    "service": [ 
    ["Deluxe","40.00","55.00","90.00","Service Information","Book Deluxe"], 
     ["Express","30.00","45.00","71.00","","BookExpress"], 
     ["Fully Loaded","55","70","119.00","ServiceInformation","Book Fully Loaded"], 
    ["French","45","60","100.00","","Book French"], 
    ["Pure and Natural","50","60","105.00","Service Information","Book Pure And Natural"], 
    ["Deluxe Series of 6","216","297","487.00","","Book Deluxe Series Of 6"], 
    ["Re-varnish and Tidy-up Hands","18","20","35","","Book Re-Varnish And Tidy-Up Hands"], 
    ["Course of 6","","","","","Book Course Of 6"] 

    ] 
}