doushan2224 2015-01-06 12:48
浏览 89
已采纳

Wordpress - 使用子主题添加到Redux主题选项

I am using a wordpress theme that has it's own framwork which is based upon redux framework I think. I am modifying this theme using a child theme. I want to add to the Theme Options in the backend and I found a function within the files of the parent theme that seems to be exactly what I need:

/*
 *
 * Custom function for filtering the sections array. Good for child themes to override or add to the sections.
 * Simply include this function in the child themes functions.php file.
 *
 * NOTE: the defined constansts for URLs, and directories will NOT be available at this point in a child theme,
 * so you must use get_template_directory_uri() if you want to use any of the built in icons
 *
 */
function add_another_section($sections){
    //$sections = array();
    $sections[] = array(
        'title' => __('A Section added by hook', 'swift-framework-admin'),
        'desc' => __('<p class="description">This is a section created by adding a filter to the sections array. Can be used by child themes to add/remove sections from the options.</p>', 'swift-framework-admin'),
        // Redux ships with the glyphicons free icon pack, included in the options folder.
        // Feel free to use them, add your own icons, or leave this blank for the default.
        'icon' => trailingslashit(get_template_directory_uri()) . 'options/img/icons/glyphicons_062_attach.png',
        // Leave this as a blank section, no options just some intro text set above.
        'fields' => array()
    );

    return $sections;
}
//add_filter('redux-opts-sections-twenty_eleven', 'add_another_section');

I have added this function to my child theme's functions.php and uncommented the add_filter. However, this does not seem to work and no new section has been added.

I came across this discussion elsewhere which suggests the name of the function needs to be changed (I was getting the same error mentioned on there). I have done that and it still won't work.

Here is what I have in my child theme functions.php

function add_another_section_bl($sections){
    $sections = array();
    $sections[] = array(
        'title' => __('A Section added by hook', 'swift-framework-admin'),
        'desc' => __('<p class="description">This is a section created by adding a filter to the sections array. Can be used by child themes to add/remove sections from the options.</p>', 'swift-framework-admin'),
        // Redux ships with the glyphicons free icon pack, included in the options folder.
        // Feel free to use them, add your own icons, or leave this blank for the default.
        'icon' => trailingslashit(get_template_directory_uri()) . 'options/img/icons/glyphicons_062_attach.png',
        // Leave this as a blank section, no options just some intro text set above.
        'fields' => array()
    );

    return $sections;
}
add_filter('redux-opts-sections-twenty_eleven', 'add_another_section_bl');

I wasn't sure if the filter name 'redux-opts-sections-twenty_eleven' needs editing as it mentions the twenty eleven theme. I have tried it with different theme names at the end instead of twenty_eleven and that had no effect.

Any help would be greatly appreciated! On a side note I have been able to accomplish adding new options to Theme Options by copying over the whole framwork folder into my child theme and defining the path to the framework in the child theme's functions.php. I just felt that there should be a much slicker, neater way to achieve this and I thought that function sounded perfect.

Many thanks.

  • 写回答

1条回答 默认 最新

  • duanmaifu3428 2015-01-07 02:31
    关注

    Lead dev of Redux Framework here. This solution only works if you're using Redux Framework 3.1+. If you have an older version, install the Redux Framework plugin and it will override the version inside your theme.

    First go to the current option panel. Open up a javascript console (use chrome or firefox) and type: redux.args.opt_name. That will echo out a name. Copy that and paste it into this function replacing OPT_NAME with the name that was echo'd out:

    function add_another_section_bl($sections){
        $sections = array(); // Delete this if you want to keep original sections!
        $sections[] = array(
            'title' => __('A Section added by hook', 'swift-framework-admin'),
            'desc' => __('<p class="description">This is a section created by adding a filter to the sections array. Can be used by child themes to add/remove sections from the options.</p>', 'swift-framework-admin'),
            // Redux ships with the glyphicons free icon pack, included in the options folder.
            // Feel free to use them, add your own icons, or leave this blank for the default.
            'icon' => trailingslashit(get_template_directory_uri()) . 'options/img/icons/glyphicons_062_attach.png',
            // Leave this as a blank section, no options just some intro text set above.
            'fields' => array()
        );
    
        return $sections;
    }
    // In this example OPT_NAME is the returned opt_name.
    add_filter("redux/options/OPT_NAME/sections", 'add_another_section_bl');
    

    Good luck!

    ** UPDATE **

    Also with the Redux API you can easily add a new section that way.

    Redux::addSection(array(
            'title' => __('A Section added by hook', 'swift-framework-admin'),
            'desc' => __('<p class="description">This is a section created by adding a filter to the sections array. Can be used by child themes to add/remove sections from the options.</p>', 'swift-framework-admin'),
            // Redux ships with the glyphicons free icon pack, included in the options folder.
            // Feel free to use them, add your own icons, or leave this blank for the default.
            'icon' => trailingslashit(get_template_directory_uri()) . 'options/img/icons/glyphicons_062_attach.png',
            // Leave this as a blank section, no options just some intro text set above.
            'fields' => array()
        ))
    

    That makes it a wee bit easier using our API I believe we released in Redux 3.2...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!