Skip to content

Instantly share code, notes, and snippets.

@wcxaaa
Last active January 31, 2018 07:21
Show Gist options
  • Save wcxaaa/3ed3345e8a6fc37b094eba7bc43b1775 to your computer and use it in GitHub Desktop.
Save wcxaaa/3ed3345e8a6fc37b094eba7bc43b1775 to your computer and use it in GitHub Desktop.
Wordpress 相对路径
/*wp-config.php*/
/*设置相对路径*/
define('WP_HOME', 'http://'.$_SERVER['HTTP_HOST']);
define('WP_SITEURL', 'http://'.$_SERVER['HTTP_HOST']);
/* wp-includes/post.php 注意加注释的地方的$url */
/* 设置图片附件为相对路径 */
function wp_get_attachment_url( $attachment_id = 0 ) {
// ...
$url = '';
// Get attached file.
if ( $file = get_post_meta( $post->ID, '_wp_attached_file', true ) ) {
// Get upload directory.
if ( ( $uploads = wp_get_upload_dir() ) && false === $uploads['error'] ) {
// Check that the upload base exists in the file location.
if ( 0 === strpos( $file, $uploads['basedir'] ) ) {
// Replace file location with url location.
//$url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
$url = $file_dir . "/wp-content/uploads/" . $file;
} elseif ( false !== strpos($file, 'wp-content/uploads') ) {
// Get the directory name relative to the basedir (back compat for pre-2.7 uploads)
//$url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $file ) ) . basename( $file );
$url = $file_dir . "/wp-content/uploads/" . $file;
} else {
// It's a newly-uploaded file, therefore $file is relative to the basedir.
//$url = $uploads['baseurl'] . "/$file";
$url = $file_dir . "/wp-content/uploads/" . $file;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment