WordPress.org

Forums

Using wp_rewrite to handle image output (1 post)

  1. darkspotinthecorner
    Member
    Posted 4 years ago #

    Hello there, this is my first post here, so be gentle. :-P

    I have some issues using $wp_rewrite, as the topic suggests. I'm trying to turn incoming urls lilke this one:

    http://www.mywpsite.de/file/thumbnail/img_test_001

    into:

    http://www.mywpsite.de/index.php/index.php?myplugin_fileaccess=1&type=thumbnail&name=img_test_001

    To do this, I have created the following code in my plugin (Note: It's all located inside a class):

    // added to filter "generate_rewrite_rules"
    static function generate_rewrite_rules($wp_rewrite) {
    
    	$wp_rewrite->rules = array_merge(
    		array(
    			'file/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/?$' => 'index.php?'.self::ID.'_fileaccess=1&type=$matches[1]&name=$matches[2]'
    		),
    		$wp_rewrite->rules
    	);
    
    }
    
    // ...
    
    // added to filter "query_vars"
    static function query_vars($public_query_vars) {
    
    	$public_query_vars[] = self::ID.'_fileaccess';
    	$public_query_vars[] = 'type';
    	$public_query_vars[] = 'name';
    
    	return $public_query_vars;
    
    }
    
    // ...
    
    // added to action "init"
    static function init() {
    
    	// only present for testing...
    	global $wp_rewrite;
    	$wp_rewrite->flush_rules();
    
    	// This should trigger if wp_rewrite worked
    	$facheck = intval(get_query_var(self::ID.'_fileaccess'));
    	if($facheck === 1) {
    		$type = preg_replace('[^a-zA-Z0-9_]', '', get_query_var('type'));
    		$name = preg_replace('[^a-zA-Z0-9_]', '', get_query_var('name'));
    		if(!empty($type) && !empty($name)) {
    			self::fileaccess($type, $name);
    		}
    	}
    
    	// This will trigger if the url is entered directly, just for testing purpose
    	if(isset($_GET[self::ID.'_fileaccess'])) {
    		$facheck = intval($_GET[self::ID.'_fileaccess']);
    		if($facheck === 1) {
    			$type = preg_replace('[^a-zA-Z0-9_]', '', $_GET['type']);
    			$name = preg_replace('[^a-zA-Z0-9_]', '', $_GET['name']);
    			if(!empty($type) && !empty($name)) {
    				self::fileaccess($type, $name);
    			}
    		}
    	}
    
    }

    However, it's not working... :-(

    Calling the url http://www.mywpsite.de/file/thumbnail/img_test_001 results in a 404 Page.

    But calling http://www.mywpsite.de/index.php/index.php?myplugin_fileaccess=1&type=thumbnail&name=img_test_001 will work correctly and deliver the image to the browser.

    To debug this mess, I did install "AskApache RewriteRules Viewer". It shows my rule in the following categories:

    rules => index.php?WowGuild_fileaccess=1&type=$matches[1]&name=$matches[2]
    wp_rewrite_rules => index.php?WowGuild_fileaccess=1&type=$matches[1]&name=$matches[2]

    Looks fine to me...

    So... I'm kinda lost now... Any suggestions? I would be really grateful :-)

Topic Closed

This topic has been closed to new replies.

About this Topic

  • RSS feed for this topic
  • Started 4 years ago by darkspotinthecorner
  • This topic is not resolved
  • WordPress version: 3.0.5