我最终将代码更改为以下内容,这正是我所需要的:
挂钩:
add_action( \'generate_rewrite_rules\', array( $this, \'addRewrites\' ) );
创建规则:
public function addRewrites( $content ) {
global $wp_rewrite;
$plugin_path = substr( str_replace( home_url(), \'\', plugins_url( \'\', __FILE__ ) ), 1 );
$upload_path = substr( str_replace( home_url(), \'\', wp_upload_dir()[\'baseurl\'] ), 1 );
$new_rules = array(
$upload_path . \'/*(.+\\.jpg)$\' => $plugin_path . \'/image.php?original=$1\',
$upload_path . \'/*(.+\\.gif)$\' => $plugin_path . \'/image.php?original=$1\',
$upload_path . \'/*(.+\\.png)$\' => $plugin_path . \'/image.php?original=$1\',
);
$wp_rewrite->non_wp_rules += $new_rules;
}
这只会上传文件夹中的图像,所以我相信这是非常安全的。