如何从子主题中删除 WordPress 主题选项

发布于 2024-12-28 16:19:28 字数 396 浏览 1 评论 0原文

我通过为二十一主题编写一个子主题来开始新项目。我很少设计新主题来使用二十一主题中内置的任何选项(例如背景颜色等)。这些剩余的选项并没有真正造成任何伤害,但我想摆脱它们,因为它们没有做任何事情。

问题是主题选项是在父主题的functions.php中声明的,它与子主题的functions.php文件一起加载(而不是代替)(所以我可以删除它们,但它们会在下次升级时回来)。

有没有办法从我的子主题中删除或禁用这些主题选项?也许类似于“remove_options()”函数?或者也许可以达到这种效果的东西?换句话说,问题是是否可以在不删除/覆盖添加它们的原始函数的情况下删除 theme_options。

我确信经过足够的推敲,我可以使用 CSS 或 javascript 隐藏该选项...但来吧。

I start new projects by writing a child theme for the twentyeleven theme. I rarely design the new theme to use any of the options built into the twentyeleven theme (such as background color, etc). Those residual options don't really hurt anything, but I'd like to get rid of them since they don't do anything.

The trouble is that the theme options are declared in the parent theme's functions.php, which is loaded along with (not instead of) the child theme's functions.php file (so I could delete them but they'll come back next upgrade).

Is there a way to remove or disable those theme options from my child theme? Perhaps something along the lines of a "remove_options()" function? Or perhaps something that would achieve that effect? In other words, the question is whether theme_options can be removed WITHOUT deleting/overriding the original function that added them.

I'm sure with enough puttering, I could hide the option with CSS or javascript... but c'mon.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撩动你心 2025-01-04 16:19:28

经过第二轮挖掘...

这非常简单!

您可以从此处开始追溯我的步骤,但代码非常不言自明:

add_action( 'init', 'remove_crap' );
    function remove_crap() {

    remove_custom_image_header();
    remove_custom_background();
    remove_theme_support('post-formats');
}

您可以在法典中查找这些内容。 Remove_theme_support 采用标识各种选项的几个字符串之一(除了后格式之外)。我遇到的唯一问题是它们需要从钩子中调用(你不能将它们转储到functions.php中)。我正在使用 init 但可能还有另一种更合适。

我唯一还没弄清楚的是如何删除出现在外观下的“主题选项”页面链接。我知道它是通过 add_theme_page() 添加的,但似乎没有方便的 remove_theme_page()

更新:我找到了!
这是非常缺乏记录的,但最终它很容易做到:

add_action('admin_init', 'remove_twentyeleven_theme_options', 11);
     function remove_twentyeleven_theme_options() {
 remove_submenu_page('themes.php', 'theme_options');
}

在我的示例中,“themes.php”针对外观菜单,“theme_options”是二十一主题中使用的 menu_slug。显然,这些参数会根据您正在编辑的菜单或子菜单而有所不同。 此页面将为您指明正确的方向。

ps:以下是如何从父主题中删除您不想使用的模板:
是对于我的确切问题来说并不重要,但它与任何尝试做我正在做的事情的人密切相关并且可能有用。

After a second round of digging...

This is TOTALLY easy!

You can retrace my steps by starting here, but the code is pretty self-explanatory:

add_action( 'init', 'remove_crap' );
    function remove_crap() {

    remove_custom_image_header();
    remove_custom_background();
    remove_theme_support('post-formats');
}

You can look these up in the codex. Remove_theme_support takes one of several strings that identify various options (besides just post-formats). The only issue I encountered is that they need to be called from a hook (you can't just dumpt them into functions.php). I'm using init but there's probably another one that's more appropriated.

The only thing I still haven't figured out is how to remove the "Theme Options" page link that appears under Appearances. I know it's added with add_theme_page() but there doesn't seem to be a handy remove_theme_page().

UPDATE: I found it!
This is VERY poorly documented, but in the end it's quite easy to do:

add_action('admin_init', 'remove_twentyeleven_theme_options', 11);
     function remove_twentyeleven_theme_options() {
 remove_submenu_page('themes.php', 'theme_options');
}

In my example, 'themes.php' targets the Appearances menu and 'theme_options' is the menu_slug used in the twentyeleven theme. Obviously these parameters will differ depending on which menu or submenu you're editing. This page will point you in the right direction.

ps: Here's how to get rid of templates from the parent theme that you don't want to use:
THIS isn't essential to my exact question, but it's closely related and probably useful to anyone who's trying to do what I'm doing.

黑寡妇 2025-01-04 16:19:28

从子主题中删除父主题中添加的主题支持的正确方法是在 after_setup_theme 操作中调用remove_theme_support,该操作的优先级低于父主题的优先级。

子主题中的functions.php文件会在父主题之前被调用,因此如果您使用after_setup_theme的默认优先级,则子主题的after_setup_theme最终会在父主题之前被调用,因此您最终会删除不存在的主题您的孩子中的主题支持,只需将其从运行 after_setup_theme 的父级添加回来。

因此,通过添加具有较低优先级的子操作,您可以确保在父操作调用相同操作之后调用它。

所以:

// added to child's functions.php    

add_action( 'after_setup_theme', 'child_after_setup_theme', 11 ); 
// Parent theme uses the default priority of 10, so
// use a priority of 11 to load after the parent theme.

function child_after_setup_theme()
{
    remove_theme_support('custom-background');
    remove_theme_support('custom-header');
    remove_theme_support('post-formats');
    // ... etc.
}

对于二十一主题,你也可以覆盖你孩子的functions.php中的整个二十一设置函数,但这是实现这一点的相当不巧妙的方法。

The correct way to remove theme support added in a parent theme from the child theme is to make the call to remove_theme_support in an after_setup_theme action called with a lower priority than that of the parent.

The functions.php file from a child themes is called immediately before that of the parent theme, so if you use the default priority for after_setup_theme, the child's after_setup_theme ends up getting called before that of the parent, so you end up removing non-existant theme support in your child, only to have it added back in from the parent running after_setup_theme.

So by adding your child action with a lower priority, you can ensure it gets called after the parent's call to the same action.

So:

// added to child's functions.php    

add_action( 'after_setup_theme', 'child_after_setup_theme', 11 ); 
// Parent theme uses the default priority of 10, so
// use a priority of 11 to load after the parent theme.

function child_after_setup_theme()
{
    remove_theme_support('custom-background');
    remove_theme_support('custom-header');
    remove_theme_support('post-formats');
    // ... etc.
}

In the case of the twentyeleven theme, you could also just over-ride the entire twentyeleven_setup function in your child's functions.php, but that is a rather unsubtle method of achieving this.

怪异←思 2025-01-04 16:19:28

不幸的是,在 WordPress 的情况下,主题继承的工作方式是子主题函数只是“添加”到父主题函数中。

与style.css不同,子主题的functions.php不会覆盖父主题的对应部分。相反,它是在父级的functions.php 之外加载的。 (具体来说,它是在父文件之前加载的。)(1)

因此,在直接回答上面的问题时,看起来(2)这对于 WordPress 处理主题和子主题的方式来说可能是不可能的。

就我个人而言,我不会担心functions.php 文件中存在这些额外的函数或变量。

  1. http://codex.wordpress.org/Child_Themes
  2. http://wordpress.org/support/topic/theme-options-in-child-theme

Unfortunately, the way that theme inheritance works in Wordpress' case is that child theme functions are just "added on" to the parent theme functions.

Unlike style.css, the functions.php of a child theme does not override its counterpart from the parent. Instead, it is loaded in addition to the parent’s functions.php. (Specifically, it is loaded right before the parent’s file.)(1)

So, in direct answer to your question above, it looks(2) like this might not possible with the way WordPress handles themes and child themes.

Personally, I wouldn't worry about having those extra functions or variables in the functions.php file.

  1. http://codex.wordpress.org/Child_Themes
  2. http://wordpress.org/support/topic/theme-options-in-child-theme
抹茶夏天i‖ 2025-01-04 16:19:28

这是一个旧线程,所以我只想添加是否有人进入这个地方并想要答案。我通过获取该确切文件、制作副本并将其添加到子主题来解决子主题。我有“高级代码编辑器”插件,所以我不需要进入 FTP。复制要编辑的特定文件,在子主题中创建具有相同名称和内容的新工作表,然后在其中进行所需的编辑。它将首先获取子主题文件,然后您的网站将被更新。

This is an old thread so I just want to add if someone enters this place and want an answer. I solve the child theme by taking that exact file, making a copy and adding it to child theme. I have the plugin "advanced code editor" so I dont need to go in FTP. Copy the specific file you want to edit, make a new sheet in child theme with same name and content, and then do the edits you want there. It will fetch the child theme files first and your site will be updated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文