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


11

是否可以允许用户从新的站点注册页面中选择他们想要安装的主题?创建网站后,显然可以安装他们选择的任何主题。

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

如果有人知道如何通过重力形式做到这一点,那也很好。

更新:

这是我到目前为止的内容,它没有考虑到儿童主题,将在之后

此函数将输出带有单选按钮的主题列表,将所选主题存储在$ _POST ['custom_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_theme的值。可以很好地创建网站,但是模板和样式表选项为空白。无论我如何尝试,都无法获得价值。我想我必须使用$ _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' );

1
我认为这是一个好问题,+ 1
Anh Tran 2012年

1
从理论上讲,可以通过向注册表单添加其他字段来实现,但是用户如何知道主题的外观?预览将使注册过程变得更加复杂……
krembo99 2012年

@ krembo99公平点。我试图简化这个问题。我将使用带有缩略图预览的单选字段,或者在每个主题页面上都有一个按钮,上面显示“使用此主题注册”。该按钮只需将主题名称的名称传递给注册表单。以为我会从简单开始:)
Andrew

1
确定-在这种情况下,如果你真的想这样做,请参阅我的答案..
krembo99

Answers:


5

为了执行您想要的操作,您可以添加所需的任何字段,然后将其存储在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_theme:

get_user_meta($user_id, 'user_custom_theme', true);

注意:这是即时写的。它没有在多博客上进行验证,而是在简单的wp安装上进行了验证,尽管应该没有太大的区别-但这仍然不是生产功能,只是使您处于正确的轨道。需要进行卫生检查并检查变量,清洁代码和FORM MARKUP,以及将该字段也添加到其他与用户相关的屏幕(创建用户,编辑用户,编辑配置文件等)。

注意II:您询问了您的uodate中的重力形式- 它们有一个附加组件


谢谢你的帮助。我已经设法整理了一些东西,并且几乎可以正常工作了。只是很难让隐藏字段粘住并将值传递给最后一个函数。更新了我的问题,以包括到目前为止的进展。
安德鲁(Andrew)

你试过我的功能吗?它应该工作..
krembo99 2012年

您的示例中的register_form钩子不适用于多站点。找到了另一个将单选按钮添加到的钩子。同样不建议使用get_themes(),我发现了一种获取主题信息的更好方法。最后,我认为将主题名称添加到用户的元表中并不是最好的方法,主题的templatestylesheet存储在选项表中。话虽如此,到目前为止,您的代码已获得了巨大的帮助,谢谢。
安德鲁(Andrew)

register_form挂钩在多站点上使用(请参阅CODEX codex.wordpress.org/Plugin_API/Action_Reference/register_form)。您可以找到许多其他的钩子,但这将是使用恕我直言的正确选择。关于其他评论,我不是很明白这点,在乎解释吗?
krembo99 2012年

感谢您对重力形式用户注册插件的说明。我已经有了该插件,但是当用户注册站点时它不允许您选择主题,因此是我的问题。
安德鲁(Andrew)

1

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

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


0

这种回答可以回答您的问题:我们在以下站点上放置了一个名为“ Theme Switch ” 的插件:focusww.com,它在侧边栏提供了一个主题列表,您可以从中进行选择。它使您可以选择可以使用哪些主题,以及cookie过期之前多长时间恢复为默认主题。


抱歉,我没有使用“主题切换器”。我希望用户在多站点安装中使用wp-signup.php注册博客时能够选择主题。
安德鲁(Andrew)

我偶然发现了一个售价19美元的插件,该插件可让用户在注册时安装主题:premium.wpmudev.org/project/new-blog-template <=签出:)
Nohl

0

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

/**
 * 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/)。希望这会有所帮助。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.