Wordpress新手问题在这里。我想在我的网站的登录页面上添加一个按钮,其中包含指向用户正在访问的浏览器平台的最新版本的应用程序的链接 - 类似于Firefox在其网站上具有的内容:https://www.mozilla.org/en-US/firefox/new/
因此,URL 将如下所示: http://www.example.com/downloads/osx
,但我希望它重定向到更具体的东西,例如 http//www.example.com/myapp_1.6.3.dmg
(这样我就不必在每次发布新版本时更新我的函数(。
这通常是怎么做到的?看起来我可以编写一个短代码来执行浏览器嗅探工作,然后使用 [bracket syntax]
从页面调用短代码。但那又如何?重定向在哪里定义?我只需要有人在这里为我连接点。
为试图解决这个问题的其他人更新。这就是我所做的。
功能.php
在函数中.php对于我的子主题,我添加了一个函数,根据浏览器报告的操作系统生成 URL,并在 init 事件中注册了该函数:
add_action('init', 'register_my_shortcodes');
function register_my_shortcodes() {
add_shortcode( 'download_os_button', 'download_os_button_shortcode' );
}
function download_os_button_shortcode() {
// Set the URL to whatever OS the browser is telling us (this can be spoofed)
if ( stristr( $_SERVER['HTTP_USER_AGENT'],"mac") ) {
$url = 'downloads/osx';
$text = 'Free Download - Mac';
} elseif ( stristr( $_SERVER['HTTP_USER_AGENT'],"windows") ) {
$url = 'downloads/windows';
$text = 'Free Download - Windows';
} else {
// Linux or mobile -- just point them to the general downloads page
$url = 'download/';
$text = 'Downloads';
}
// Return this as a hyperlink
return '<a class=download-btn href=' . $url . '>' . $text . '</a>';
}
WordPress页面简码
现在,[download_os_button]
的短代码可以放置在WordPress页面中,并且会发出以下内容之一,具体取决于操作系统:
<a class="download-btn" href="http://www.example.com/downloads/windows">Free Download - Windows</a>
<a class="download-btn" href="http://www.example.com/downloads/osx">Free Download - Mac</a>
<a class="download-btn" href="http://www.example.com/download/">Downloads</a>
请注意,还必须在.css文件中定义download-btn
样式,才能使其看起来像按钮。
其余的重定向内容可以通过几种方式处理。在我们的服务器管理员的建议下,我最终使用两个.php文件(每个平台一个(进行重定向; 分别.../httpdocs/downloads/osx/index.php
和.../httpdocs/downloads/windows/index.php
。osx 目录中的那个看起来像这样:
<?php
header('Location: http://www.example.com/myapp_1.6.3.dmg');
?>
它要求每次发布产品时,这两个文件中的重定向都会获得更新。
此外,对于那些注意到Linux/移动超链接的URL拼写的人,我有一个单独的WordPress页面,其中包含完整的下载列表。如果您执行单独的.php路线,请确保您有不同的拼写,否则.php文件结构将接管,您将看不到您的WordPress页面。
1( .htaccess 文件中
的短代码和重定向
您可以创建一个具有 2 个属性的自闭式 wordpress 简码。text
和url
:
// Copy this Shortcode to the function.php file located in your active child theme or theme.
if( !function_exists('download_dmg_shortcode') ) {
function download_dmg_shortcode( $atts ) {
// Attributes
extract( shortcode_atts(
array(
'url' => '',
'text' => '',
), $atts )
);
// Code
return '<a class="button-dmg" href="' . $url . '">' . $text . '</a>';
}
add_shortcode( 'dload_dmg', 'download_dmg_shortcode' );
}
您将以这种方式使用它:
[dload_dmg url="http://www.example.com/downloads/osx" text="Download my App" /]
您可以在代码中更改类名并添加更多 html 标签,以满足您的需求。
您可以通过不同的方式进行重定向,但您可以在.htaccess
文件中执行此操作,例如:
<IfModule mod_rewrite.c>
RewriteEngine On
RedirectMatch permanent /downloads/osx http//www.example.com/myapp_1.6.3.dmg
# You can add this to force download for .dmg files:
AddType application/octet-stream .dmg
</IfModule>
2(在onclick事件中包含javascript的简码(最佳解决方案(
这个自闭式wordpress简码还有2个参数,text
和url
。但这次url
是重定向链接:
// Copy this Shortcode to the function.php file located in your active child theme or theme.
if( !function_exists('download_dmg_shortcode') ) {
function download_dmg_shortcode( $atts ) {
// Attributes
extract( shortcode_atts(
array(
'url' => '',
'text' => '',
), $atts )
);
// Code
return '<a class="button-dmg" href="http://www.example.com/downloads/osx" onclick="event.preventDefault(); window.location.href = '''.$url.''';">' . $text . '</a>';
}
add_shortcode( 'dload_dmg', 'download_dmg_shortcode' );
}
您将以这种方式使用它: [dload_dmg url="../myapp_1.6.3.dmg" text="Download my App" /]
您需要微调url
参数添加或删除../
的路径,具体取决于激活短代码的页面路径。它无法与域名正常工作...
简码生成器