Open file, write to file, save file as a zip and stream to user for download
I'd like if someone could give me some advice on creating this script, which I will add to the existing plug-in (see code below) script below.
So what I have now (with the script below) is a means to insert a predefined set of defaults into the wordpress site. What I'm wanting to add, is a helper utility, activated by a button or link that just reads "Copy Settings", that will take a site's existing settings (the sb2_options), write that to a file, then package the resulting file, along with the original file into a new zip file that essentially becomes a custom copy of the original plug-in for use in another site.
So the code needs to take an existing .php file containing the static code, open it up for writing, then insert all the name/value pairs from the wordpress options table matching a specific prefix (for example, all my custom options are prefixed with "sb2_"). Once it's done this, it would save the resulting file as "plugin.zip", for example and stream it to the user for download.
Here is the code that I have now, which sets up the site's defaults...
<?php
/**
* Plugin Name: my plugin
* Description: Sets up your sites defaults.
* Version: 1.0
*/
function sb2_plugin_init()
{
if ( get_option( 'sb2_plugin' ) == "")
{
//Begin Insert List here. Open the file and write out all the name value pairs, just like in the example.
//Option 1",
$sb2_option1 = "test";
//Option 2",
$sb2_option2 = "test";
//Option 1",
$sb2_option3 = "test";
//End insert list here
//update site defaults
update_option('sb2_option1', sb2_option1);
update_option('sb2_option2', sb2_option2);
update_option('sb2_option3', sb2_option3);
//etc
// Create post objects
$my_post = array();
$my_post['post_title'] = 'Main Blog Post Title';
$my_post['post_content'] = 'Main Blog Post Content';
开发者_如何学运维 $my_post['post_type'] = 'post';
//TODO >>> NEED TO MAKE THE POST STICKY
// Insert the post into the database
wp_insert_post($my_post);
wp_cache_flush();
update_option('sb2_plugin', "1");
}
}
add_action( 'init','sb2_plugin_init');
Reading and writing to file shouldn't be difficult for you but here is a good way to create zip files.
精彩评论