Hướng dẫn how to get file url in wordpress - cách lấy url tệp trong wordpress

TL;DR

$themes_search_index = strpos[__FILE__,'wp-content/themes'];
$plugins_search_index = strpos[__FILE__,'wp-content/plugins'];
$url_postfix_prospect = dirname[__FILE__];

if[ $themes_search_index !== false ]{
    $url_postfix_index = strpos[$url_postfix_prospect,'wp-content/themes'];
} elseif[ $plugins_search_index !== false ] {
    $url_postfix_index = strpos[$url_postfix_prospect,'wp-content/plugins'];
}

$url_prefix = get_site_url[];
$url_postfix = substr[$url_postfix_prospect, $url_postfix_index];
$lib_path_url = $url_prefix .'/'. $url_postfix;

echo $lib_path_url;

Đưa ra đường dẫn tệp ...

/var/www/html/example.com/wp-content/plugins/my-plugin/lib/an-awesome-library/autoload.php

Kết quả cho ...

//example.com/wp-content/plugins/my-plugin/lib/an-awesome-library

Vấn đề

Xây dựng một thư viện cắm và chơi cho WordPress có thể "bao gồm" bên trong một chủ đề hoặc plugin có thể khó khăn. WordPress không có chức năng được xác định trước để lấy URL của tệp đó.no pre-defined function for getting the URL of that file.

Dung dịch

Điều trị các plugin và chủ đề riêng biệt.

Bước 1. Kiểm tra xem chủ đề hoặc plugin bên trong có bao gồm

/**
 * Contains numeric values if the respective strings are found,
 * Contains boolean `false` if the string is not found
 */
$themes_search_index = strpos[__FILE__,'wp-content/themes'];
$plugins_search_index = strpos[__FILE__,'wp-content/plugins'];

Bước 2A. Nếu bên trong một chủ đề

$url_postfix_prospect = dirname[__FILE__];

$url_postfix_index = strpos[$url_postfix_prospect,'wp-content/themes'];
$url_prefix = get_site_url[];
$url_postfix = substr[$url_postfix_prospect, $url_postfix_index];
$lib_path_url = $url_prefix .'/'. $url_postfix;

echo $lib_path_url;

Bước 2b. Nếu bên trong một plugin

$url_postfix_prospect = dirname[__FILE__];

$url_postfix_index = strpos[$url_postfix_prospect,'wp-content/plugins'];
$url_prefix = get_site_url[];
$url_postfix = substr[$url_postfix_prospect, $url_postfix_index];
$lib_path_url = $url_prefix .'/'. $url_postfix;

echo $lib_path_url;

wp_get_attachment_url [int & nbsp; $ bottomment_id & nbsp;]: chuỗi | falseint $attachment_id ]: string|false

Lấy URL cho một tệp đính kèm.

$attachment_id Intoptionalint Optional

Id bài đính kèm. Mặc định là toàn cầu $ Post.

Chuỗi | URL đính kèm sai, nếu không là sai. Attachment URL, otherwise false.

Bạn có thể thay đổi đầu ra của chức năng này thông qua & nbsp; wp nhận url đính kèm & nbsp; filter.

Hàm này sẽ không mã hóa URL. Nếu bạn có các tệp đính kèm có ký tự không hợp lệ trong tên của họ, bạn nên mã hóa URL RAW mã hóa đầu ra của chức năng này để có URL hợp lệ.

Mã mẫu cung cấp cho bạn một URL liên quan đến gốc cho tệp đính kèm của bạn:

$parsed = parse_url[ wp_get_attachment_url[ $attachment->ID ] ];
$url = dirname[ $parsed [ 'path' ] ] . '/' . rawurlencode[ basename[ $parsed[ 'path' ] ] ];

Nếu bạn muốn một URI cho trang & nbsp; trang đính kèm, không phải chính tệp đính kèm, bạn có thể sử dụng & nbsp; get_attachment_link.

Cũng tham khảo: & nbsp; wp_insert_attachment, & nbsp; wp_upload_dir, & nbsp; wp_get_attachment_image_src

Tệp: wp-includes/post.php. Xem tất cả các tài liệu tham khảo

function wp_get_attachment_url[ $attachment_id = 0 ] {
	global $pagenow;

	$attachment_id = [int] $attachment_id;

	$post = get_post[ $attachment_id ];

	if [ ! $post ] {
		return false;
	}

	if [ 'attachment' !== $post->post_type ] {
		return false;
	}

	$url = '';
	// Get attached file.
	$file = get_post_meta[ $post->ID, '_wp_attached_file', true ];
	if [ $file ] {
		// Get upload directory.
		$uploads = wp_get_upload_dir[];
		if [ $uploads && 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 ];
			} 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 ] ] . wp_basename[ $file ];
			} else {
				// It's a newly-uploaded file, therefore $file is relative to the basedir.
				$url = $uploads['baseurl'] . "/$file";
			}
		}
	}

	/*
	 * If any of the above options failed, Fallback on the GUID as used pre-2.7,
	 * not recommended to rely upon this.
	 */
	if [ ! $url ] {
		$url = get_the_guid[ $post->ID ];
	}

	// On SSL front end, URLs should be HTTPS.
	if [ is_ssl[] && ! is_admin[] && 'wp-login.php' !== $pagenow ] {
		$url = set_url_scheme[ $url ];
	}

	/**
	 * Filters the attachment URL.
	 *
	 * @since 2.1.0
	 *
	 * @param string $url           URL for the given attachment.
	 * @param int    $attachment_id Attachment post ID.
	 */
	$url = apply_filters[ 'wp_get_attachment_url', $url, $post->ID ];

	if [ ! $url ] {
		return false;
	}

	return $url;
}

Xem trên Tracview trên GitHub View on GitHub

Ứng dụng_filters ['wp_get_attachment_url', chuỗi $ url, int $ đính kèm_id][ 'wp_get_attachment_url', string $url, int $attachment_id ]

Lọc url đính kèm.

Thay đổiPhiên bảnSự mô tả
2.1.0 Được giới thiệu.

Làm cách nào để tìm đường dẫn tệp trong WordPress?

Bạn có thể sử dụng hàm WordPress của plugin_dir_path [] để có được đường dẫn đầy đủ của thư mục. Bao gồm_once plugin_dir_path [__file__].use a WordPress function of plugin_dir_path[] to get the full path of the folder. include_once plugin_dir_path[ __FILE__ ] .

Tôi có thể chỉnh sửa URL tệp trong WordPress không?

Trong bảng điều khiển WordPress của bạn, đi đến Cài đặt / Chung.Thay đổi địa chỉ WordPress [URL] và địa chỉ trang web [URL] thành URL mới của bạn.Lưu thay đổi.Chuyển đến Cài đặt / Cài đặt Permalink mà không thay đổi bất cứ điều gì và nhấp vào Lưu Thay đổi.. Change the WordPress Address [URL] and the Site Address [URL] to your new URL. Save changes. Go to Settings / Permalink Settings without changing anything and click on Save changes.

Làm cách nào để tìm URL hình ảnh phương tiện trong WordPress?

Mở một trang hoặc đăng nơi hình ảnh được công bố và xác định vị trí hình ảnh.Sau đó, nhấp chuột phải vào hình ảnh và chọn tùy chọn 'Mở hình ảnh trong tab mới'. Đổi hình ảnh mở trong tab mới, hãy nhìn vào URL trong thanh địa chỉ.Đó là URL hình ảnh WordPress của bạn.right-click on the image, and select 'Open image in new tab' option. Once the image opens in the new tab, look at the URL in the address bar. That is your WordPress image URL.

Làm cách nào để liên kết các tệp phương tiện trong WordPress?

Cách thêm phương tiện [PDFS, JPG, v.v.] vào các bài đăng và trang..
Đăng nhập vào trang web WordPress của bạn ..
Nhấp vào phương tiện [điều hướng bên trái]> Thêm mới.....
Một trong hai: ... .
[Tùy chọn] Bây giờ bạn có thể chỉnh sửa hình ảnh bằng cách nhấp vào nút 'Chỉnh sửa hình ảnh' ..
Điền vào trường văn bản thay thế.....
Lưu ý URL tệp ..

Bài Viết Liên Quan

Chủ Đề