How to update 'Site Title' from custom settings API

Hi folks.

I am a little stuck on something at the mo. I am using the WP Settings API to create some custom admin options pages. One of them saves business info such as business name, address, telephone etc.

I would actually like business name to hook up to ‘Site Title’ so that this can be set when a user first creates a site.

Do you know if this option can be updated in the DB via a field in the Settings API and if so could you provide any guidelines?

My current code for the business name in the settings page is:

$this->settings['business_name'] = array(
'title' => __( 'Business Name' ),
'desc' => __( 'Your full business name as you would like it to be displayed.' ),
'id' => 'busname',
'std' => $blog_title,
'type' => 'text',
'section' => 'general'
);

I have searched the net and have not found much that is any good.

Many thanks for any help!

  • Alexander
    • DEV MAN’s Mascot

    Hey there @ffdltd,

    The meta keys are “blogname” and “blogdescription” respectively.

    These are covered under the New Theme Customization API, but it shouldn’t be hard to hook them up to what you’re doing.

    https://codex.wordpress.org/Theme_Customization_API

    You can update them with these functions:

    update_option (single site)

    update_blog_option (sub site in multisite network)

    update_site_option (main site in multisite network

    http://codex.wordpress.org/Function_Reference/update_option

    http://codex.wordpress.org/Function_Reference/update_blog_option

    http://codex.wordpress.org/Function_Reference/update_site_option

  • ffdltd
    • Design Lord, Child of Thor

    Thanks for your help here!

    I have figured out how to update options, but I am struggling with how to get what is entered into a settings field (the one in my first post) to use in the update_option when the page is ‘saved’.

    So what I need to happen is, when a user saves my custom settings page, it pulls the ‘business name’ that has just been entered and uses that to update the desired ‘option.

    I actually have it working using this code, but it is always one behind as it doesnt get the value when the page is saved. So whenefer I update the business name field it updates the business name option OK, but the blogname is the previous entry.

    $options = get_option( 'business_setup' );
    $bus_name = $options['business_name'];

    update_option( 'blogname', $bus_name );

    I think I need to use some kind of ‘$_POST[ ]’ but I am unsure??

    Please help!! Thanks a lot :slight_smile:

  • Alexander
    • DEV MAN’s Mascot

    Hey @ffdltd,

    You shouldn’t need to use $_POST, in fact you probably just need to put that code you have there in a hook so it runs after the other option is updated.

    You could hook the updating of the other option itself by doing something like this:

    add_action('update_option_business_setup','your_function');

    function your_function() {
    //code from above
    }

    This uses the “update_option_(option_name)” hook you can read about here: http://codex.wordpress.org/index.php?title=Plugin_API/Action_Reference/

    Let me know if this works for you.

    Best regards

  • ffdltd
    • Design Lord, Child of Thor

    Thanks for your help again!

    Almost there now I can feel it haha!

    I have added the above but with no luck. Nothing seems to happen. Do I need to place the add_action in a specific place on the page?

    I read elsewhere that no hook is given when the API options page is saved. Could this be the problem?

  • Greta (M3PCS)
    • New Recruit

    Hi there,

    Perhaps try: adding update_option(‘blogname’:wink: to the validation or sanitization callback you used in your register_settings() declaration.

    // assuming you have registered your settings with a validation/sanitization callback function.
    register_settings('your_custom_page' , 'business_setup' , 'your_validation_or_sanitization_callback');
    // validation callback $input is the settings inputs form used to compare against the stuff already there:
    function your_validation_or_sanitization_callback($input){
    $options = get_option( 'business_setup' );

    //example settings field check: business_email check if it is indeed in email format
    if ( is_email( $input['business_email'] ) ){
    $options['business_email'] = $input['business_email'];
    }else{
    // looks like its not a valid email add an error field
    add_settings_error( 'business_setup', 'invalid-email', 'You have entered an invalid e-mail address.' );
    }

    //lets check to make sure the business_name field is not empty
    if( !empty($input['business_name']) ){
    // Lets set the existing options value with the new form input value
    $options['business_name'] = $input['business_name'];
    //lets get the current site title option to compare
    $site_title = get_option('blogname');
    // if the business name does not equal the site title update it
    if($options['business_name'] != $site_title){
    $bus_name = $option['business_name'];
    update_option('blogname', $bus_name);
    }
    //Business_name is EMPTY... GRRR
    }else{
    // looks like its not a valid email add an error field see:http://codex.wordpress.org/Function_Reference/add_settings_error
    add_settings_error( 'business_setup', 'invalid-business-name', 'A Business name is Required.' );
    }

    // rinse repeat if you have other validation or sanitizations you wish to target..

    return $options;

    }

    Please note this is wholly untested… but give it a try..

    Just an experimental thought…

  • Alexander
    • DEV MAN’s Mascot

    @ffdltd,

    The add_action would need to be called before anything else. So it should be fine if you’ve just included it in the base of the plugin and not within a function that is delayed until later.

    I was actually unaware that those hooks aren’t fired during a settings API update.

    I think Greta has a great suggestion there! That’s a perfect place to get access to that data during the same request that it was submitted. I’d give that a try next, and see what you can come up with,

    Thanks for being a WPMU DEV member. Let us know if we can be of any further assistance.

    Best regards,

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.