Home News

Attachment_link — Позволяет изменить ссылку на вложение (файл из медиабиблиотеки). Хук-фильтр WordPress

01.09.2018

Позволяет изменить ссылку на вложение (файл из медиабиблиотеки).

Используется в функции get_attachment_link() .

Использование

add_filter( 'attachment_link', 'custom_filter_attachment_link', 10, 2 ); function custom_filter_attachment_link( $link, $post_id ){ // фильтр... return $link; } $link(строка) Ссылка на страницу вложения. $post_id(число) ID вложения.

Примеры

#1 Замена ссылки на страницу медиафайла ссылкой на сам медиафайл

add_filter( 'attachment_link', 'change_attachment_link', 10, 2 ); function change_attachment_link( $link, $post_id ) { return wp_get_attachment_url( $post_id ); }

Где используется хук

... } elseif ( $wp_rewrite->using_permalinks() && ! $leavename ) { $link = home_url( user_trailingslashit( $post->post_name ) ); } if ( ! $link ) $link = home_url( '/?attachment_id=' . $post->ID ); /** * Filters the permalink for an attachment. * * @since 2.0.0 * * @param string $link The attachment's permalink. * @param int $post_id Attachment ID. */ return apply_filters( 'attachment_link', $link, $post->ID ); } /** * Retrieves the permalink for the year archives. * * @since 1.5.0 * * @global WP_Rewrite $wp_rewrite * * @param int|bool $year False for current year or year for permalink. * @return string The permalink for the specified year archive. */ function get_year_link( $year ) { global $wp_rewrite; if ( !$year ) ... campusboy 2826 — youtube.com/c/wpplus

Создатель YouTube канала wp-plus, на котором делюсь своими опытом. Активный пользователь wp-kama.ru. WordPress-разработчик. Разработка сайтов и лендингов. Доработка существующих проектов. Сопровождение ресурсов.

Редакторы: Kama 5397
rss