这是Wordpress新手的问题。我想在我的网站登陆页面中添加一个按钮,链接到用户正在访问的浏览器平台的应用程序的最新版本--类似于火狐在其网站上的内容: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]从页面调用这些短代码。但之后呢?重定向定义在哪里?我只需要有人帮我把这些点连接起来。
为其他试图解决这一问题的人提供更新。我就是这么做的。
Functions.php
在子主题的functions.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>请注意,download-btn样式还必须在.css文件中定义,才能显示为按钮。
其他重定向的东西可以用几种方式来处理。在服务器管理员的建议下,我最终使用了两个.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 / mobile超链接的URL拼写的人,我有一个单独的WordPress页面,其中包含完整的下载列表。如果您执行单独的.php路由,请确保您有不同的拼写,否则.php文件结构将接管,您不会看到您的WordPress页面。
发布于 2016-05-09 22:20:25
1) .htaccess文件中的短代码和重定向
您可以创建一个具有两个属性的自关闭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短代码也有两个参数,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参数添加或删除../的路径,这取决于激活短代码的页面路径。它与域名…不能正常工作
https://stackoverflow.com/questions/37125959
复制相似问题