WordPress.org

Ready to get started?Download WordPress

Forums

Dealing with Permalinks and Dynamic Code (8 posts)

  1. buckrogers
    Member
    Posted 3 years ago #

    I have a question on permalinks.
     
    I added some code to my template page.php.  The code checks for a specific page ID then generates dynamic content based on two inputs (state and town).
     
    I also have code in .htaccess to rewrite my the URL.  I request /consultants/NH/Manchester and it will translate to index.php?page_id=100&state=NH&town=Manchester.
     
    If I disable permalinks, this works fine.  If I enable permalinks, the browser gets a 302 redirect to the actual permalink for page_id 100.  In this case /category/consultants-directory.
     
    Can someone explain which function executes the 302 redirect.  I can change it to ignore the 302 if the page_id=100.  There could be a better way to do this.  Suggestions?

  2. Chrome Orange
    Member
    Posted 3 years ago #

    Did you ever resolve this, I'm trying to do something similar

    Thanks

  3. buckrogers
    Member
    Posted 3 years ago #

    Yes, but it's not a nice solution. I didn't have time to figure out how the permalink functions work. Or I couldn't figure it out after a week of late nights. I just needed this for one page ID. I changed it in wp-includes/canonical.php. I added one line to ignore the function for my page ID. A better way is to use a custom field if you have multiple pages you want this for:

    if ( $do_redirect ) {
    		//CHR this line will exclude permalink translation for a single page
                      //Use it to call a page with dynamic page generation code
    	    if ( get_query_var('page_id') == "1137" ) return false;
    		// CHR end modification for 31 directory
    
    		// protect against chained redirects
    		if ( !redirect_canonical($redirect_url, false) ) {
    			wp_redirect($redirect_url, 301);
    			exit();
    		} else {
    			// Debug
    			// die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) );
    			return false;
    		}
    	} else {
    		return $redirect_url;
    	}

    Remember to make a backup copy after the change. The next WordPress upgrade will overwrite this.

  4. Chrome Orange
    Member
    Posted 3 years ago #

    Thank you for the reply. Unfortunately I couldn't do it your way ao after further investigation (ycrta asking on the forum again) my prayers were answered

    http://wordpress.org/support/topic/permalinks-and-getting-the-id-which-file?replies=3

    points me to http://codex.wordpress.org/Function_Reference/WP_Rewrite

    What this means is I can have

    http://www.domain.com/your-town-is-TOWNNAME/

    convert to http://www.domain.com/index.php?pagename=your-town-is&town=TOWNNAME

    So I only need to write 1 page (your-town-is) and use some shortcode to GET the town variable and replace [town] with the variable

    Perfect solution for me.

  5. buckrogers
    Member
    Posted 3 years ago #

    Did you figure this out? It will be very helpful if you explain how you did it. I have a similar setup that translate /statelist to index.php?page_id=1137&input1=statelist in my .htaccess file. I then have dynamic code in my template page.php that generates the content.

    How do I get rid of the original permalink for page_id=1137?

    Thanks
    Chris

  6. Chrome Orange
    Member
    Posted 3 years ago #

    I did it by creating a new function in functions.php and editing the .htaccess file

    for the sake of this lets say that the permalink for your page_id 1137 is STATELIST and input1 is OHIO

    The user would click on a link http://www.DOMAIN.com/statelist-ohio/

    This goes in functions.php and converts the link into something wordpress can use.

    add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
    add_filter('query_vars','wp_insertMyRewriteQueryVars');
    add_filter('init','flushRules');
    
    // Remember to flush_rules() when adding rules
    function flushRules(){
    	global $wp_rewrite;
       	$wp_rewrite->flush_rules();
    }
    
    // Adding a new rule
    function wp_insertMyRewriteRules($rules)
    {
    	$newrules = array();
    	$newrules['(statelist-)(.+?)$'] = 'index.php?pagename=$matches[1]&input1=$matches[2]';
    	return $newrules + $rules;
    }
    
    // Adding the id var so that WP recognizes it
    function wp_insertMyRewriteQueryVars($vars)
    {
        array_push($vars, 'id');
        return $vars;
    }

    and my .htaccess looks like this

    <IfModule mod_rewrite.c>
    
    	Options +FollowSymlinks
    	RewriteEngine On
    	RewriteOptions MaxRedirects=10
    
    	RewriteBase /SUBFOLDER/
    
    	RewriteRule ^statelist-([^/\.]+)/?$ /SUBFOLDER/index.php?pagename=windows-in&town=$1 [QSA,L]
    
    #	RewriteRule ^index\.php$ - [L]
    	RewriteCond %{REQUEST_FILENAME} !-f
    	RewriteCond %{REQUEST_FILENAME} !-d
    	RewriteRule . /SUBFOLDER/index.php [L]
    
    </IfModule>

    obviously replace SUBFOLDER as necessary for your setup

    Now just GET input1 as necessary.

  7. buckrogers
    Member
    Posted 3 years ago #

    Great thanks! This worked for me.

  8. alex chousmith
    Member
    Posted 3 years ago #

    just wanted to say Thanks for this - it totally works, just what i was looking for, and is are very slick solution

Topic Closed

This topic has been closed to new replies.

About this Topic