How to use add_option during blog creation

Hey guys. Me again. I have tried for hours and seem to be getting nowhere so have come to you for your expert help...

I am trying to achieve something which I believe should be simple. However it does not seem to work for me.

All I want to do is add a few options to the wp_options table when a user creates a blog.

<?php // update smtp settings on blog creation
			add_option( 'smtp_host', '', '', 'yes' );
			add_option( 'mail_from_name', 'crazyname', '', 'yes' );
			add_option( 'smtp_from', 'crazyemail', '', 'yes' ); ?>

This is basically what I am using to test. I have tried this in a few places, but I have put this in the remove-email-verification plug-in as this happens once the new blog details have been submitted.

I have actually added it to here;

function activate_on_blog_signup($domain, $path, $title, $user, $user_email, $key, $meta) {

But no options are being added to the new blog when I check in phpmyadmin.

What am I doing wrong?

Thanks for any help, as always!! :slight_smile:

  • ffdltd
    • Design Lord, Child of Thor

    OK, so it turns out it IS working, but it is applying to the main blog, not the newly created users blog (I realised this when I tried 'update_option' and noticed the change)

    So, all I need to figure out is how to apply this to the newly created blog only and not the main blog on the network.... hmmmm!

  • aristath
    • Recruit

    Hello there @ffdltd,
    Try the code below:

    function adding_my_custom_options() {
      if ( !get_option( 'smtp_host' ) )
        add_option( 'smtp_host', '', '', 'yes' );
      if ( !get_option( 'mail_from_name' ) )
        add_option( 'mail_from_name', 'crazyname', '', 'yes' );
      if ( !get_option( 'smtp_from' ) )
        add_option( 'smtp_from', 'crazyemail', '', 'yes' );
    add_action( 'init', 'adding_my_custom_options' );

    I also added this on a gist for better readability:

    What this actually does is this:
    First, it checks if the settings exist.
    If (and only if) they don't, they they are created.
    All of this was wrapped in a function, and I added an action that makes this function run when WordPress initializes.

    Please let me know if that works or you...


    Was that helpful?

  • ffdltd
    • Design Lord, Child of Thor

    OK, I figured it out. Typical it was 10 mins after asking. You need to use add_blog_option and specify the blog ID. If anyone else needs the info then the code is here:

    $blog_id = $result['blog_id'];
    add_blog_option( $blog_id, 'mail_from_name', 'crazyname' );

    Pretty damn simple once you know how! 2 hours I will never get back haha.

  • ffdltd
    • Design Lord, Child of Thor

    I just saw your reply Ari.

    Thank you so much for your help, let me take a look at this as it seems like a better solution than my hacking of other plug-ins!! :smiley:

    Appreciate your prompt response and answer!!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.