• I’d like to be able to create widget instances and assign them to a widget area when a theme is initially activated. I can’t seem to find any documentation or discussion on how to go about this. There definitely aren’t any specific WordPress functions for this, but I’d like to figure out how to do it even if its fairly complex.

    Bear with me while I explain why I think this is a good idea…

    I have several themes on WordPress.org and have been working on ways to have the theme preview look good. i.e. Show off some of the theme’s features.

    We all know the theme Preview data is pretty minimal. It doesn’t even have featured images or different posts types or anything else that shows off even the core WordPress features. The theme guidelines don’t even let our theme screenshot really show off the theme’s features unless the theme looks like the screenshot right out of the box. Plus you can’t have multiple screenshots and the screenshot is so short you can’t show off a whole page or even a few “sections” of a page.

    One of the things that drives users to 3rd-party theme sites is that the themes there have multiple screenshots and they are allowed to show off the theme features in them. The theme previews are also driven off of demo data specifically set up to show off the theme’s features.

    For now, I have two of my theme’s widget areas set to default to displaying certain widgets. One of them is a sample “call to action” section. One of my themes also includes sample social networking icons, a sample map and company address. Another has a sample contact form. So those things looks nice in the theme preview, but then the user has no idea how to recreate similar widgets.

    I don’t believe anyone would suggest the user go into the PHP source and hard-code these things for their site. So I’m looking for a better way and all I can come up with is the idea of automatically creating these sample widget instances and assigning them when the theme is activated.

    Specifically, I’d like to check to see if the sample widget instances are there and if not then create them and assign them to the respective widget areas. Most of these are text widgets that use CSS built into the theme to style them. This way the user could easily go into each text widget and update them for their site.

Viewing 1 replies (of 1 total)
  • Thread Starter Tim Nicholson

    (@timnicholson)

    AFAICT, there don’t seem to be any functions related to this. It looks like widgets are added only through the Admin interface via the widgets.php file. This file uses ajax calls to post (and get and delete) to add widgets to widget areas.

    I think I’ve figured out how to create a nonce for security purposes and then I could use wp_remote_post() to trigger the add. However, there are several fields that I wouldn’t begin to know how to fill. See the “???” below.

    $_POST[$nonce] = wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', $referrer = false, $echo = false );
    $_POST['widget-id'] = 999;
    $_POST['multi_number'] = ???; (optional)
    $_POST['sidebar'] = 'sidebar-4';
    $_POST[$sidebar_id . '_position'] = null; (optional)
    $_POST['id_base'] = ???;

    I’m just not sure how to work through the remaining details. I’m also not sure if this is something that the WordPress.org theme review team would frown upon.

    Do general theming questions get much attention here? It seems like most of the activity is threads for support of individual themes. Is there a better place for us theme developers to discuss topics like this?

Viewing 1 replies (of 1 total)
  • The topic ‘Creating a Widget Instance Upon Theme Activation’ is closed to new replies.