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

Wordpress - Adding to Redux Theme Options using Child Theme

我正在使用一个 wordpress 主题,它有自己的框架,我认为它基于 redux 框架。我正在使用子主题修改此主题。我想添加到后端的主题选项,我在父主题的文件中发现了一个函数,它似乎正是我需要的:

/*
 *
 * 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');

我已将此功能添加到我的子主题 functions.php 并取消注释 add_filter。但是,这似乎不起作用,也没有添加新的部分。

我遇到了这个讨论 elsewhere,它建议需要更改函数的名称(我遇到了上面提到的相同错误)。我已经这样做了,但还是不行。

这是我的儿童主题 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');

我不确定过滤器名称 'redux-opts-sections-twenty_eleven' 是否需要编辑,因为它提到了二十一世纪的主题。我在最后尝试使用不同的主题名称而不是 twenty_eleven,但没有效果。

如有任何帮助,我们将不胜感激!在旁注中,我已经能够通过将整个框架文件夹复制到我的子主题中并在子主题的 functions.php 中定义框架的路径来完成向主题选项添加新选项。我只是觉得应该有一种更巧妙、更简洁的方法来实现这一点,我认为这个功能听起来很完美。

非常感谢。

在这里领导 Redux 框架的开发。此解决方案仅在您使用 Redux Framework 3.1+ 时有效。如果您有旧版本,请安装 Redux Framework 插件,它将覆盖您主题中的版本。

首先进入当前选项面板。打开 javascript 控制台(使用 chrome 或 firefox)并输入:redux.args.opt_name。这将回显一个名字。将其复制并粘贴到此函数中,用回显的名称替换 OPT_NAME

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');

祝你好运!

** 更新 **

同样使用 Redux API 你可以轻松地添加一个新的部分。

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()
    ))

使用我们的 API 我相信我们在 Redux 3.2 中发布了...

如果您在插件中使用 redux 选项,您的过滤器将无法在主题中使用 为此,请确保将代码放入插件中。

这里是适用于主题和插件的代码!

function add_social_media_options($sections) {
$sections[] = array(
    // 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' => 'el-icon-wrench',
    'title' => esc_html__('Social Media Settings', 'textdomain'),
    'desc' => esc_html__('These are settings social media link', 'textdomain'),
    'fields' => array(
        array(
            'id' => 'facebook_link',
            'type' => 'text',
            'url' => true,
            'title' => __('Facebook link', 'textdomain'),
            'compiler' => 'true',
            //'mode' => false, // Can be set to false to allow any media type, or can also be set to any mime type.
            'desc' => __('', 'textdomain'),
            'default' => ''
        ),
        array(
            'id' => 'twitter_link',
            'type' => 'text',
            'url' => true,
            'title' => __('Twitter link', 'textdomain'),
            'compiler' => 'true',
            //'mode' => false, // Can be set to false to allow any media type, or can also be set to any mime type.
            'desc' => __('', 'textdomain'),
            'default' => ''
        ),

    )
);

return $sections;
}

add_filter("redux/options/redux_demo/sections", 'add_social_media_options');