在插件设置页面中编辑Wordpress远程文件

时间:2014-02-28 17:21:27

标签: php jquery ajax wordpress-plugin wordpress

我有两到三周的时间来使用Wordpress,我正在编写一个小型自定义系统作为基于wordpress的网站的插件。该系统读取一组json编码文件并将其显示为表格列表。

与我已经保存的一些基本设置一起,我想为这些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之类的东西,我就无法返回管理界面。

感谢阅读:)

1 个答案:

答案 0 :(得分: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"]

    ]
}