0

This plugin code was working fine on apache server with php5 but now i am installing on nginx with php7 & wordpress 5.x, its having permalink error (showing 404 error). It's working with wordpress plain permalink (only not showing 404 error i.e. new page is opening) but not with custom permalink.

Plugin overview: This plugin checks links in a wp post/page and onclick of those link it opens in a new page with that link. Basically it does two step process for a link instead of directly opening the link in a post.

    <?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();
?>
  • try to flush permalinks. Go to Settings > Permalinks and then hit save button. – maverick Apr 26 at 8:15
  • flushing permalinks doesn't help. Problem is with the code. – AvtarCode Apr 26 at 8:18
  • Do you have define('WP_DEBUG', true); and are there any entries in your error_log file? – Alexander Holsgrove Apr 26 at 8:22
  • 1
    Are other (pretty) permalinks working? Could be an NGINX issue, you need to configure this manually and can't rely on WordPress' .htaccess (which is why many use NGINX only as a proxy to Apache) – kero Apr 26 at 9:35
  • 1
    I have submitted an answer. I am saying the same thing as @kero but I have provided a workaround you might find interesting. Please have a look. – Ted Stresen-Reuter May 1 at 9:53
1

Please remember that you are more likely to get a helpful response if you ask a specific question. If the question is "what PHP do I have to change to make it work?" that would be better but most programmers can provide helpful answers if you describe the functionality you require (vs. just posting code from an unnamed plugin).

There are many things to keep in mind regarding the code you've sent:

  1. Either there is code missing (probable) or there is an issue with the plugin (in general). You'll note that the method rewrite_rules requires a parameter called $rules and yet the filter doesn't seem to pass a value to the method (but I could be missing something here).

  2. The plugin is failing because mod_rewrite is an Apache module and it is not supported by nginx. This plugin writes some custom mod_rewrite rules to .htaccess and since nginx doesn't recognize the mod_rewrite apache module nor does it read .htaccess files, it will never work and would require a complete rewrite.

As a workaround, you might consider adding a simple filter in functions.php on the_content to rewrite the URLs to recreate the functionality that this plugin appears to provide. Here is an example:

add_filter('the_content', function( $input ) {
  $output = $input;
  $re = '#href=.[^"\']+.#';
  $replace = '$1 target="_blank"';
  $output = preg_replace($re, $replace, $output);

  return $output;
}, 1);

Unfortunately, because I don't know what this plugin is intended to do, I can't provide a more detailed example but in all honesty, you're probably better served by finding a URL rewriting plugin that does work under nginx.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.