这个插件代码在使用php5的apache服务器上运行良好,但现在我正在使用php7和;wordpress 5。x、 它有permalink错误(显示404错误)。它使用wordpress普通永久链接(只显示404错误,即新页面正在打开),但不使用自定义永久链接。
插件概述:该插件检查wp帖子/页面中的链接,单击这些链接,它将在新页面中打开该链接。基本上,它对链接执行两个步骤,而不是在帖子中直接打开链接。
<?php
if (!class_exists(\'wordpress_link_page\')) {
class wordpress_link_page {
var $options_name = \'wplm_options\';
var $options;
var $myfile = \'\';
var $myfolder = \'\';
var $url_pattern =
\'@
(<a[\\s]+[^>]*href\\s*=\\s*) # \\1
(?: "([^">]+)" | \\\'([^\\\'>]+)\\\' ) # \\2 or \\3 is URL
([^<>]*>) # \\4
(.*?) # \\5 Anchor text
(</a>) # \\6
@xsi\';
function wordpress_link_page(){
$this->options = get_option($this->options_name);
$this->myfile = str_replace(\'\\\\\', \'/\',__FILE__);
$this->myfile = preg_replace(\'/^.*wp-content[\\\\\\\\\\/]plugins[\\\\\\\\\\/]/\', \'\', $this->myfile);
$this->myfolder = basename(dirname(__FILE__));
add_action(\'activate_\'.$this->myfile, array(&$this,\'activation\'));
add_filter(\'the_content\', array(&$this,\'content_filter\'), -200); //Rewrite URL before all other plugins or hooks
add_filter(\'mod_rewrite_rules\', array(&$this,\'rewrite_rules\'));
add_action(\'admin_menu\', array(&$this,\'options_menu\'));
add_action(\'admin_print_scripts\', array(&$this,\'load_admin_scripts\'));
}
// Strip Slashes
function slashe($text){
if(get_magic_quotes_gpc())
return stripslashes($text);
else
return $text;
}
//
// Check if it\'s a page or single post
if( (is_page($post->ID) && $this->options[\'wplm_page\']) || (!is_page($post->ID) && $this->options[\'wplm_post\']))
{
// Final link
$flink=get_permalink($this->options[\'wplm_output\']);
if(!preg_match(\'#/$#\', $flink))
$flink.=\'/\';
$flink .= str_replace(\'%2F\', \'/\', urlencode($niceurl));
//
// Add cloaked link in onclick
if($this->options[\'wplm_onclick\'])
{
if($this->options[\'wplm_window\'])
$flink = "window.open(\'$flink\'); return false;";
else
$flink = "location.href=\'$flink\'; return false";
$link = \'<a href="\'.$url.\'" onclick="\'.$flink.\'">\'.$urltitle.\'</a>\';
}else{
$window=\'\';
if($this->options[\'wplm_window\'])
$window = "target=\'_blank\'";
$link = \'<a href="\'.$flink.\'" \'.$window.\'>\'.$urltitle.\'</a>\';
}
}
}
return $link;
}
function content_filter($content){
$content = preg_replace_callback($this->url_pattern, array(&$this,\'rewrite_link\'), $content);
return $content;
}
function activation(){
global $wp_rewrite;
if(!is_array($this->options)){
//by default, add the current domain name to exclusion list
$parts=@parse_url(get_option(\'siteurl\'));
$exclusions=array();
if($parts && isset($parts[\'host\'])){
$exclusions[]=$parts[\'host\'];
}
//Force WP to regenerate the .htaccess file and insert the plugin\'s rules into it.
$wp_rewrite->flush_rules();
}
// Load required JS
function load_admin_scripts(){
wp_enqueue_script(\'jquery\');
wp_enqueue_script(\'jquery-ui-core\');
wp_enqueue_script(\'jquery-ui-tabs\');
}
function rewrite_rules($rules){
global $wp_rewrite;
$prefix = $this->options[\'prefix\'];
$siteurl = get_option(\'siteurl\');
$pageurl = get_permalink($this->options[\'wplm_output\']);
$page_data = get_page($this->options[\'wplm_output\']);
$pagename = $page_data->post_name;
$myrules="\\n# Wordpress Link BEGIN\\n";
$myrules.="<IfModule mod_rewrite.c>\\n";
$myrules.="RewriteEngine On\\n";
$myrules.="RewriteRule ^$pagename/(.+)$ $pageurl?url=$1 [QSA,L]\\n";
$myrules.="</IfModule>\\n";
$myrules.="# Wordpress Link ENDS\\n\\n";
$rules = $myrules.$rules;
return $rules;
}
function options_menu(){
$settings_hook = add_options_page(\'Link Page Settings\', \'Link Page\', \'manage_options\', \'wp-link-page-options\',array(&$this,\'options_page\'));
}
function options_page(){
global $wp_rewrite;
// Update Data from Form
if (isset($_GET[\'updated\']) && ($_GET[\'updated\'] == \'true\')) {
if(isset($_POST[\'Submit\']) && current_user_can(\'manage_options\')) {
// Settings
$this->options[\'wplm_type\'] = $_POST[\'wplm_type\'];
$this->options[\'wplm_enable\'] = isset($_POST[\'wplm_enable\']) ? 1 : 0;
$this->options[\'wplm_onclick\'] = isset($_POST[\'wplm_onclick\']) ? 1 : 0;
$this->options[\'wplm_window\'] = isset($_POST[\'wplm_window\']) ? 1 : 0;
$this->options[\'wplm_external\'] = isset($_POST[\'wplm_external\']) ? 1 : 0;
$this->options[\'wplm_post\'] = isset($_POST[\'wplm_post\']) ? 1 : 0;
$this->options[\'wplm_page\'] = isset($_POST[\'wplm_page\']) ? 1 : 0;
$this->options[\'wplm_google\'] = $this->slashe($_POST[\'wplm_google\']);
$this->options[\'wplm_excat\'] = array();
$this->options[\'wplm_expage\'] = array();
$this->options[\'wplm_exsite\'] = array();
$this->options[\'wplm_expostype\'] = array();
if(count($_POST[\'wplm_excat\'])){
foreach ($_POST[\'wplm_excat\'] as $idcat)
$tab[] = $idcat;
$this->options[\'wplm_excat\']=$tab;
}
if(count($_POST[\'wplm_expage\'])){
foreach ($_POST[\'wplm_expage\'] as $idpage)
$tab[] = $idpage;
$this->options[\'wplm_expage\']=$tab;
}
if(!empty($_POST[\'wplm_exsite\']))
$this->options[\'wplm_exsite\'] = explode("\\r\\n", trim($_POST[\'wplm_exsite\']));
if(!empty($_POST[\'wplm_expostype\']))
$this->options[\'wplm_expostype\'] = explode("\\r\\n", trim($_POST[\'wplm_expostype\']));
// Outgoing Page
$this->options[\'wplm_output\'] = $_POST[\'wplm_output\'];
$upage = array();
$upage[\'ID\'] = $_POST[\'wplm_output\'];
$upage[\'post_content\'] = $_POST[\'wplm_output_content\'];
wp_update_post($upage);
$this->options[\'wplm_code_iframe\'] = $this->slashe($_POST[\'wplm_code_iframe\']);
$this->options[\'wplm_code_link\'] = $this->slashe($_POST[\'wplm_code_link\']);
$this->options[\'wplm_site_url\'] = $this->slashe($_POST[\'wplm_site_url\']);
$this->options[\'wplm_logo\'] = $this->slashe($_POST[\'wplm_logo\']);
.
.
.
.
} //if class_exists()...
$wplm = new wordpress_link_page();
?>
SO网友:Ted Stresen-Reuter
请记住,如果你问一个特定的问题,你更有可能得到有用的回答。如果问题是“我必须更改什么PHP才能使其工作?”这会更好,但如果您描述所需的功能,大多数程序员都可以提供有用的答案(而不是仅仅发布来自未命名插件的代码)。
关于您发送的代码,有许多事情需要记住:
要么代码缺失(可能),要么插件存在问题(一般)。您会注意到rewrite_rules
需要一个名为$rules
然而,过滤器似乎没有将值传递给方法(但我可能在这里遗漏了一些东西)。
插件失败,因为mod\\u rewrite是一个Apache模块,nginx不支持它。这个插件编写了一些自定义的mod_重写规则。htaccess和nginx既不识别mod\\u rewrite apache模块,也不读取。htaccess文件,它永远不会工作,需要完全重写。
作为一种解决方法,您可以考虑在函数中添加一个简单的过滤器。php重写URL以重新创建此插件似乎提供的功能。以下是一个示例:
add_filter(\'the_content\', function( $input ) {
$output = $input;
$re = \'#href=.[^"\\\']+.#\';
$replace = \'$1 target="_blank"\';
$output = preg_replace($re, $replace, $output);
return $output;
}, 1);
不幸的是,因为我不知道这个插件打算做什么,所以我无法提供更详细的示例,但老实说,找到一个在nginx下工作的URL重写插件可能会更好。