Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Create a Automatic Demo Site Which Deletes Itself After.. WordPress

Hello there,

I want to make a WP theme website, where I want to add a possibility for users to test drive a theme before buying it. I've installed WP MU for demo sites, I want them to enter their email address, name and they will be emailed their login details where they can just activate the theme/s they want to see and can also add a dummy data. The demo site/s created should get deleted after 5 days or so.

Please refer to this link and check how it works:

Thank you.

Answers (2)


John Cotton answers:

It's easy enough to do. You need to look at the [[LINK href=""]]wpmu_create_blog[[/LINK]], [[LINK href=""]]wp_insert_user[[/LINK]] and [[LINK href=""]]wpmu_welcome_notification[[/LINK]] functions.

Use of those three will create the blog, the user account to login and email the account details off to them.

In terms of taking it down, you could run a cron job that checks how long it's been there and then execute wpmu_delete_blog if it's time to remove it.

Ritwick comments:

Hi John,

Thanks for your reply.

Could you please explain it more?

John Cotton comments:

<blockquote>Hi John you can easily create a cron in WordPress, WP has its own cron.</blockquote>
Sure you can, but it relies on visits to the website to activate. Perhaps in this case that doesn't matter (since a visit to a site that should be down would trigger it to be taken offline), but sometimes its' important that tasks are run at specific times. In those circumstances, relying on the internal cron could cause problems.

So I prefer to set mine up externally. That way, I chose exactly when things are run and I haven't got to edit my code to make changes to that.

John Cotton comments:

<blockquote>Could you please explain it more?</blockquote>
Which bit don't you understand?

If you read the Codex pages I've linked to, you should be able to construct the code easily (it's not many lines....).

The cron job just needs to look up the "registered" value from the wp_blogs table, and then use a [[LINK href=""]]date_diff[[/LINK]] caluclation to determine whether to remove the blog or not.

Ritwick comments:

Hi John,

Thank you for your reply.

I am barely able to make things out of it, may be because its not something from standard WP things. Could you make a code for it all and if there is something you need to be done for that, let me know. Thank you.


Francisco Javier Carazo Gil answers:

As John says you have to use these functions. To control time try the next functions:
wp_schedule_event($timestamp, $recurrencia, $hook, $args);

You should create something like a cron. First create it:
if ( !wp_next_scheduled('mi_hook') ) {
wp_schedule_event( time(), 'daily', 'mi_hook' );

The function that have to run (fill it with the function that John has said):
function my_task() {
// function

Finally, join them:
add_action('mi_hook', 'my_task');

When you need to switch it off, use this:

function mi_tarea_desactivar() {
delete_action( 'mi_hook', 'mi_tarea_desactivar' );

Ritwick comments:

Hello Francisco,

Thank you for your reply, could you show me how to make a cron job? I am not good with these things.

A dummy which I can test out would be really appreciated.

Francisco Javier Carazo Gil comments:

Hi John you can easily create a cron in WordPress, WP has its own cron.

Look at this. With instructions given by John you create a function called delete_old_blogs() for example.

With this function you create a plugin around it using this other function to handle cron tasks:

if ( !wp_next_scheduled('my_task_hook') ) {
wp_schedule_event( time(), 'daily', 'my_task_hook' ); // hourly, daily and twicedaily
add_action( 'my_task_hook', 'delete_old_blogs' );

Be care with the first parameter, it indicates the timestamp of the first execution.

If you need more options than: hourly, daily and twicedaily, you have to use a filter:

add_filter( 'cron_schedules', 'filter_cron_schedules' );
// add custom time to cron
function filter_cron_schedules( $param ) {
return array( 'once_half_hour' => array(
'interval' => 1800, // seconds
'display' => __( 'Once Half an Hour' )
) );

You will need five days cron.

Ritwick comments:

Hi Francisco,

Thank you for your helpful response.

I guess, the above code deletes all the blogs after x days. But could you please provide a code which will run individually on new installation (blog/s ) and deletes each of them after 5 days of their creation date.

Expanding the request more, if you could please provide a code which stays in the core of WP MU and runs every day at a specific time and deletes all the blogs which are older than 5 days excluding the main installation. This will actually solve all the problem.

Thanks in anticipation.

Francisco Javier Carazo Gil comments:


You will only have to do a function that hourly, for example, check "registered_date" in wp_blogs table of your WordPress Network installation.

If blog has been alive more thant 5 days, use delete blog functions.

Ritwick comments:

Hi Francisco,

Could you please provide a code to me of what you stated above? Thanks.

Francisco Javier Carazo Gil comments:


I can't do that for $10.

I have to go out, if you want send me a private message and we talk about it.