允许用户在注册时选择要安装的主题

时间:2012-10-23 作者:Andrew

是否可以允许用户从新站点注册页中选择要安装的主题?一旦创建了网站,它显然会安装他们选择的主题。

我发现wp_get_themes. 这就是您如何预先填充包含所有可用主题的下拉菜单的方法吗?如何将主题的信息传递到实际的注册过程中,以便使用正确的主题创建网站?

如果有人知道如何处理重力形式,那也太好了。

Update:

这是我到目前为止所做的,它没有考虑到儿童主题,之后将继续研究

此函数将输出带有单选按钮的主题列表,将所选主题存储在$\\u POST[\'custom\\u theme\']

/**
* Show list of themes at bottom of wp-signup.php (multisite)
*/
function 70169_add_signup_extra_fields() { ?>

Themes<br />
<?php
$themes = wp_get_themes();

foreach ( $themes as $theme ) {
    $theme_name = $theme[\'Name\'];
    $theme_stylesheet = $theme->stylesheet;
?>
    <label>
        <input id="<?php echo $theme_stylesheet; ?>" type="radio" <?php if ( isset( $_POST[\'custom_theme\'] ) ) checked( $_POST[\'custom_theme\'], $theme_stylesheet ); ?> name="custom_theme" value="<?php echo $theme_stylesheet; ?>" ><?php echo $theme_name; ?>
    </label>

<?php } ?>

<?php }
add_action( \'signup_extra_fields\', \'70169_add_signup_extra_fields\' );
我想我应该添加一个隐藏字段,以将主题的价值传递到网站创建上。不过,这有点问题——在最后一步,它失去了它的价值,但不知道为什么。

/**
 * Add a hidden field with the theme\'s value
 */
function 70169_theme_hidden_fields() { ?>

<?php
    $theme = isset( $_POST[\'custom_theme\'] ) ? $_POST[\'custom_theme\'] : null;
?>
<input type="hidden" name="user_theme" value="<?php echo $theme; ?>" />
<?php }
add_action( \'signup_hidden_fields\', \'70169_theme_hidden_fields\' );
最后是一个函数,用于将主题名称传递给新创建的站点。如果我硬编码变量,这是可行的,但我还无法传递custom\\u主题的值。网站创建得很好,但模板和样式表选项为空。无论我怎么努力,都无法获得价值。我想我必须使用$\\u GET来访问我之前创建的隐藏字段。同样,此时我要做的就是将相同的主题名称传递给模板和样式表选项,我将在工作后找出如何区分它们。

/**     
 * Create the new site with the theme name
*/
function 70169_wpmu_new_blog( $blog_id ) {

// need to get this working, use $_GET?
//    $theme = ???

    update_blog_option( $blog_id, \'template\', $theme );  // $theme works if I hardcode it with a theme name
    update_blog_option( $blog_id, \'stylesheet\', $theme );
}

add_action( \'wpmu_new_blog\', \'70169_wpmu_new_blog\' );

4 个回复
SO网友:krembo99

为了完成所需操作,可以添加所需的任何字段,然后将它们存储在user_meta ...

(也可以将其存储在$user_info 数组/对象,但我不确定有什么好处……)

  // Render Form Fields
add_action(\'register_form\',\'k99_register_form_add_theme_field\');
// Checking
add_action(\'register_post\',\'k99_check_fields\',10,3);
// Insert Data
add_action(\'user_register\', \'k99_register_new_register_fields\');

// Render the form with the additional radio field 
function k99_register_form_add_theme_field(){
?>

<p>
<label>Theme<br />
 <?php $themes=wp_get_themes();
foreach ($themes as $theme ) {
$theme[\'Name\'] = sanitize_title_with_dashes($theme[\'Name\']);
$checked = checked( $_POST[\'custom_theme\'], 1 );
 echo \'<input id="custom_theme\'.$theme[\'Name\'] .\'" type="radio" name="custom_theme" value="\'. $theme[\'Name\'] .\'" \'.$checked.\'>  \'. $theme[\'Name\'].\'<br />\';
$custom_theme = $_POST[\'custom_theme\'];
} ?>
</label>
</p>

<?php
}

// checking , sanitation etc .. of course this is not done...

function k99_check_fields($login, $email, $errors) {
global $custom_theme;
if ($_POST[\'custom_theme\'] == \'\') {
$errors->add(\'empty_theme\', "<strong>Error:</strong> Please select theme.");
}
else {
$custom_theme = $_POST[\'custom_theme\'];
}
}

// Write to DB ... if you will..
function k99_register_new_register_fields($user_id, $password="", $meta=array())  {

$custom_theme = $_POST[\'custom_theme\']; //just in case ..
update_usermeta($user_id, \'user_custom_theme\',$custom_theme);

}
所有这些之后,您可以像这样检索user\\u主题:

get_user_meta($user_id, \'user_custom_theme\', true);
NOTE : 这是即时写的。它并没有在多博客上得到验证,而是在一个简单的wp安装上得到验证,虽然应该没有太大区别-但这不是一个生产功能,只是为了让您走上正确的轨道。需要清洁和检查变量、清理代码和表单标记,以及将字段也添加到其他与用户相关的屏幕(创建用户、编辑用户、编辑配置文件等)。

NOTE II: 您在uodate中询问了重力表格-they have an add-on for that

SO网友:speedypancake

我知道这是一种欺骗,但我使用这个插件。它允许您复制任何现有的网络站点,然后在新用户注册时将其作为模板提供。您可以创建任意数量的新博客模板。它们将包含所有内容、插件、设置等,用户可以在设置新站点/帐户时选择一个:)

http://premium.wpmudev.org/project/new-blog-template/

SO网友:Nohl

这类回答了你的问题:我们放置了一个名为\'Theme Switch\' 在这个网站上:focusww。com,它放了一个侧边栏,您可以从主题列表中进行选择。它允许您选择可以使用哪些主题,以及在cookie过期之前多长时间恢复为默认主题。

SO网友:ankittiwaari

如果仍然相关,也许这可以帮助其他人寻找类似的解决方案

/**
 * Add custom field to registration form
 */
add_action( \'signup_blogform\', \'aoc_show_addtional_fields\' );
add_action( \'user_register\', \'aoc_register_extra_fields\' );

function aoc_show_addtional_fields() 
{
    $themes = wp_get_themes();
    echo \'<label>Choose template for your site\';
    foreach ($themes as $theme){
        echo \'<img src="\'.$theme->get_screenshot().\'" width="240"/>\';
        echo $theme->name . \' <input id="template" type="radio" tabindex="30" size="25" value="\'.$theme->template.\'" name="template" />\';
    }
    echo \'</label>\';
}

function aoc_register_extra_fields ( $user_id, $password = "", $meta = array() ) {
    update_user_meta( $user_id, \'template\', $_POST[\'template\'] );
}

// The value submitted in our custom input field needs to be added to meta array as the user might not be created yet.
add_filter(\'add_signup_meta\', \'aoc_append_extra_field_as_meta\');
function aoc_append_extra_field_as_meta($meta) 
{
    if(isset($_REQUEST[\'template\'])) {
        $meta[\'template\'] = $_REQUEST[\'template\'];
    }
    return $meta;
}

// Once the new site added by registered user is created and activated by user after email verification, update the template selected by user in database.
add_action(\'wpmu_new_blog\', \'aoc_extra_field\', 10, 6);
function aoc_extra_field($blog_id, $user_id, $domain, $path, $site_id, $meta) 
{
    update_blog_option($blog_id, \'template\', $meta[\'template\']);
    update_blog_option($blog_id, \'stylesheet\', $meta[\'template\']);
}
我写了一篇博文(http://artofcoding.in/select-theme-while-registering-wordpress-multisite-network/) 在这里,我有一个类似的要求。希望这有帮助。

结束