不带插件的 WordPress 移动主题切换器(用户代理和 Cookie)

发布于 2024-10-04 00:13:28 字数 383 浏览 0 评论 0原文

我发现了一些插件,例如 WP Mobile Pack,但我想组合我自己的非常简单的主题切换器。 (只是不想有任何膨胀。)

我有一个常规的 WordPress 主题,我还创建了一个 WordPress Mobile 主题。这些都是标准 WP 主题,可以在仪表板中激活。

如何将某些用户代理直接定向到移动主题? (我不需要移动域、子域或尾随目录。)www.example.com 应该是任何访问者的域。

一个问题是,我还想提供一个链接让用户切换回来。例如,iOS 设备应该转到移动主题。但如果他们使用的是大型 iPad,他们可能只想使用常规网站。使用某种 cookie 覆盖默认的 .htaccess 或 PHP 重定向会很有用。

任何专门针对 WP 和处理 WP 主题切换的帮助将不胜感激。

I found some plugins like WP Mobile Pack, but I want to put together my own very simple theme switcher. (Just don't want any bloat.)

I have a regular WordPress theme and I also created a WordPress Mobile theme as well. These are both standard WP themes that can be activated in the dashboard.

How can I direct certain user agents directly to the mobile theme? (I don't want a mobile domain, sub-domain, or trailing directory.) www.example.com should be the domain for any visitor.

The one catch is, I also want to offer a link to let users switch back. For instance, iOS devices should go to the Mobile Theme. But if they are on the large iPad, they may want to simply use the regular site. Having some sort of cookie override the default .htaccess or PHP redirection would be useful.

Any help, specifically targeted at WP and handling WP theme switching would be truly appreciated.

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

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

发布评论

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

评论(2

盛夏已如深秋| 2024-10-11 00:13:28

我将使用 2 个样式表来主题化页面,并检测浏览器并根据检测到的浏览器提供正确的 css。看看 PHP 的 'get_browser' 函数 http://php.net/ Manual/en/function.get-browser.php

然后只有一个 if 语句:

if (preg_match('/mobile/i', $u_agent)) {
    $css = 'mobile.css';
}

然后加载 $css

希望这是有意义的。

I would use 2 stylesheets to theme the page, and detect the browser and serve up the right css depending on what the browser is detected as. Have a look at the 'get_browser' function of PHP http://php.net/manual/en/function.get-browser.php

then just have an if statement:

if (preg_match('/mobile/i', $u_agent)) {
    $css = 'mobile.css';
}

and then just load the $css

Hope that makes some sense.

So要识趣 2024-10-11 00:13:28

您必须创建一个插件来触发切换主题操作。这是一个参考:

https://codex.wordpress.org/Function_Reference/switch_theme

我认为你可以尝试“after_setup_theme”挂钩。

You will have to create a plugin, that fires the switch theme action. Here is a reference:

https://codex.wordpress.org/Function_Reference/switch_theme

I think you can try the 'after_setup_theme' hook.

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