创建管理选项页,用户可以在其中对主题进行更改

时间:2015-07-18 作者:user3438958

我需要帮助来完成这最后一段php代码。我正在尝试创建一个类似于“管理”>“设置”>“讨论”中的管理选项页面,例如用户如何选择化身,但用户选择的不是化身,而是改变主题的样式选项(通过在前端将CSS文件排队)。

我的CSS文件已完成。对于php,到目前为止,我有以下内容:

<?php

// register CSS files ready
function register_custom_styles() {
    wp_register_style( \'style1\', plugins_url( \'/css/style1.css\', (__FILE__) ) );
    wp_register_style( \'style2\', plugins_url( \'/css/style2.css\', (__FILE__) ) );
    wp_register_style( \'style3\', plugins_url( \'/css/style3.css\', (__FILE__) ) );
}
add_action( \'init\', \'register_custom_styles\' );


//create admin sub menu - admin>appearance>styles
add_action(\'admin_menu\', \'my_custom_submenu\');
function my_custom_submenu() {
    add_submenu_page( \'themes.php\', \'Styles\', \'Styles\', \'manage_options\', \'styles\', \'my_custom_submenu_page\' );
}


//create admin page for admin>appearance>styles
function my_custom_submenu_page() {
?>
<div>
  <h2>Select Style</h2>
  <form method="post" action="options.php">

     <!--this option to wp_enqueue_style(\'style1\')-->
     <label>  <input type="radio" name="myoption[radio1]" value="style1" />  <img src="//path-to-style1-img" />  Style1  </label>  

     <br /> 

     <!--this option to wp_enqueue_style(\'style2\')-->
     <label>  <input type="radio" name="myoption[radio1]" value="style2" />  <img src="//path-to-style2-img" />  Style2  </label>  

     <br /> 

     <!--this option to wp_enqueue_style(\'style3\')-->
     <label>  <input type="radio" name="myoption[radio1]" value="style3" />  <img src="path-to-style3-img" />  Style3  </label>  

     <br /> 

     <?php submit_button(); ?>

  </form>
</div>
<?php
}
如您所见,当用户选择单选输入并单击提交按钮时,我不知道如何将该选项与注册的CSS文件连接起来。

https://codex.wordpress.org/Creating_Options_Pages 显示了如何创建选项页面,但没有示例说明如何将CSS文件排入选项队列。

感谢您的帮助。

2 个回复
最合适的回答,由SO网友:user3438958 整理而成

经过长时间的努力,我终于找到了工作!

感谢Samuel Elh对我的帮助,http://sam.elegance-style.com/

如果有人想允许用户对主题进行小的样式更改,这将非常有用。如果您只需要一些样式表否决来更改主题,而不是创建许多子主题,这是理想的:

  /*
Plugin Name: test1
*/

// register CSS files ready
function register_custom_styles() {
    wp_register_style( \'style1\', plugins_url( \'/css/themes/s1.css\', (__FILE__) ) );
    wp_register_style( \'style2\', plugins_url( \'/css/themes/s2.css\', (__FILE__) ) );
    wp_register_style( \'style3\', plugins_url( \'/css/themes/s3.css\', (__FILE__) ) );
}
add_action( \'init\', \'register_custom_styles\' );

//create admin sub menu - admin>appearance>styles
add_action(\'admin_menu\', \'my_custom_submenu\');
function my_custom_submenu() {
    add_submenu_page( \'themes.php\', \'Styles\', \'Styles\', \'manage_options\', \'styles\', \'my_custom_submenu_page\' );
}

// register option
add_action( \'admin_init\', \'elh_register_settings\' );
function elh_register_settings() {
    register_setting( \'elh-settings-group\', \'user_select_styles\' );
}

//create admin page for admin>appearance>styles
function my_custom_submenu_page() {
?>
<div>
  <h2>Select Style</h2>
  <form method="post" enctype="multipart/form-data" action="options.php">
    <?php
        settings_fields( \'elh-settings-group\' );
        do_settings_sections( \'elh-settings-group\' );
        $selected = (get_option(\'user_select_styles\') != \'\') ? get_option(\'user_select_styles\') : \'\';
    ?>

     <!--this option to wp_enqueue_style(\'style1\')-->
     <label>  <input type="radio" name="user_select_styles" value="style1" <?php if($selected == "style1") echo "checked"; ?> />  <!--<img src="//path-to-style1-img" /> -->  Style1  </label>  

     <br /> 

     <!--this option to wp_enqueue_style(\'style2\')-->
     <label>  <input type="radio" name="user_select_styles" value="style2" <?php if($selected == "style2") echo "checked"; ?> />  <!--<img src="//path-to-style2-img" /> --> Style2  </label>  

     <br /> 

     <!--this option to wp_enqueue_style(\'style3\')-->
     <label>  <input type="radio" name="user_select_styles" value="style3" <?php if($selected == "style3") echo "checked"; ?> />  <!--<img src="path-to-style3-img" /> --> Style3  </label>  

     <br /> 

     <?php submit_button(); ?>

  </form>

  <h2><?php if($selected != \'\') printf("You have selected %s.", $selected); ?></h2>

</div>
<?php
}

function enqueue_my_css() {

    $selected = (get_option(\'user_select_styles\') != \'\') ? get_option(\'user_select_styles\') : \'\';
    if($selected != \'\')
        wp_enqueue_style( $selected , plugins_url( \'/css/\'.$selected.\'.css\', (__FILE__) ) );

}
add_action(\'wp_enqueue_scripts\', \'enqueue_my_css\', 99);
我不知道我是否应该选择自己的答案。

SO网友:Mahavar Hitesh

在wordpress主题中,您可以添加选项面板,您不能注册脚本或您可以添加的wp\\u enqueue\\u脚本

/*-----------------------------------------------------------*
/*          THEME OPTION ADMIN PRINT SCRIPT EXAMPLE
/*-----------------------------------------------------------*/

add_action( \'admin_menu\', \'ease_print_script\' );
function ease_print_script() {      
    add_action( \'admin_print_scripts-\' . $ease_options_page, \'ease_print_scripts\' );
}
function ease_print_scripts() {
    wp_enqueue_style( \'bootstrap\', get_template_directory_uri().\'/framework/css/bootstrap.css\');        
    wp_enqueue_style( \'bootstrap.vertical-tabs\', get_template_directory_uri().\'/framework/css/bootstrap.vertical-tabs.css\');
    wp_enqueue_script( \'script\', get_template_directory_uri().\'/framework/js/script.js\');
    wp_enqueue_script( \'bootstrap\', get_template_directory_uri().\'/framework/js/bootstrap.js\');         
}   
您的选项面板页面在此处创建了代码。。

/*-----------------------------------------------------------*
/*          THEME OPTION PAGE
/*-----------------------------------------------------------*/
add_action( \'admin_init\', \'ease_add_options_1\' );
function ease_add_options_1() {
    // Register new options
    register_setting( \'ease_options\', \'ease_options\', \'ease_options_validate\' );
}
/*-----------------------------------------------------------*
/*          THEME OPTION ADMIN IN MENU
/*-----------------------------------------------------------*/
add_action( \'admin_menu\', \'ease_add_page_1\' );
function ease_add_page_1() {
    /**
     *  IF you want to add in wordpress ==  menu page OR add theme page ==
     */

    // add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
    // MENU PAGE
    $ease_options_page = add_menu_page( \'__ease\', \'Theme Option\', \'manage_options\', \'__ease\', \'yout_code_here\' );

    // THEME PAGE
    // add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
    // $ease_options_page = add_theme_page( \'ease\', \'Theme Option\', \'manage_options\', \'ease\', \'ease_options_page\' );

}
function yout_code_here(){
    ?>
    <h2>Select Style</h2>
    <form method="post" action="options.php">
      <!--this option to wp_enqueue_style(\'style1\')-->
      <label>
        <input type="radio" name="myoption[radio1]" value="style1" />
        <img src="//path-to-style1-img" /> Style1 </label>
      <br />
      <!--this option to wp_enqueue_style(\'style2\')-->
      <label>
        <input type="radio" name="myoption[radio1]" value="style2" />
        <img src="//path-to-style2-img" /> Style2 </label>
      <br />
      <!--this option to wp_enqueue_style(\'style3\')-->
      <label>
        <input type="radio" name="myoption[radio1]" value="style3" />
        <img src="path-to-style3-img" /> Style3 </label>
      <br />
      <?php submit_button(); ?>
    </form>
    <?php 
}

结束

相关推荐

Multisite switch to blog

在我开始这个问题之前,我很抱歉,因为我在本地工作,所以我没有任何代码可以显示。我有一个Wordpress多站点,有3个站点-“site-1”、“site-2”、“site-3”。它们都导入相同的导航部分。php”,从模板文件夹。导航是硬编码的,但动态地从“site-2”中为site-2按钮提取内容在导航部分的开头。php“我获取当前博客id,然后切换到“site-2”。 $the_blog_id = get_current_blog_id(); switch_to_blog(2);&