Programatically change template

Hi, I'd like a user to be able to select a template as they are creating a subsite (I need to do this after* the user is registered for my UX flow), so far - I'm at the point where the user enters information in a form and the subsite is created with an ajax request (wpmu_create_blog) and the new blog id is returned.

Once the subsite is created, I'm looking to return the available blog templates, their description, and image to the form, so that a user can click the one they like and have it update their site using the new blog ID. I also need to limit the templates returned based on other conditions, so this is why I need it done in the backend.

I am not seeing any API etc. to access the blog templates in the backend. Do you have one and if not how do I go about doing this? Thank you

  • Panos

    Hello svaults ,

    Hope you are doing well :slight_smile:

    The plugin has been written for specific functionality, that's why it's hard for you to find such documentation that you are looking for.

    I am not the developer however I would be happy to try and help out :smiley: .

    As I see you need three things:

    1. Blog ID and user id. I understand you have them sorted.

    2. The list of the templates. You can get this list from the plugin settings:

    $settings = nbt_get_settings();
    $templates = $settings['templates'];
    
    echo '<pre>'; print_r($templates); echo '</pre>';

    The listing is entirely up to you and how you wish to display them. From the print_r output you can find all necessary info you will need.

    You can take some examples of how to list templates from the template files which you can see at the folder blogtemplates/blogtemplatesfiles/template/

    3. Assign template to a blog. There is a sample code that can be found at file nbt-api.php, which uses the function
    nbt_api_copy_contents_to_new_blog( $source_blog_id, $new_blog_id, $new_user_id, $args )

    The first parameter is the blog id the template was created from. It is contained in the list mentioned at step 2 in the field "[blog_id]"

    The second one is the id of your new blog

    Third is the user id

    Fourth parameter is an array which should contain what you want to copy. Again all this is included in the templates list provided at step 2. You can include them in your $args array like following:

    $selected_template = $templates[5];
    
    $args = array(
    "to_copy" => $selected_template['to_copy'],
    "additional_tables" => $selected_template['additional_tables'],
    "copy_status" => $selected_template['copy_status'],
    "block_posts_pages" => $selected_template['block_posts_pages'],
    "post_category" => $selected_template['post_category'],
    "pages_ids" => $selected_template['pages_ids'],
    "update_dates" => $selected_template['update_dates']
    );

    Now that you have all parameters set you can execute nbt_api_copy_contents_to_new_blog function. Please keep in mind that you will probably need to :

    include_once( WP_CONTENT_DIR . '/plugins/blogtemplates/nbt-api.php' );
    nbt_load_api();

    as in the example code found in the nbt-api.php file.

    Hope this helps you achieve what you want :slight_smile:

    Kind regards,
    Panos

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.