PageRenderTime 68ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/link-template.php

https://bitbucket.org/acipriani/madeinapulia.com
PHP | 3324 lines | 2287 code | 212 blank | 825 comment | 191 complexity | 159852143c1f5bd97fb3c5f3365af446 MD5 | raw file
Possible License(s): GPL-3.0, MIT, BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-2.0
  1. <?php
  2. /**
  3. * WordPress Link Template Functions
  4. *
  5. * @package WordPress
  6. * @subpackage Template
  7. */
  8. /**
  9. * Display the permalink for the current post.
  10. *
  11. * @since 1.2.0
  12. */
  13. function the_permalink() {
  14. /**
  15. * Filter the display of the permalink for the current post.
  16. *
  17. * @since 1.5.0
  18. *
  19. * @param string $permalink The permalink for the current post.
  20. */
  21. echo esc_url( apply_filters( 'the_permalink', get_permalink() ) );
  22. }
  23. /**
  24. * Retrieve trailing slash string, if blog set for adding trailing slashes.
  25. *
  26. * Conditionally adds a trailing slash if the permalink structure has a trailing
  27. * slash, strips the trailing slash if not. The string is passed through the
  28. * 'user_trailingslashit' filter. Will remove trailing slash from string, if
  29. * blog is not set to have them.
  30. *
  31. * @since 2.2.0
  32. * @uses $wp_rewrite
  33. *
  34. * @param string $string URL with or without a trailing slash.
  35. * @param string $type_of_url The type of URL being considered (e.g. single, category, etc) for use in the filter.
  36. * @return string The URL with the trailing slash appended or stripped.
  37. */
  38. function user_trailingslashit($string, $type_of_url = '') {
  39. global $wp_rewrite;
  40. if ( $wp_rewrite->use_trailing_slashes )
  41. $string = trailingslashit($string);
  42. else
  43. $string = untrailingslashit($string);
  44. /**
  45. * Filter the trailing slashed string, depending on whether the site is set
  46. * to use training slashes.
  47. *
  48. * @since 2.2.0
  49. *
  50. * @param string $string URL with or without a trailing slash.
  51. * @param string $type_of_url The type of URL being considered. Accepts 'single', 'single_trackback',
  52. * 'single_feed', 'single_paged', 'feed', 'category', 'page', 'year',
  53. * 'month', 'day', 'paged', 'post_type_archive'.
  54. */
  55. $string = apply_filters( 'user_trailingslashit', $string, $type_of_url );
  56. return $string;
  57. }
  58. /**
  59. * Display permalink anchor for current post.
  60. *
  61. * The permalink mode title will use the post title for the 'a' element 'id'
  62. * attribute. The id mode uses 'post-' with the post ID for the 'id' attribute.
  63. *
  64. * @since 0.71
  65. *
  66. * @param string $mode Permalink mode can be either 'title', 'id', or default, which is 'id'.
  67. */
  68. function permalink_anchor( $mode = 'id' ) {
  69. $post = get_post();
  70. switch ( strtolower( $mode ) ) {
  71. case 'title':
  72. $title = sanitize_title( $post->post_title ) . '-' . $post->ID;
  73. echo '<a id="'.$title.'"></a>';
  74. break;
  75. case 'id':
  76. default:
  77. echo '<a id="post-' . $post->ID . '"></a>';
  78. break;
  79. }
  80. }
  81. /**
  82. * Retrieve full permalink for current post or post ID.
  83. *
  84. * This function is an alias for get_permalink().
  85. *
  86. * @since 3.9.0
  87. *
  88. * @see get_permalink()
  89. *
  90. * @param int|WP_Post $id Optional. Post ID or post object. Default is the current post.
  91. * @param bool $leavename Optional. Whether to keep post name or page name. Default false.
  92. * @return string|bool The permalink URL or false if post does not exist.
  93. */
  94. function get_the_permalink( $id = 0, $leavename = false ) {
  95. return get_permalink( $id, $leavename );
  96. }
  97. /**
  98. * Retrieve full permalink for current post or post ID.
  99. *
  100. * @since 1.0.0
  101. *
  102. * @param int|WP_Post $id Optional. Post ID or post object. Default current post.
  103. * @param bool $leavename Optional. Whether to keep post name or page name. Default false.
  104. * @return string|bool The permalink URL or false if post does not exist.
  105. */
  106. function get_permalink( $id = 0, $leavename = false ) {
  107. $rewritecode = array(
  108. '%year%',
  109. '%monthnum%',
  110. '%day%',
  111. '%hour%',
  112. '%minute%',
  113. '%second%',
  114. $leavename? '' : '%postname%',
  115. '%post_id%',
  116. '%category%',
  117. '%author%',
  118. $leavename? '' : '%pagename%',
  119. );
  120. if ( is_object($id) && isset($id->filter) && 'sample' == $id->filter ) {
  121. $post = $id;
  122. $sample = true;
  123. } else {
  124. $post = get_post($id);
  125. $sample = false;
  126. }
  127. if ( empty($post->ID) )
  128. return false;
  129. if ( $post->post_type == 'page' )
  130. return get_page_link($post, $leavename, $sample);
  131. elseif ( $post->post_type == 'attachment' )
  132. return get_attachment_link( $post, $leavename );
  133. elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) )
  134. return get_post_permalink($post, $leavename, $sample);
  135. $permalink = get_option('permalink_structure');
  136. /**
  137. * Filter the permalink structure for a post before token replacement occurs.
  138. *
  139. * Only applies to posts with post_type of 'post'.
  140. *
  141. * @since 3.0.0
  142. *
  143. * @param string $permalink The site's permalink structure.
  144. * @param WP_Post $post The post in question.
  145. * @param bool $leavename Whether to keep the post name.
  146. */
  147. $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename );
  148. if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
  149. $unixtime = strtotime($post->post_date);
  150. $category = '';
  151. if ( strpos($permalink, '%category%') !== false ) {
  152. $cats = get_the_category($post->ID);
  153. if ( $cats ) {
  154. usort($cats, '_usort_terms_by_ID'); // order by ID
  155. /**
  156. * Filter the category that gets used in the %category% permalink token.
  157. *
  158. * @since 3.5.0
  159. *
  160. * @param stdClass $cat The category to use in the permalink.
  161. * @param array $cats Array of all categories associated with the post.
  162. * @param WP_Post $post The post in question.
  163. */
  164. $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
  165. $category_object = get_term( $category_object, 'category' );
  166. $category = $category_object->slug;
  167. if ( $parent = $category_object->parent )
  168. $category = get_category_parents($parent, false, '/', true) . $category;
  169. }
  170. // show default category in permalinks, without
  171. // having to assign it explicitly
  172. if ( empty($category) ) {
  173. $default_category = get_term( get_option( 'default_category' ), 'category' );
  174. $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
  175. }
  176. }
  177. $author = '';
  178. if ( strpos($permalink, '%author%') !== false ) {
  179. $authordata = get_userdata($post->post_author);
  180. $author = $authordata->user_nicename;
  181. }
  182. $date = explode(" ",date('Y m d H i s', $unixtime));
  183. $rewritereplace =
  184. array(
  185. $date[0],
  186. $date[1],
  187. $date[2],
  188. $date[3],
  189. $date[4],
  190. $date[5],
  191. $post->post_name,
  192. $post->ID,
  193. $category,
  194. $author,
  195. $post->post_name,
  196. );
  197. $permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) );
  198. $permalink = user_trailingslashit($permalink, 'single');
  199. } else { // if they're not using the fancy permalink option
  200. $permalink = home_url('?p=' . $post->ID);
  201. }
  202. /**
  203. * Filter the permalink for a post.
  204. *
  205. * Only applies to posts with post_type of 'post'.
  206. *
  207. * @since 1.5.0
  208. *
  209. * @param string $permalink The post's permalink.
  210. * @param WP_Post $post The post in question.
  211. * @param bool $leavename Whether to keep the post name.
  212. */
  213. return apply_filters( 'post_link', $permalink, $post, $leavename );
  214. }
  215. /**
  216. * Retrieve the permalink for a post with a custom post type.
  217. *
  218. * @since 3.0.0
  219. *
  220. * @param int $id Optional. Post ID.
  221. * @param bool $leavename Optional, defaults to false. Whether to keep post name.
  222. * @param bool $sample Optional, defaults to false. Is it a sample permalink.
  223. * @return string The post permalink.
  224. */
  225. function get_post_permalink( $id = 0, $leavename = false, $sample = false ) {
  226. global $wp_rewrite;
  227. $post = get_post($id);
  228. if ( is_wp_error( $post ) )
  229. return $post;
  230. $post_link = $wp_rewrite->get_extra_permastruct($post->post_type);
  231. $slug = $post->post_name;
  232. $draft_or_pending = isset($post->post_status) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
  233. $post_type = get_post_type_object($post->post_type);
  234. if ( $post_type->hierarchical ) {
  235. $slug = get_page_uri( $id );
  236. }
  237. if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) {
  238. if ( ! $leavename ) {
  239. $post_link = str_replace("%$post->post_type%", $slug, $post_link);
  240. }
  241. $post_link = home_url( user_trailingslashit($post_link) );
  242. } else {
  243. if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) )
  244. $post_link = add_query_arg($post_type->query_var, $slug, '');
  245. else
  246. $post_link = add_query_arg(array('post_type' => $post->post_type, 'p' => $post->ID), '');
  247. $post_link = home_url($post_link);
  248. }
  249. /**
  250. * Filter the permalink for a post with a custom post type.
  251. *
  252. * @since 3.0.0
  253. *
  254. * @param string $post_link The post's permalink.
  255. * @param WP_Post $post The post in question.
  256. * @param bool $leavename Whether to keep the post name.
  257. * @param bool $sample Is it a sample permalink.
  258. */
  259. return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample );
  260. }
  261. /**
  262. * Retrieve permalink from post ID.
  263. *
  264. * @since 1.0.0
  265. *
  266. * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
  267. * @param mixed $deprecated Not used.
  268. * @return string
  269. */
  270. function post_permalink( $post_id = 0, $deprecated = '' ) {
  271. if ( !empty( $deprecated ) )
  272. _deprecated_argument( __FUNCTION__, '1.3' );
  273. return get_permalink($post_id);
  274. }
  275. /**
  276. * Retrieve the permalink for current page or page ID.
  277. *
  278. * Respects page_on_front. Use this one.
  279. *
  280. * @since 1.5.0
  281. *
  282. * @param int|object $post Optional. Post ID or object.
  283. * @param bool $leavename Optional, defaults to false. Whether to keep page name.
  284. * @param bool $sample Optional, defaults to false. Is it a sample permalink.
  285. * @return string The page permalink.
  286. */
  287. function get_page_link( $post = false, $leavename = false, $sample = false ) {
  288. $post = get_post( $post );
  289. if ( 'page' == get_option( 'show_on_front' ) && $post->ID == get_option( 'page_on_front' ) )
  290. $link = home_url('/');
  291. else
  292. $link = _get_page_link( $post, $leavename, $sample );
  293. /**
  294. * Filter the permalink for a page.
  295. *
  296. * @since 1.5.0
  297. *
  298. * @param string $link The page's permalink.
  299. * @param int $post_id The ID of the page.
  300. * @param bool $sample Is it a sample permalink.
  301. */
  302. return apply_filters( 'page_link', $link, $post->ID, $sample );
  303. }
  304. /**
  305. * Retrieve the page permalink.
  306. *
  307. * Ignores page_on_front. Internal use only.
  308. *
  309. * @since 2.1.0
  310. * @access private
  311. *
  312. * @param int|object $post Optional. Post ID or object.
  313. * @param bool $leavename Optional. Leave name.
  314. * @param bool $sample Optional. Sample permalink.
  315. * @return string The page permalink.
  316. */
  317. function _get_page_link( $post = false, $leavename = false, $sample = false ) {
  318. global $wp_rewrite;
  319. $post = get_post( $post );
  320. $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
  321. $link = $wp_rewrite->get_page_permastruct();
  322. if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) {
  323. if ( ! $leavename ) {
  324. $link = str_replace('%pagename%', get_page_uri( $post ), $link);
  325. }
  326. $link = home_url($link);
  327. $link = user_trailingslashit($link, 'page');
  328. } else {
  329. $link = home_url( '?page_id=' . $post->ID );
  330. }
  331. /**
  332. * Filter the permalink for a non-page_on_front page.
  333. *
  334. * @since 2.1.0
  335. *
  336. * @param string $link The page's permalink.
  337. * @param int $post_id The ID of the page.
  338. */
  339. return apply_filters( '_get_page_link', $link, $post->ID );
  340. }
  341. /**
  342. * Retrieve permalink for attachment.
  343. *
  344. * This can be used in the WordPress Loop or outside of it.
  345. *
  346. * @since 2.0.0
  347. *
  348. * @param int|object $post Optional. Post ID or object.
  349. * @param bool $leavename Optional. Leave name.
  350. * @return string The attachment permalink.
  351. */
  352. function get_attachment_link( $post = null, $leavename = false ) {
  353. global $wp_rewrite;
  354. $link = false;
  355. $post = get_post( $post );
  356. $parent = ( $post->post_parent > 0 && $post->post_parent != $post->ID ) ? get_post( $post->post_parent ) : false;
  357. if ( $wp_rewrite->using_permalinks() && $parent ) {
  358. if ( 'page' == $parent->post_type )
  359. $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front
  360. else
  361. $parentlink = get_permalink( $post->post_parent );
  362. if ( is_numeric($post->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
  363. $name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
  364. else
  365. $name = $post->post_name;
  366. if ( strpos($parentlink, '?') === false )
  367. $link = user_trailingslashit( trailingslashit($parentlink) . '%postname%' );
  368. if ( ! $leavename )
  369. $link = str_replace( '%postname%', $name, $link );
  370. }
  371. if ( ! $link )
  372. $link = home_url( '/?attachment_id=' . $post->ID );
  373. /**
  374. * Filter the permalink for an attachment.
  375. *
  376. * @since 2.0.0
  377. *
  378. * @param string $link The attachment's permalink.
  379. * @param int $post_id Attachment ID.
  380. */
  381. return apply_filters( 'attachment_link', $link, $post->ID );
  382. }
  383. /**
  384. * Retrieve the permalink for the year archives.
  385. *
  386. * @since 1.5.0
  387. *
  388. * @param int|bool $year False for current year or year for permalink.
  389. * @return string The permalink for the specified year archive.
  390. */
  391. function get_year_link($year) {
  392. global $wp_rewrite;
  393. if ( !$year )
  394. $year = gmdate('Y', current_time('timestamp'));
  395. $yearlink = $wp_rewrite->get_year_permastruct();
  396. if ( !empty($yearlink) ) {
  397. $yearlink = str_replace('%year%', $year, $yearlink);
  398. $yearlink = home_url( user_trailingslashit( $yearlink, 'year' ) );
  399. } else {
  400. $yearlink = home_url( '?m=' . $year );
  401. }
  402. /**
  403. * Filter the year archive permalink.
  404. *
  405. * @since 1.5.0
  406. *
  407. * @param string $yearlink Permalink for the year archive.
  408. * @param int $year Year for the archive.
  409. */
  410. return apply_filters( 'year_link', $yearlink, $year );
  411. }
  412. /**
  413. * Retrieve the permalink for the month archives with year.
  414. *
  415. * @since 1.0.0
  416. *
  417. * @param bool|int $year False for current year. Integer of year.
  418. * @param bool|int $month False for current month. Integer of month.
  419. * @return string The permalink for the specified month and year archive.
  420. */
  421. function get_month_link($year, $month) {
  422. global $wp_rewrite;
  423. if ( !$year )
  424. $year = gmdate('Y', current_time('timestamp'));
  425. if ( !$month )
  426. $month = gmdate('m', current_time('timestamp'));
  427. $monthlink = $wp_rewrite->get_month_permastruct();
  428. if ( !empty($monthlink) ) {
  429. $monthlink = str_replace('%year%', $year, $monthlink);
  430. $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
  431. $monthlink = home_url( user_trailingslashit( $monthlink, 'month' ) );
  432. } else {
  433. $monthlink = home_url( '?m=' . $year . zeroise( $month, 2 ) );
  434. }
  435. /**
  436. * Filter the month archive permalink.
  437. *
  438. * @since 1.5.0
  439. *
  440. * @param string $monthlink Permalink for the month archive.
  441. * @param int $year Year for the archive.
  442. * @param int $month The month for the archive.
  443. */
  444. return apply_filters( 'month_link', $monthlink, $year, $month );
  445. }
  446. /**
  447. * Retrieve the permalink for the day archives with year and month.
  448. *
  449. * @since 1.0.0
  450. *
  451. * @param bool|int $year False for current year. Integer of year.
  452. * @param bool|int $month False for current month. Integer of month.
  453. * @param bool|int $day False for current day. Integer of day.
  454. * @return string The permalink for the specified day, month, and year archive.
  455. */
  456. function get_day_link($year, $month, $day) {
  457. global $wp_rewrite;
  458. if ( !$year )
  459. $year = gmdate('Y', current_time('timestamp'));
  460. if ( !$month )
  461. $month = gmdate('m', current_time('timestamp'));
  462. if ( !$day )
  463. $day = gmdate('j', current_time('timestamp'));
  464. $daylink = $wp_rewrite->get_day_permastruct();
  465. if ( !empty($daylink) ) {
  466. $daylink = str_replace('%year%', $year, $daylink);
  467. $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
  468. $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
  469. $daylink = home_url( user_trailingslashit( $daylink, 'day' ) );
  470. } else {
  471. $daylink = home_url( '?m=' . $year . zeroise( $month, 2 ) . zeroise( $day, 2 ) );
  472. }
  473. /**
  474. * Filter the day archive permalink.
  475. *
  476. * @since 1.5.0
  477. *
  478. * @param string $daylink Permalink for the day archive.
  479. * @param int $year Year for the archive.
  480. * @param int $month Month for the archive.
  481. * @param int $day The day for the archive.
  482. */
  483. return apply_filters( 'day_link', $daylink, $year, $month, $day );
  484. }
  485. /**
  486. * Display the permalink for the feed type.
  487. *
  488. * @since 3.0.0
  489. *
  490. * @param string $anchor The link's anchor text.
  491. * @param string $feed Optional, defaults to default feed. Feed type.
  492. */
  493. function the_feed_link( $anchor, $feed = '' ) {
  494. $link = '<a href="' . esc_url( get_feed_link( $feed ) ) . '">' . $anchor . '</a>';
  495. /**
  496. * Filter the feed link anchor tag.
  497. *
  498. * @since 3.0.0
  499. *
  500. * @param string $link The complete anchor tag for a feed link.
  501. * @param string $feed The feed type, or an empty string for the
  502. * default feed type.
  503. */
  504. echo apply_filters( 'the_feed_link', $link, $feed );
  505. }
  506. /**
  507. * Retrieve the permalink for the feed type.
  508. *
  509. * @since 1.5.0
  510. *
  511. * @param string $feed Optional, defaults to default feed. Feed type.
  512. * @return string The feed permalink.
  513. */
  514. function get_feed_link($feed = '') {
  515. global $wp_rewrite;
  516. $permalink = $wp_rewrite->get_feed_permastruct();
  517. if ( '' != $permalink ) {
  518. if ( false !== strpos($feed, 'comments_') ) {
  519. $feed = str_replace('comments_', '', $feed);
  520. $permalink = $wp_rewrite->get_comment_feed_permastruct();
  521. }
  522. if ( get_default_feed() == $feed )
  523. $feed = '';
  524. $permalink = str_replace('%feed%', $feed, $permalink);
  525. $permalink = preg_replace('#/+#', '/', "/$permalink");
  526. $output = home_url( user_trailingslashit($permalink, 'feed') );
  527. } else {
  528. if ( empty($feed) )
  529. $feed = get_default_feed();
  530. if ( false !== strpos($feed, 'comments_') )
  531. $feed = str_replace('comments_', 'comments-', $feed);
  532. $output = home_url("?feed={$feed}");
  533. }
  534. /**
  535. * Filter the feed type permalink.
  536. *
  537. * @since 1.5.0
  538. *
  539. * @param string $output The feed permalink.
  540. * @param string $feed Feed type.
  541. */
  542. return apply_filters( 'feed_link', $output, $feed );
  543. }
  544. /**
  545. * Retrieve the permalink for the post comments feed.
  546. *
  547. * @since 2.2.0
  548. *
  549. * @param int $post_id Optional. Post ID.
  550. * @param string $feed Optional. Feed type.
  551. * @return string The permalink for the comments feed for the given post.
  552. */
  553. function get_post_comments_feed_link($post_id = 0, $feed = '') {
  554. $post_id = absint( $post_id );
  555. if ( ! $post_id )
  556. $post_id = get_the_ID();
  557. if ( empty( $feed ) )
  558. $feed = get_default_feed();
  559. if ( '' != get_option('permalink_structure') ) {
  560. if ( 'page' == get_option('show_on_front') && $post_id == get_option('page_on_front') )
  561. $url = _get_page_link( $post_id );
  562. else
  563. $url = get_permalink($post_id);
  564. $url = trailingslashit($url) . 'feed';
  565. if ( $feed != get_default_feed() )
  566. $url .= "/$feed";
  567. $url = user_trailingslashit($url, 'single_feed');
  568. } else {
  569. $type = get_post_field('post_type', $post_id);
  570. if ( 'page' == $type )
  571. $url = add_query_arg( array( 'feed' => $feed, 'page_id' => $post_id ), home_url( '/' ) );
  572. else
  573. $url = add_query_arg( array( 'feed' => $feed, 'p' => $post_id ), home_url( '/' ) );
  574. }
  575. /**
  576. * Filter the post comments feed permalink.
  577. *
  578. * @since 1.5.1
  579. *
  580. * @param string $url Post comments feed permalink.
  581. */
  582. return apply_filters( 'post_comments_feed_link', $url );
  583. }
  584. /**
  585. * Display the comment feed link for a post.
  586. *
  587. * Prints out the comment feed link for a post. Link text is placed in the
  588. * anchor. If no link text is specified, default text is used. If no post ID is
  589. * specified, the current post is used.
  590. *
  591. * @since 2.5.0
  592. *
  593. * @param string $link_text Descriptive text.
  594. * @param int $post_id Optional post ID. Default to current post.
  595. * @param string $feed Optional. Feed format.
  596. * @return string Link to the comment feed for the current post.
  597. */
  598. function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
  599. $url = esc_url( get_post_comments_feed_link( $post_id, $feed ) );
  600. if ( empty($link_text) )
  601. $link_text = __('Comments Feed');
  602. /**
  603. * Filter the post comment feed link anchor tag.
  604. *
  605. * @since 2.8.0
  606. *
  607. * @param string $link The complete anchor tag for the comment feed link.
  608. * @param int $post_id Post ID.
  609. * @param string $feed The feed type, or an empty string for the default feed type.
  610. */
  611. echo apply_filters( 'post_comments_feed_link_html', "<a href='$url'>$link_text</a>", $post_id, $feed );
  612. }
  613. /**
  614. * Retrieve the feed link for a given author.
  615. *
  616. * Returns a link to the feed for all posts by a given author. A specific feed
  617. * can be requested or left blank to get the default feed.
  618. *
  619. * @since 2.5.0
  620. *
  621. * @param int $author_id ID of an author.
  622. * @param string $feed Optional. Feed type.
  623. * @return string Link to the feed for the author specified by $author_id.
  624. */
  625. function get_author_feed_link( $author_id, $feed = '' ) {
  626. $author_id = (int) $author_id;
  627. $permalink_structure = get_option('permalink_structure');
  628. if ( empty($feed) )
  629. $feed = get_default_feed();
  630. if ( '' == $permalink_structure ) {
  631. $link = home_url("?feed=$feed&amp;author=" . $author_id);
  632. } else {
  633. $link = get_author_posts_url($author_id);
  634. if ( $feed == get_default_feed() )
  635. $feed_link = 'feed';
  636. else
  637. $feed_link = "feed/$feed";
  638. $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
  639. }
  640. /**
  641. * Filter the feed link for a given author.
  642. *
  643. * @since 1.5.1
  644. *
  645. * @param string $link The author feed link.
  646. * @param string $feed Feed type.
  647. */
  648. $link = apply_filters( 'author_feed_link', $link, $feed );
  649. return $link;
  650. }
  651. /**
  652. * Retrieve the feed link for a category.
  653. *
  654. * Returns a link to the feed for all posts in a given category. A specific feed
  655. * can be requested or left blank to get the default feed.
  656. *
  657. * @since 2.5.0
  658. *
  659. * @param int $cat_id ID of a category.
  660. * @param string $feed Optional. Feed type.
  661. * @return string Link to the feed for the category specified by $cat_id.
  662. */
  663. function get_category_feed_link($cat_id, $feed = '') {
  664. return get_term_feed_link($cat_id, 'category', $feed);
  665. }
  666. /**
  667. * Retrieve the feed link for a term.
  668. *
  669. * Returns a link to the feed for all posts in a given term. A specific feed
  670. * can be requested or left blank to get the default feed.
  671. *
  672. * @since 3.0.0
  673. *
  674. * @param int $term_id ID of a category.
  675. * @param string $taxonomy Optional. Taxonomy of $term_id
  676. * @param string $feed Optional. Feed type.
  677. * @return string Link to the feed for the term specified by $term_id and $taxonomy.
  678. */
  679. function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
  680. $term_id = ( int ) $term_id;
  681. $term = get_term( $term_id, $taxonomy );
  682. if ( empty( $term ) || is_wp_error( $term ) )
  683. return false;
  684. if ( empty( $feed ) )
  685. $feed = get_default_feed();
  686. $permalink_structure = get_option( 'permalink_structure' );
  687. if ( '' == $permalink_structure ) {
  688. if ( 'category' == $taxonomy ) {
  689. $link = home_url("?feed=$feed&amp;cat=$term_id");
  690. }
  691. elseif ( 'post_tag' == $taxonomy ) {
  692. $link = home_url("?feed=$feed&amp;tag=$term->slug");
  693. } else {
  694. $t = get_taxonomy( $taxonomy );
  695. $link = home_url("?feed=$feed&amp;$t->query_var=$term->slug");
  696. }
  697. } else {
  698. $link = get_term_link( $term_id, $term->taxonomy );
  699. if ( $feed == get_default_feed() )
  700. $feed_link = 'feed';
  701. else
  702. $feed_link = "feed/$feed";
  703. $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' );
  704. }
  705. if ( 'category' == $taxonomy ) {
  706. /**
  707. * Filter the category feed link.
  708. *
  709. * @since 1.5.1
  710. *
  711. * @param string $link The category feed link.
  712. * @param string $feed Feed type.
  713. */
  714. $link = apply_filters( 'category_feed_link', $link, $feed );
  715. } elseif ( 'post_tag' == $taxonomy ) {
  716. /**
  717. * Filter the post tag feed link.
  718. *
  719. * @since 2.3.0
  720. *
  721. * @param string $link The tag feed link.
  722. * @param string $feed Feed type.
  723. */
  724. $link = apply_filters( 'tag_feed_link', $link, $feed );
  725. } else {
  726. /**
  727. * Filter the feed link for a taxonomy other than 'category' or 'post_tag'.
  728. *
  729. * @since 3.0.0
  730. *
  731. * @param string $link The taxonomy feed link.
  732. * @param string $feed Feed type.
  733. * @param string $feed The taxonomy name.
  734. */
  735. $link = apply_filters( 'taxonomy_feed_link', $link, $feed, $taxonomy );
  736. }
  737. return $link;
  738. }
  739. /**
  740. * Retrieve permalink for feed of tag.
  741. *
  742. * @since 2.3.0
  743. *
  744. * @param int $tag_id Tag ID.
  745. * @param string $feed Optional. Feed type.
  746. * @return string The feed permalink for the given tag.
  747. */
  748. function get_tag_feed_link($tag_id, $feed = '') {
  749. return get_term_feed_link($tag_id, 'post_tag', $feed);
  750. }
  751. /**
  752. * Retrieve edit tag link.
  753. *
  754. * @since 2.7.0
  755. *
  756. * @param int $tag_id Tag ID
  757. * @param string $taxonomy Taxonomy
  758. * @return string The edit tag link URL for the given tag.
  759. */
  760. function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) {
  761. /**
  762. * Filter the edit link for a tag (or term in another taxonomy).
  763. *
  764. * @since 2.7.0
  765. *
  766. * @param string $link The term edit link.
  767. */
  768. return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag_id, $taxonomy ) );
  769. }
  770. /**
  771. * Display or retrieve edit tag link with formatting.
  772. *
  773. * @since 2.7.0
  774. *
  775. * @param string $link Optional. Anchor text.
  776. * @param string $before Optional. Display before edit link.
  777. * @param string $after Optional. Display after edit link.
  778. * @param object $tag Tag object.
  779. * @return string HTML content.
  780. */
  781. function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
  782. $link = edit_term_link( $link, '', '', $tag, false );
  783. /**
  784. * Filter the anchor tag for the edit link for a tag (or term in another taxonomy).
  785. *
  786. * @since 2.7.0
  787. *
  788. * @param string $link The anchor tag for the edit link.
  789. */
  790. echo $before . apply_filters( 'edit_tag_link', $link ) . $after;
  791. }
  792. /**
  793. * Retrieve edit term url.
  794. *
  795. * @since 3.1.0
  796. *
  797. * @param int $term_id Term ID
  798. * @param string $taxonomy Taxonomy
  799. * @param string $object_type The object type
  800. * @return string The edit term link URL for the given term.
  801. */
  802. function get_edit_term_link( $term_id, $taxonomy, $object_type = '' ) {
  803. $tax = get_taxonomy( $taxonomy );
  804. if ( !current_user_can( $tax->cap->edit_terms ) )
  805. return;
  806. $term = get_term( $term_id, $taxonomy );
  807. $args = array(
  808. 'action' => 'edit',
  809. 'taxonomy' => $taxonomy,
  810. 'tag_ID' => $term->term_id,
  811. );
  812. if ( $object_type )
  813. $args['post_type'] = $object_type;
  814. $location = add_query_arg( $args, admin_url( 'edit-tags.php' ) );
  815. /**
  816. * Filter the edit link for a term.
  817. *
  818. * @since 3.1.0
  819. *
  820. * @param string $location The edit link.
  821. * @param int $term_id Term ID.
  822. * @param string $taxonomy Taxonomy name.
  823. * @param string $object_type The object type (eg. the post type).
  824. */
  825. return apply_filters( 'get_edit_term_link', $location, $term_id, $taxonomy, $object_type );
  826. }
  827. /**
  828. * Display or retrieve edit term link with formatting.
  829. *
  830. * @since 3.1.0
  831. *
  832. * @param string $link Optional. Anchor text.
  833. * @param string $before Optional. Display before edit link.
  834. * @param string $after Optional. Display after edit link.
  835. * @param object $term Term object.
  836. * @return string HTML content.
  837. */
  838. function edit_term_link( $link = '', $before = '', $after = '', $term = null, $echo = true ) {
  839. if ( is_null( $term ) )
  840. $term = get_queried_object();
  841. if ( ! $term )
  842. return;
  843. $tax = get_taxonomy( $term->taxonomy );
  844. if ( ! current_user_can( $tax->cap->edit_terms ) )
  845. return;
  846. if ( empty( $link ) )
  847. $link = __('Edit This');
  848. $link = '<a href="' . get_edit_term_link( $term->term_id, $term->taxonomy ) . '">' . $link . '</a>';
  849. /**
  850. * Filter the anchor tag for the edit link of a term.
  851. *
  852. * @since 3.1.0
  853. *
  854. * @param string $link The anchor tag for the edit link.
  855. * @param int $term_id Term ID.
  856. */
  857. $link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after;
  858. if ( $echo )
  859. echo $link;
  860. else
  861. return $link;
  862. }
  863. /**
  864. * Retrieve permalink for search.
  865. *
  866. * @since 3.0.0
  867. *
  868. * @param string $query Optional. The query string to use. If empty the current query is used.
  869. * @return string The search permalink.
  870. */
  871. function get_search_link( $query = '' ) {
  872. global $wp_rewrite;
  873. if ( empty($query) )
  874. $search = get_search_query( false );
  875. else
  876. $search = stripslashes($query);
  877. $permastruct = $wp_rewrite->get_search_permastruct();
  878. if ( empty( $permastruct ) ) {
  879. $link = home_url('?s=' . urlencode($search) );
  880. } else {
  881. $search = urlencode($search);
  882. $search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it unencoded.
  883. $link = str_replace( '%search%', $search, $permastruct );
  884. $link = home_url( user_trailingslashit( $link, 'search' ) );
  885. }
  886. /**
  887. * Filter the search permalink.
  888. *
  889. * @since 3.0.0
  890. *
  891. * @param string $link Search permalink.
  892. * @param string $search The URL-encoded search term.
  893. */
  894. return apply_filters( 'search_link', $link, $search );
  895. }
  896. /**
  897. * Retrieve the permalink for the feed of the search results.
  898. *
  899. * @since 2.5.0
  900. *
  901. * @param string $search_query Optional. Search query.
  902. * @param string $feed Optional. Feed type.
  903. * @return string The search results feed permalink.
  904. */
  905. function get_search_feed_link($search_query = '', $feed = '') {
  906. global $wp_rewrite;
  907. $link = get_search_link($search_query);
  908. if ( empty($feed) )
  909. $feed = get_default_feed();
  910. $permastruct = $wp_rewrite->get_search_permastruct();
  911. if ( empty($permastruct) ) {
  912. $link = add_query_arg('feed', $feed, $link);
  913. } else {
  914. $link = trailingslashit($link);
  915. $link .= "feed/$feed/";
  916. }
  917. /**
  918. * Filter the search feed link.
  919. *
  920. * @since 2.5.0
  921. *
  922. * @param string $link Search feed link.
  923. * @param string $feed Feed type.
  924. * @param string $type The search type. One of 'posts' or 'comments'.
  925. */
  926. $link = apply_filters( 'search_feed_link', $link, $feed, 'posts' );
  927. return $link;
  928. }
  929. /**
  930. * Retrieve the permalink for the comments feed of the search results.
  931. *
  932. * @since 2.5.0
  933. *
  934. * @param string $search_query Optional. Search query.
  935. * @param string $feed Optional. Feed type.
  936. * @return string The comments feed search results permalink.
  937. */
  938. function get_search_comments_feed_link($search_query = '', $feed = '') {
  939. global $wp_rewrite;
  940. if ( empty($feed) )
  941. $feed = get_default_feed();
  942. $link = get_search_feed_link($search_query, $feed);
  943. $permastruct = $wp_rewrite->get_search_permastruct();
  944. if ( empty($permastruct) )
  945. $link = add_query_arg('feed', 'comments-' . $feed, $link);
  946. else
  947. $link = add_query_arg('withcomments', 1, $link);
  948. /** This filter is documented in wp-includes/link-template.php */
  949. $link = apply_filters('search_feed_link', $link, $feed, 'comments');
  950. return $link;
  951. }
  952. /**
  953. * Retrieve the permalink for a post type archive.
  954. *
  955. * @since 3.1.0
  956. *
  957. * @param string $post_type Post type
  958. * @return string The post type archive permalink.
  959. */
  960. function get_post_type_archive_link( $post_type ) {
  961. global $wp_rewrite;
  962. if ( ! $post_type_obj = get_post_type_object( $post_type ) )
  963. return false;
  964. if ( ! $post_type_obj->has_archive )
  965. return false;
  966. if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
  967. $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive;
  968. if ( $post_type_obj->rewrite['with_front'] )
  969. $struct = $wp_rewrite->front . $struct;
  970. else
  971. $struct = $wp_rewrite->root . $struct;
  972. $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
  973. } else {
  974. $link = home_url( '?post_type=' . $post_type );
  975. }
  976. /**
  977. * Filter the post type archive permalink.
  978. *
  979. * @since 3.1.0
  980. *
  981. * @param string $link The post type archive permalink.
  982. * @param string $post_type Post type name.
  983. */
  984. return apply_filters( 'post_type_archive_link', $link, $post_type );
  985. }
  986. /**
  987. * Retrieve the permalink for a post type archive feed.
  988. *
  989. * @since 3.1.0
  990. *
  991. * @param string $post_type Post type
  992. * @param string $feed Optional. Feed type
  993. * @return string The post type feed permalink.
  994. */
  995. function get_post_type_archive_feed_link( $post_type, $feed = '' ) {
  996. $default_feed = get_default_feed();
  997. if ( empty( $feed ) )
  998. $feed = $default_feed;
  999. if ( ! $link = get_post_type_archive_link( $post_type ) )
  1000. return false;
  1001. $post_type_obj = get_post_type_object( $post_type );
  1002. if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) && $post_type_obj->rewrite['feeds'] ) {
  1003. $link = trailingslashit( $link );
  1004. $link .= 'feed/';
  1005. if ( $feed != $default_feed )
  1006. $link .= "$feed/";
  1007. } else {
  1008. $link = add_query_arg( 'feed', $feed, $link );
  1009. }
  1010. /**
  1011. * Filter the post type archive feed link.
  1012. *
  1013. * @since 3.1.0
  1014. *
  1015. * @param string $link The post type archive feed link.
  1016. * @param string $feed Feed type.
  1017. */
  1018. return apply_filters( 'post_type_archive_feed_link', $link, $feed );
  1019. }
  1020. /**
  1021. * Retrieve edit posts link for post.
  1022. *
  1023. * Can be used within the WordPress loop or outside of it. Can be used with
  1024. * pages, posts, attachments, and revisions.
  1025. *
  1026. * @since 2.3.0
  1027. *
  1028. * @param int $id Optional. Post ID.
  1029. * @param string $context Optional, defaults to display. How to write the '&', defaults to '&amp;'.
  1030. * @return string The edit post link for the given post.
  1031. */
  1032. function get_edit_post_link( $id = 0, $context = 'display' ) {
  1033. if ( ! $post = get_post( $id ) )
  1034. return;
  1035. if ( 'revision' === $post->post_type )
  1036. $action = '';
  1037. elseif ( 'display' == $context )
  1038. $action = '&amp;action=edit';
  1039. else
  1040. $action = '&action=edit';
  1041. $post_type_object = get_post_type_object( $post->post_type );
  1042. if ( !$post_type_object )
  1043. return;
  1044. if ( !current_user_can( 'edit_post', $post->ID ) )
  1045. return;
  1046. /**
  1047. * Filter the post edit link.
  1048. *
  1049. * @since 2.3.0
  1050. *
  1051. * @param string $link The edit link.
  1052. * @param int $post_id Post ID.
  1053. * @param string $context The link context. If set to 'display' then ampersands
  1054. * are encoded.
  1055. */
  1056. return apply_filters( 'get_edit_post_link', admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) ), $post->ID, $context );
  1057. }
  1058. /**
  1059. * Display edit post link for post.
  1060. *
  1061. * @since 1.0.0
  1062. *
  1063. * @param string $text Optional. Anchor text.
  1064. * @param string $before Optional. Display before edit link.
  1065. * @param string $after Optional. Display after edit link.
  1066. * @param int $id Optional. Post ID.
  1067. */
  1068. function edit_post_link( $text = null, $before = '', $after = '', $id = 0 ) {
  1069. if ( ! $post = get_post( $id ) ) {
  1070. return;
  1071. }
  1072. if ( ! $url = get_edit_post_link( $post->ID ) ) {
  1073. return;
  1074. }
  1075. if ( null === $text ) {
  1076. $text = __( 'Edit This' );
  1077. }
  1078. $link = '<a class="post-edit-link" href="' . $url . '">' . $text . '</a>';
  1079. /**
  1080. * Filter the post edit link anchor tag.
  1081. *
  1082. * @since 2.3.0
  1083. *
  1084. * @param string $link Anchor tag for the edit link.
  1085. * @param int $post_id Post ID.
  1086. * @param string $text Anchor text.
  1087. */
  1088. echo $before . apply_filters( 'edit_post_link', $link, $post->ID, $text ) . $after;
  1089. }
  1090. /**
  1091. * Retrieve delete posts link for post.
  1092. *
  1093. * Can be used within the WordPress loop or outside of it, with any post type.
  1094. *
  1095. * @since 2.9.0
  1096. *
  1097. * @param int $id Optional. Post ID.
  1098. * @param string $deprecated Not used.
  1099. * @param bool $force_delete Whether to bypass trash and force deletion. Default is false.
  1100. * @return string The delete post link URL for the given post.
  1101. */
  1102. function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) {
  1103. if ( ! empty( $deprecated ) )
  1104. _deprecated_argument( __FUNCTION__, '3.0' );
  1105. if ( !$post = get_post( $id ) )
  1106. return;
  1107. $post_type_object = get_post_type_object( $post->post_type );
  1108. if ( !$post_type_object )
  1109. return;
  1110. if ( !current_user_can( 'delete_post', $post->ID ) )
  1111. return;
  1112. $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';
  1113. $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );
  1114. /**
  1115. * Filter the post delete link.
  1116. *
  1117. * @since 2.9.0
  1118. *
  1119. * @param string $link The delete link.
  1120. * @param int $post_id Post ID.
  1121. * @param bool $force_delete Whether to bypass the trash and force deletion. Default false.
  1122. */
  1123. return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete );
  1124. }
  1125. /**
  1126. * Retrieve edit comment link.
  1127. *
  1128. * @since 2.3.0
  1129. *
  1130. * @param int $comment_id Optional. Comment ID.
  1131. * @return string The edit comment link URL for the given comment.
  1132. */
  1133. function get_edit_comment_link( $comment_id = 0 ) {
  1134. $comment = get_comment( $comment_id );
  1135. if ( !current_user_can( 'edit_comment', $comment->comment_ID ) )
  1136. return;
  1137. $location = admin_url('comment.php?action=editcomment&amp;c=') . $comment->comment_ID;
  1138. /**
  1139. * Filter the comment edit link.
  1140. *
  1141. * @since 2.3.0
  1142. *
  1143. * @param string $location The edit link.
  1144. */
  1145. return apply_filters( 'get_edit_comment_link', $location );
  1146. }
  1147. /**
  1148. * Display edit comment link with formatting.
  1149. *
  1150. * @since 1.0.0
  1151. *
  1152. * @param string $text Optional. Anchor text.
  1153. * @param string $before Optional. Display before edit link.
  1154. * @param string $after Optional. Display after edit link.
  1155. */
  1156. function edit_comment_link( $text = null, $before = '', $after = '' ) {
  1157. global $comment;
  1158. if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
  1159. return;
  1160. }
  1161. if ( null === $text ) {
  1162. $text = __( 'Edit This' );
  1163. }
  1164. $link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '">' . $text . '</a>';
  1165. /**
  1166. * Filter the comment edit link anchor tag.
  1167. *
  1168. * @since 2.3.0
  1169. *
  1170. * @param string $link Anchor tag for the edit link.
  1171. * @param int $comment_id Comment ID.
  1172. * @param string $text Anchor text.
  1173. */
  1174. echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID, $text ) . $after;
  1175. }
  1176. /**
  1177. * Display edit bookmark (literally a URL external to blog) link.
  1178. *
  1179. * @since 2.7.0
  1180. *
  1181. * @param int $link Optional. Bookmark ID.
  1182. * @return string The edit bookmark link URL.
  1183. */
  1184. function get_edit_bookmark_link( $link = 0 ) {
  1185. $link = get_bookmark( $link );
  1186. if ( !current_user_can('manage_links') )
  1187. return;
  1188. $location = admin_url('link.php?action=edit&amp;link_id=') . $link->link_id;
  1189. /**
  1190. * Filter the bookmark (link) edit link.
  1191. *
  1192. * @since 2.7.0
  1193. *
  1194. * @param string $location The edit link.
  1195. * @param int $link_id Bookmark ID.
  1196. */
  1197. return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
  1198. }
  1199. /**
  1200. * Display edit bookmark (literally a URL external to blog) link anchor content.
  1201. *
  1202. * @since 2.7.0
  1203. *
  1204. * @param string $link Optional. Anchor text.
  1205. * @param string $before Optional. Display before edit link.
  1206. * @param string $after Optional. Display after edit link.
  1207. * @param int $bookmark Optional. Bookmark ID.
  1208. */
  1209. function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
  1210. $bookmark = get_bookmark($bookmark);
  1211. if ( !current_user_can('manage_links') )
  1212. return;
  1213. if ( empty($link) )
  1214. $link = __('Edit This');
  1215. $link = '<a href="' . get_edit_bookmark_link( $bookmark ) . '">' . $link . '</a>';
  1216. /**
  1217. * Filter the bookmark edit link anchor tag.
  1218. *
  1219. * @since 2.7.0
  1220. *
  1221. * @param string $link Anchor tag for the edit link.
  1222. * @param int $link_id Bookmark ID.
  1223. */
  1224. echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
  1225. }
  1226. /**
  1227. * Retrieve edit user link
  1228. *
  1229. * @since 3.5.0
  1230. *
  1231. * @param int $user_id Optional. User ID. Defaults to the current user.
  1232. * @return string URL to edit user page or empty string.
  1233. */
  1234. function get_edit_user_link( $user_id = null ) {
  1235. if ( ! $user_id )
  1236. $user_id = get_current_user_id();
  1237. if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) )
  1238. return '';
  1239. $user = get_userdata( $user_id );
  1240. if ( ! $user )
  1241. return '';
  1242. if ( get_current_user_id() == $user->ID )
  1243. $link = get_edit_profile_url( $user->ID );
  1244. else
  1245. $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) );
  1246. /**
  1247. * Filter the user edit link.
  1248. *
  1249. * @since 3.5.0
  1250. *
  1251. * @param string $link The edit link.
  1252. * @param int $user_id User ID.
  1253. */
  1254. return apply_filters( 'get_edit_user_link', $link, $user->ID );
  1255. }
  1256. // Navigation links
  1257. /**
  1258. * Retrieve previous post that is adjacent to current post.
  1259. *
  1260. * @since 1.5.0
  1261. *
  1262. * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
  1263. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1264. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1265. * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
  1266. */
  1267. function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1268. return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy );
  1269. }
  1270. /**
  1271. * Retrieve next post that is adjacent to current post.
  1272. *
  1273. * @since 1.5.0
  1274. *
  1275. * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
  1276. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1277. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1278. * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
  1279. */
  1280. function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1281. return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy );
  1282. }
  1283. /**
  1284. * Retrieve adjacent post.
  1285. *
  1286. * Can either be next or previous post.
  1287. *
  1288. * @since 2.5.0
  1289. *
  1290. * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
  1291. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1292. * @param bool $previous Optional. Whether to retrieve previous post.
  1293. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1294. * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
  1295. */
  1296. function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
  1297. global $wpdb;
  1298. if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) )
  1299. return null;
  1300. $current_post_date = $post->post_date;
  1301. $join = '';
  1302. $where = '';
  1303. if ( $in_same_term || ! empty( $excluded_terms ) ) {
  1304. $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
  1305. $where = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
  1306. if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
  1307. // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
  1308. if ( false !== strpos( $excluded_terms, ' and ' ) ) {
  1309. _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
  1310. $excluded_terms = explode( ' and ', $excluded_terms );
  1311. } else {
  1312. $excluded_terms = explode( ',', $excluded_terms );
  1313. }
  1314. $excluded_terms = array_map( 'intval', $excluded_terms );
  1315. }
  1316. if ( $in_same_term ) {
  1317. if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
  1318. return '';
  1319. $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
  1320. // Remove any exclusions from the term array to include.
  1321. $term_array = array_diff( $term_array, (array) $excluded_terms );
  1322. $term_array = array_map( 'intval', $term_array );
  1323. if ( ! $term_array || is_wp_error( $term_array ) )
  1324. return '';
  1325. $where .= " AND tt.term_id IN (" . implode( ',', $term_array ) . ")";
  1326. }
  1327. if ( ! empty( $excluded_terms ) ) {
  1328. $where .= " AND p.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships tr LEFT JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE tt.term_id IN (" . implode( $excluded_terms, ',' ) . ') )';
  1329. }
  1330. }
  1331. $adjacent = $previous ? 'previous' : 'next';
  1332. $op = $previous ? '<' : '>';
  1333. $order = $previous ? 'DESC' : 'ASC';
  1334. /**
  1335. * Filter the JOIN clause in the SQL for an adjacent post query.
  1336. *
  1337. * The dynamic portion of the hook name, `$adjacent`, refers to the type
  1338. * of adjacency, 'next' or 'previous'.
  1339. *
  1340. * @since 2.5.0
  1341. *
  1342. * @param string $join The JOIN clause in the SQL.
  1343. * @param bool $in_same_term Whether post should be in a same taxonomy term.
  1344. * @param array $excluded_terms Array of excluded term IDs.
  1345. */
  1346. $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms );
  1347. /**
  1348. * Filter the WHERE clause in the SQL for an adjacent post query.
  1349. *
  1350. * The dynamic portion of the hook name, `$adjacent`, refers to the type
  1351. * of adjacency, 'next' or 'previous'.
  1352. *
  1353. * @since 2.5.0
  1354. *
  1355. * @param string $where The `WHERE` clause in the SQL.
  1356. * @param bool $in_same_term Whether post should be in a same taxonomy term.
  1357. * @param array $excluded_terms Array of excluded term IDs.
  1358. */
  1359. $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $where", $current_post_date, $post->post_type ), $in_same_term, $excluded_terms );
  1360. /**
  1361. * Filter the ORDER BY clause in the SQL for an adjacent post query.
  1362. *
  1363. * The dynamic portion of the hook name, `$adjacent`, refers to the type
  1364. * of adjacency, 'next' or 'previous'.
  1365. *
  1366. * @since 2.5.0
  1367. *
  1368. * @param string $order_by The `ORDER BY` clause in the SQL.
  1369. */
  1370. $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
  1371. $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
  1372. $query_key = 'adjacent_post_' . md5( $query );
  1373. $result = wp_cache_get( $query_key, 'counts' );
  1374. if ( false !== $result ) {
  1375. if ( $result )
  1376. $result = get_post( $result );
  1377. return $result;
  1378. }
  1379. $result = $wpdb->get_var( $query );
  1380. if ( null === $result )
  1381. $result = '';
  1382. wp_cache_set( $query_key, $result, 'counts' );
  1383. if ( $result )
  1384. $result = get_post( $result );
  1385. return $result;
  1386. }
  1387. /**
  1388. * Get adjacent post relational link.
  1389. *
  1390. * Can either be next or previous post relational link.
  1391. *
  1392. * @since 2.8.0
  1393. *
  1394. * @param string $title Optional. Link title format.
  1395. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1396. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1397. * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
  1398. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1399. * @return string The adjacent post relational link URL.
  1400. */
  1401. function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
  1402. if ( $previous && is_attachment() && $post = get_post() )
  1403. $post = get_post( $post->post_parent );
  1404. else
  1405. $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
  1406. if ( empty( $post ) )
  1407. return;
  1408. $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) );
  1409. if ( empty( $post_title ) )
  1410. $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
  1411. $date = mysql2date( get_option( 'date_format' ), $post->post_date );
  1412. $title = str_replace( '%title', $post_title, $title );
  1413. $title = str_replace( '%date', $date, $title );
  1414. $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
  1415. $link .= esc_attr( $title );
  1416. $link .= "' href='" . get_permalink( $post ) . "' />\n";
  1417. $adjacent = $previous ? 'previous' : 'next';
  1418. /**
  1419. * Filter the adjacent post relational link.
  1420. *
  1421. * The dynamic portion of the hook name, `$adjacent`, refers to the type
  1422. * of adjacency, 'next' or 'previous'.
  1423. *
  1424. * @since 2.8.0
  1425. *
  1426. * @param string $link The relational link.
  1427. */
  1428. return apply_filters( "{$adjacent}_post_rel_link", $link );
  1429. }
  1430. /**
  1431. * Display relational links for the posts adjacent to the current post.
  1432. *
  1433. * @since 2.8.0
  1434. *
  1435. * @param string $title Optional. Link title format.
  1436. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1437. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1438. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1439. */
  1440. function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1441. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
  1442. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
  1443. }
  1444. /**
  1445. * Display relational links for the posts adjacent to the current post for single post pages.
  1446. *
  1447. * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins or theme templates.
  1448. * @since 3.0.0
  1449. *
  1450. */
  1451. function adjacent_posts_rel_link_wp_head() {
  1452. if ( ! is_single() || is_attachment() ) {
  1453. return;
  1454. }
  1455. adjacent_posts_rel_link();
  1456. }
  1457. /**
  1458. * Display relational link for the next post adjacent to the current post.
  1459. *
  1460. * @since 2.8.0
  1461. *
  1462. * @param string $title Optional. Link title format.
  1463. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1464. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1465. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1466. */
  1467. function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1468. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
  1469. }
  1470. /**
  1471. * Display relational link for the previous post adjacent to the current post.
  1472. *
  1473. * @since 2.8.0
  1474. *
  1475. * @param string $title Optional. Link title format.
  1476. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1477. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default true.
  1478. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1479. */
  1480. function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1481. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
  1482. }
  1483. /**
  1484. * Retrieve boundary post.
  1485. *
  1486. * Boundary being either the first or last post by publish date within the constraints specified
  1487. * by $in_same_term or $excluded_terms.
  1488. *
  1489. * @since 2.8.0
  1490. *
  1491. * @param bool $in_same_term Optional. Whether returned post should be in a same taxonomy term.
  1492. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1493. * @param bool $start Optional. Whether to retrieve first or last post.
  1494. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1495. * @return mixed Array containing the boundary post object if successful, null otherwise.
  1496. */
  1497. function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
  1498. $post = get_post();
  1499. if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
  1500. return null;
  1501. $query_args = array(
  1502. 'posts_per_page' => 1,
  1503. 'order' => $start ? 'ASC' : 'DESC',
  1504. 'update_post_term_cache' => false,
  1505. 'update_post_meta_cache' => false
  1506. );
  1507. $term_array = array();
  1508. if ( ! is_array( $excluded_terms ) ) {
  1509. if ( ! empty( $excluded_terms ) )
  1510. $excluded_terms = explode( ',', $excluded_terms );
  1511. else
  1512. $excluded_terms = array();
  1513. }
  1514. if ( $in_same_term || ! empty( $excluded_terms ) ) {
  1515. if ( $in_same_term )
  1516. $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
  1517. if ( ! empty( $excluded_terms ) ) {
  1518. $excluded_terms = array_map( 'intval', $excluded_terms );
  1519. $excluded_terms = array_diff( $excluded_terms, $term_array );
  1520. $inverse_terms = array();
  1521. foreach ( $excluded_terms as $excluded_term )
  1522. $inverse_terms[] = $excluded_term * -1;
  1523. $excluded_terms = $inverse_terms;
  1524. }
  1525. $query_args[ 'tax_query' ] = array( array(
  1526. 'taxonomy' => $taxonomy,
  1527. 'terms' => array_merge( $term_array, $excluded_terms )
  1528. ) );
  1529. }
  1530. return get_posts( $query_args );
  1531. }
  1532. /*
  1533. * Get previous post link that is adjacent to the current post.
  1534. *
  1535. * @since 3.7.0
  1536. *
  1537. * @param string $format Optional. Link anchor format.
  1538. * @param string $link Optional. Link permalink format.
  1539. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1540. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1541. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1542. * @return string The link URL of the previous post in relation to the current post.
  1543. */
  1544. function get_previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1545. return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy );
  1546. }
  1547. /**
  1548. * Display previous post link that is adjacent to the current post.
  1549. *
  1550. * @since 1.5.0
  1551. * @see get_previous_post_link()
  1552. *
  1553. * @param string $format Optional. Link anchor format.
  1554. * @param string $link Optional. Link permalink format.
  1555. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1556. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1557. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1558. */
  1559. function previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1560. echo get_previous_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
  1561. }
  1562. /**
  1563. * Get next post link that is adjacent to the current post.
  1564. *
  1565. * @since 3.7.0
  1566. *
  1567. * @param string $format Optional. Link anchor format.
  1568. * @param string $link Optional. Link permalink format.
  1569. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1570. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1571. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1572. * @return string The link URL of the next post in relation to the current post.
  1573. */
  1574. function get_next_post_link( $format = '%link &raquo;', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1575. return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy );
  1576. }
  1577. /**
  1578. * Display next post link that is adjacent to the current post.
  1579. *
  1580. * @since 1.5.0
  1581. * @see get_next_post_link()
  1582. *
  1583. * @param string $format Optional. Link anchor format.
  1584. * @param string $link Optional. Link permalink format.
  1585. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1586. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
  1587. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1588. */
  1589. function next_post_link( $format = '%link &raquo;', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  1590. echo get_next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
  1591. }
  1592. /**
  1593. * Get adjacent post link.
  1594. *
  1595. * Can be either next post link or previous.
  1596. *
  1597. * @since 3.7.0
  1598. *
  1599. * @param string $format Link anchor format.
  1600. * @param string $link Link permalink format.
  1601. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1602. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs.
  1603. * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
  1604. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1605. * @return string The link URL of the previous or next post in relation to the current post.
  1606. */
  1607. function get_adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
  1608. if ( $previous && is_attachment() )
  1609. $post = get_post( get_post()->post_parent );
  1610. else
  1611. $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
  1612. if ( ! $post ) {
  1613. $output = '';
  1614. } else {
  1615. $title = $post->post_title;
  1616. if ( empty( $post->post_title ) )
  1617. $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
  1618. /** This filter is documented in wp-includes/post-template.php */
  1619. $title = apply_filters( 'the_title', $title, $post->ID );
  1620. $date = mysql2date( get_option( 'date_format' ), $post->post_date );
  1621. $rel = $previous ? 'prev' : 'next';
  1622. $string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">';
  1623. $inlink = str_replace( '%title', $title, $link );
  1624. $inlink = str_replace( '%date', $date, $inlink );
  1625. $inlink = $string . $inlink . '</a>';
  1626. $output = str_replace( '%link', $inlink, $format );
  1627. }
  1628. $adjacent = $previous ? 'previous' : 'next';
  1629. /**
  1630. * Filter the adjacent post link.
  1631. *
  1632. * The dynamic portion of the hook name, `$adjacent`, refers to the type
  1633. * of adjacency, 'next' or 'previous'.
  1634. *
  1635. * @since 2.6.0
  1636. *
  1637. * @param string $output The adjacent post link.
  1638. * @param string $format Link anchor format.
  1639. * @param string $link Link permalink format.
  1640. * @param WP_Post $post The adjacent post.
  1641. */
  1642. return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post );
  1643. }
  1644. /**
  1645. * Display adjacent post link.
  1646. *
  1647. * Can be either next post link or previous.
  1648. *
  1649. * @since 2.5.0
  1650. *
  1651. * @param string $format Link anchor format.
  1652. * @param string $link Link permalink format.
  1653. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
  1654. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded category IDs.
  1655. * @param bool $previous Optional. Whether to display link to previous or next post. Default true.
  1656. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
  1657. */
  1658. function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
  1659. echo get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, $previous, $taxonomy );
  1660. }
  1661. /**
  1662. * Retrieve links for page numbers.
  1663. *
  1664. * @since 1.5.0
  1665. *
  1666. * @param int $pagenum Optional. Page ID.
  1667. * @param bool $escape Optional. Whether to escape the URL for display, with esc_url(). Defaults to true.
  1668. * Otherwise, prepares the URL with esc_url_raw().
  1669. * @return string The link URL for the given page number.
  1670. */
  1671. function get_pagenum_link($pagenum = 1, $escape = true ) {
  1672. global $wp_rewrite;
  1673. $pagenum = (int) $pagenum;
  1674. $request = remove_query_arg( 'paged' );
  1675. $home_root = parse_url(home_url());
  1676. $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
  1677. $home_root = preg_quote( $home_root, '|' );
  1678. $request = preg_replace('|^'. $home_root . '|i', '', $request);
  1679. $request = preg_replace('|^/+|', '', $request);
  1680. if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
  1681. $base = trailingslashit( get_bloginfo( 'url' ) );
  1682. if ( $pagenum > 1 ) {
  1683. $result = add_query_arg( 'paged', $pagenum, $base . $request );
  1684. } else {
  1685. $result = $base . $request;
  1686. }
  1687. } else {
  1688. $qs_regex = '|\?.*?$|';
  1689. preg_match( $qs_regex, $request, $qs_match );
  1690. if ( !empty( $qs_match[0] ) ) {
  1691. $query_string = $qs_match[0];
  1692. $request = preg_replace( $qs_regex, '', $request );
  1693. } else {
  1694. $query_string = '';
  1695. }
  1696. $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request);
  1697. $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request);
  1698. $request = ltrim($request, '/');
  1699. $base = trailingslashit( get_bloginfo( 'url' ) );
  1700. if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
  1701. $base .= $wp_rewrite->index . '/';
  1702. if ( $pagenum > 1 ) {
  1703. $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' );
  1704. }
  1705. $result = $base . $request . $query_string;
  1706. }
  1707. /**
  1708. * Filter the page number link for the current request.
  1709. *
  1710. * @since 2.5.0
  1711. *
  1712. * @param string $result The page number link.
  1713. */
  1714. $result = apply_filters( 'get_pagenum_link', $result );
  1715. if ( $escape )
  1716. return esc_url( $result );
  1717. else
  1718. return esc_url_raw( $result );
  1719. }
  1720. /**
  1721. * Retrieve next posts page link.
  1722. *
  1723. * Backported from 2.1.3 to 2.0.10.
  1724. *
  1725. * @since 2.0.10
  1726. *
  1727. * @param int $max_page Optional. Max pages.
  1728. * @return string The link URL for next posts page.
  1729. */
  1730. function get_next_posts_page_link($max_page = 0) {
  1731. global $paged;
  1732. if ( !is_single() ) {
  1733. if ( !$paged )
  1734. $paged = 1;
  1735. $nextpage = intval($paged) + 1;
  1736. if ( !$max_page || $max_page >= $nextpage )
  1737. return get_pagenum_link($nextpage);
  1738. }
  1739. }
  1740. /**
  1741. * Display or return the next posts page link.
  1742. *
  1743. * @since 0.71
  1744. *
  1745. * @param int $max_page Optional. Max pages.
  1746. * @param boolean $echo Optional. Echo or return;
  1747. * @return string The link URL for next posts page if `$echo = false`.
  1748. */
  1749. function next_posts( $max_page = 0, $echo = true ) {
  1750. $output = esc_url( get_next_posts_page_link( $max_page ) );
  1751. if ( $echo )
  1752. echo $output;
  1753. else
  1754. return $output;
  1755. }
  1756. /**
  1757. * Return the next posts page link.
  1758. *
  1759. * @since 2.7.0
  1760. *
  1761. * @param string $label Content for link text.
  1762. * @param int $max_page Optional. Max pages.
  1763. * @return string|null HTML-formatted next posts page link.
  1764. */
  1765. function get_next_posts_link( $label = null, $max_page = 0 ) {
  1766. global $paged, $wp_query;
  1767. if ( !$max_page )
  1768. $max_page = $wp_query->max_num_pages;
  1769. if ( !$paged )
  1770. $paged = 1;
  1771. $nextpage = intval($paged) + 1;
  1772. if ( null === $label )
  1773. $label = __( 'Next Page &raquo;' );
  1774. if ( !is_single() && ( $nextpage <= $max_page ) ) {
  1775. /**
  1776. * Filter the anchor tag attributes for the next posts page link.
  1777. *
  1778. * @since 2.7.0
  1779. *
  1780. * @param string $attributes Attributes for the anchor tag.
  1781. */
  1782. $attr = apply_filters( 'next_posts_link_attributes', '' );
  1783. return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label) . '</a>';
  1784. }
  1785. }
  1786. /**
  1787. * Display the next posts page link.
  1788. *
  1789. * @since 0.71
  1790. *
  1791. * @param string $label Content for link text.
  1792. * @param int $max_page Optional. Max pages.
  1793. */
  1794. function next_posts_link( $label = null, $max_page = 0 ) {
  1795. echo get_next_posts_link( $label, $max_page );
  1796. }
  1797. /**
  1798. * Retrieve previous posts page link.
  1799. *
  1800. * Will only return string, if not on a single page or post.
  1801. *
  1802. * Backported to 2.0.10 from 2.1.3.
  1803. *
  1804. * @since 2.0.10
  1805. *
  1806. * @return string|null The link for the previous posts page.
  1807. */
  1808. function get_previous_posts_page_link() {
  1809. global $paged;
  1810. if ( !is_single() ) {
  1811. $nextpage = intval($paged) - 1;
  1812. if ( $nextpage < 1 )
  1813. $nextpage = 1;
  1814. return get_pagenum_link($nextpage);
  1815. }
  1816. }
  1817. /**
  1818. * Display or return the previous posts page link.
  1819. *
  1820. * @since 0.71
  1821. *
  1822. * @param boolean $echo Optional. Echo or return;
  1823. * @return string The previous posts page link if `$echo = false`.
  1824. */
  1825. function previous_posts( $echo = true ) {
  1826. $output = esc_url( get_previous_posts_page_link() );
  1827. if ( $echo )
  1828. echo $output;
  1829. else
  1830. return $output;
  1831. }
  1832. /**
  1833. * Return the previous posts page link.
  1834. *
  1835. * @since 2.7.0
  1836. *
  1837. * @param string $label Optional. Previous page link text.
  1838. * @return string|null HTML-formatted previous page link.
  1839. */
  1840. function get_previous_posts_link( $label = null ) {
  1841. global $paged;
  1842. if ( null === $label )
  1843. $label = __( '&laquo; Previous Page' );
  1844. if ( !is_single() && $paged > 1 ) {
  1845. /**
  1846. * Filter the anchor tag attributes for the previous posts page link.
  1847. *
  1848. * @since 2.7.0
  1849. *
  1850. * @param string $attributes Attributes for the anchor tag.
  1851. */
  1852. $attr = apply_filters( 'previous_posts_link_attributes', '' );
  1853. return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label ) .'</a>';
  1854. }
  1855. }
  1856. /**
  1857. * Display the previous posts page link.
  1858. *
  1859. * @since 0.71
  1860. *
  1861. * @param string $label Optional. Previous page link text.
  1862. */
  1863. function previous_posts_link( $label = null ) {
  1864. echo get_previous_posts_link( $label );
  1865. }
  1866. /**
  1867. * Return post pages link navigation for previous and next pages.
  1868. *
  1869. * @since 2.8.0
  1870. *
  1871. * @param string|array $args Optional args.
  1872. * @return string The posts link navigation.
  1873. */
  1874. function get_posts_nav_link( $args = array() ) {
  1875. global $wp_query;
  1876. $return = '';
  1877. if ( !is_singular() ) {
  1878. $defaults = array(
  1879. 'sep' => ' &#8212; ',
  1880. 'prelabel' => __('&laquo; Previous Page'),
  1881. 'nxtlabel' => __('Next Page &raquo;'),
  1882. );
  1883. $args = wp_parse_args( $args, $defaults );
  1884. $max_num_pages = $wp_query->max_num_pages;
  1885. $paged = get_query_var('paged');
  1886. //only have sep if there's both prev and next results
  1887. if ($paged < 2 || $paged >= $max_num_pages) {
  1888. $args['sep'] = '';
  1889. }
  1890. if ( $max_num_pages > 1 ) {
  1891. $return = get_previous_posts_link($args['prelabel']);
  1892. $return .= preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $args['sep']);
  1893. $return .= get_next_posts_link($args['nxtlabel']);
  1894. }
  1895. }
  1896. return $return;
  1897. }
  1898. /**
  1899. * Display post pages link navigation for previous and next pages.
  1900. *
  1901. * @since 0.71
  1902. *
  1903. * @param string $sep Optional. Separator for posts navigation links.
  1904. * @param string $prelabel Optional. Label for previous pages.
  1905. * @param string $nxtlabel Optional Label for next pages.
  1906. */
  1907. function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
  1908. $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
  1909. echo get_posts_nav_link($args);
  1910. }
  1911. /**
  1912. * Return navigation to next/previous post when applicable.
  1913. *
  1914. * @since 4.1.0
  1915. *
  1916. * @param array $args {
  1917. * Optional. Default post navigation arguments. Default empty array.
  1918. *
  1919. * @type string $prev_text Anchor text to display in the previous post link. Default `%title`.
  1920. * @type string $next_text Anchor text to display in the next post link. Default `%title`.
  1921. * @type string $screen_reader_text Screen reader text for nav element. Default 'Post navigation'.
  1922. * }
  1923. * @return string Markup for post links.
  1924. */
  1925. function get_the_post_navigation( $args = array() ) {
  1926. $args = wp_parse_args( $args, array(
  1927. 'prev_text' => '%title',
  1928. 'next_text' => '%title',
  1929. 'screen_reader_text' => __( 'Post navigation' ),
  1930. ) );
  1931. $navigation = '';
  1932. $previous = get_previous_post_link( '<div class="nav-previous">%link</div>', $args['prev_text'] );
  1933. $next = get_next_post_link( '<div class="nav-next">%link</div>', $args['next_text'] );
  1934. // Only add markup if there's somewhere to navigate to.
  1935. if ( $previous || $next ) {
  1936. $navigation = _navigation_markup( $previous . $next, 'post-navigation', $args['screen_reader_text'] );
  1937. }
  1938. return $navigation;
  1939. }
  1940. /**
  1941. * Display navigation to next/previous post when applicable.
  1942. *
  1943. * @since 4.1.0
  1944. *
  1945. * @param array $args Optional. See {@see get_the_post_navigation()} for available
  1946. * arguments. Default empty array.
  1947. */
  1948. function the_post_navigation( $args = array() ) {
  1949. echo get_the_post_navigation( $args );
  1950. }
  1951. /**
  1952. * Return navigation to next/previous set of posts when applicable.
  1953. *
  1954. * @since 4.1.0
  1955. *
  1956. * @global WP_Query $wp_query WordPress Query object.
  1957. *
  1958. * @param array $args {
  1959. * Optional. Default posts navigation arguments. Default empty array.
  1960. *
  1961. * @type string $prev_text Anchor text to display in the previous posts link.
  1962. * Default 'Older posts'.
  1963. * @type string $next_text Anchor text to display in the next posts link.
  1964. * Default 'Newer posts'.
  1965. * @type string $screen_reader_text Screen reader text for nav element.
  1966. * Default 'Posts navigation'.
  1967. * }
  1968. * @return string Markup for posts links.
  1969. */
  1970. function get_the_posts_navigation( $args = array() ) {
  1971. $navigation = '';
  1972. // Don't print empty markup if there's only one page.
  1973. if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
  1974. $args = wp_parse_args( $args, array(
  1975. 'prev_text' => __( 'Older posts' ),
  1976. 'next_text' => __( 'Newer posts' ),
  1977. 'screen_reader_text' => __( 'Posts navigation' ),
  1978. ) );
  1979. $next_link = get_previous_posts_link( $args['next_text'] );
  1980. $prev_link = get_next_posts_link( $args['prev_text'] );
  1981. if ( $prev_link ) {
  1982. $navigation .= '<div class="nav-previous">' . $prev_link . '</div>';
  1983. }
  1984. if ( $next_link ) {
  1985. $navigation .= '<div class="nav-next">' . $next_link . '</div>';
  1986. }
  1987. $navigation = _navigation_markup( $navigation, 'posts-navigation', $args['screen_reader_text'] );
  1988. }
  1989. return $navigation;
  1990. }
  1991. /**
  1992. * Display navigation to next/previous set of posts when applicable.
  1993. *
  1994. * @since 4.1.0
  1995. *
  1996. * @param array $args Optional. See {@see get_the_posts_navigation()} for available
  1997. * arguments. Default empty array.
  1998. */
  1999. function the_posts_navigation( $args = array() ) {
  2000. echo get_the_posts_navigation( $args );
  2001. }
  2002. /**
  2003. * Return a paginated navigation to next/previous set of posts,
  2004. * when applicable.
  2005. *
  2006. * @since 4.1.0
  2007. *
  2008. * @param array $args {
  2009. * Optional. Default pagination arguments, {@see paginate_links()}.
  2010. *
  2011. * @type string $screen_reader_text Screen reader text for navigation element.
  2012. * Default 'Posts navigation'.
  2013. * }
  2014. * @return string Markup for pagination links.
  2015. */
  2016. function get_the_posts_pagination( $args = array() ) {
  2017. $navigation = '';
  2018. // Don't print empty markup if there's only one page.
  2019. if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
  2020. $args = wp_parse_args( $args, array(
  2021. 'mid_size' => 1,
  2022. 'prev_text' => __( 'Previous' ),
  2023. 'next_text' => __( 'Next' ),
  2024. 'screen_reader_text' => __( 'Posts navigation' ),
  2025. ) );
  2026. // Make sure we get a string back. Plain is the next best thing.
  2027. if ( isset( $args['type'] ) && 'array' == $args['type'] ) {
  2028. $args['type'] = 'plain';
  2029. }
  2030. // Set up paginated links.
  2031. $links = paginate_links( $args );
  2032. if ( $links ) {
  2033. $navigation = _navigation_markup( $links, 'pagination', $args['screen_reader_text'] );
  2034. }
  2035. }
  2036. return $navigation;
  2037. }
  2038. /**
  2039. * Display a paginated navigation to next/previous set of posts,
  2040. * when applicable.
  2041. *
  2042. * @since 4.1.0
  2043. *
  2044. * @param array $args Optional. See {@see get_the_posts_pagination()} for available arguments.
  2045. * Default empty array.
  2046. */
  2047. function the_posts_pagination( $args = array() ) {
  2048. echo get_the_posts_pagination( $args );
  2049. }
  2050. /**
  2051. * Wraps passed links in navigational markup.
  2052. *
  2053. * @since 4.1.0
  2054. * @access private
  2055. *
  2056. * @param string $links Navigational links.
  2057. * @param string $class Optional. Custom class for nav element. Default: 'posts-navigation'.
  2058. * @param string $screen_reader_text Optional. Screen reader text for nav element. Default: 'Posts navigation'.
  2059. * @return string Navigation template tag.
  2060. */
  2061. function _navigation_markup( $links, $class = 'posts-navigation', $screen_reader_text = '' ) {
  2062. if ( empty( $screen_reader_text ) ) {
  2063. $screen_reader_text = __( 'Posts navigation' );
  2064. }
  2065. $template = '
  2066. <nav class="navigation %1$s" role="navigation">
  2067. <h2 class="screen-reader-text">%2$s</h2>
  2068. <div class="nav-links">%3$s</div>
  2069. </nav>';
  2070. return sprintf( $template, sanitize_html_class( $class ), esc_html( $screen_reader_text ), $links );
  2071. }
  2072. /**
  2073. * Retrieve comments page number link.
  2074. *
  2075. * @since 2.7.0
  2076. *
  2077. * @param int $pagenum Optional. Page number.
  2078. * @param int $max_page Optional. The maximum number of comment pages.
  2079. * @return string The comments page number link URL.
  2080. */
  2081. function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
  2082. global $wp_rewrite;
  2083. $pagenum = (int) $pagenum;
  2084. $result = get_permalink();
  2085. if ( 'newest' == get_option('default_comments_page') ) {
  2086. if ( $pagenum != $max_page ) {
  2087. if ( $wp_rewrite->using_permalinks() )
  2088. $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
  2089. else
  2090. $result = add_query_arg( 'cpage', $pagenum, $result );
  2091. }
  2092. } elseif ( $pagenum > 1 ) {
  2093. if ( $wp_rewrite->using_permalinks() )
  2094. $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
  2095. else
  2096. $result = add_query_arg( 'cpage', $pagenum, $result );
  2097. }
  2098. $result .= '#comments';
  2099. /**
  2100. * Filter the comments page number link for the current request.
  2101. *
  2102. * @since 2.7.0
  2103. *
  2104. * @param string $result The comments page number link.
  2105. */
  2106. $result = apply_filters( 'get_comments_pagenum_link', $result );
  2107. return $result;
  2108. }
  2109. /**
  2110. * Return the link to next comments page.
  2111. *
  2112. * @since 2.7.1
  2113. *
  2114. * @param string $label Optional. Label for link text.
  2115. * @param int $max_page Optional. Max page.
  2116. * @return string|null HTML-formatted link for the next page of comments.
  2117. */
  2118. function get_next_comments_link( $label = '', $max_page = 0 ) {
  2119. global $wp_query;
  2120. if ( !is_singular() || !get_option('page_comments') )
  2121. return;
  2122. $page = get_query_var('cpage');
  2123. $nextpage = intval($page) + 1;
  2124. if ( empty($max_page) )
  2125. $max_page = $wp_query->max_num_comment_pages;
  2126. if ( empty($max_page) )
  2127. $max_page = get_comment_pages_count();
  2128. if ( $nextpage > $max_page )
  2129. return;
  2130. if ( empty($label) )
  2131. $label = __('Newer Comments &raquo;');
  2132. /**
  2133. * Filter the anchor tag attributes for the next comments page link.
  2134. *
  2135. * @since 2.7.0
  2136. *
  2137. * @param string $attributes Attributes for the anchor tag.
  2138. */
  2139. return '<a href="' . esc_url( get_comments_pagenum_link( $nextpage, $max_page ) ) . '" ' . apply_filters( 'next_comments_link_attributes', '' ) . '>'. preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label) .'</a>';
  2140. }
  2141. /**
  2142. * Display the link to next comments page.
  2143. *
  2144. * @since 2.7.0
  2145. *
  2146. * @param string $label Optional. Label for link text.
  2147. * @param int $max_page Optional. Max page.
  2148. */
  2149. function next_comments_link( $label = '', $max_page = 0 ) {
  2150. echo get_next_comments_link( $label, $max_page );
  2151. }
  2152. /**
  2153. * Return the previous comments page link.
  2154. *
  2155. * @since 2.7.1
  2156. *
  2157. * @param string $label Optional. Label for comments link text.
  2158. * @return string|null HTML-formatted link for the previous page of comments.
  2159. */
  2160. function get_previous_comments_link( $label = '' ) {
  2161. if ( !is_singular() || !get_option('page_comments') )
  2162. return;
  2163. $page = get_query_var('cpage');
  2164. if ( intval($page) <= 1 )
  2165. return;
  2166. $prevpage = intval($page) - 1;
  2167. if ( empty($label) )
  2168. $label = __('&laquo; Older Comments');
  2169. /**
  2170. * Filter the anchor tag attributes for the previous comments page link.
  2171. *
  2172. * @since 2.7.0
  2173. *
  2174. * @param string $attributes Attributes for the anchor tag.
  2175. */
  2176. return '<a href="' . esc_url( get_comments_pagenum_link( $prevpage ) ) . '" ' . apply_filters( 'previous_comments_link_attributes', '' ) . '>' . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label) .'</a>';
  2177. }
  2178. /**
  2179. * Display the previous comments page link.
  2180. *
  2181. * @since 2.7.0
  2182. *
  2183. * @param string $label Optional. Label for comments link text.
  2184. */
  2185. function previous_comments_link( $label = '' ) {
  2186. echo get_previous_comments_link( $label );
  2187. }
  2188. /**
  2189. * Create pagination links for the comments on the current post.
  2190. *
  2191. * @see paginate_links()
  2192. * @since 2.7.0
  2193. *
  2194. * @param string|array $args Optional args. See paginate_links().
  2195. * @return string Markup for pagination links.
  2196. */
  2197. function paginate_comments_links($args = array()) {
  2198. global $wp_rewrite;
  2199. if ( !is_singular() || !get_option('page_comments') )
  2200. return;
  2201. $page = get_query_var('cpage');
  2202. if ( !$page )
  2203. $page = 1;
  2204. $max_page = get_comment_pages_count();
  2205. $defaults = array(
  2206. 'base' => add_query_arg( 'cpage', '%#%' ),
  2207. 'format' => '',
  2208. 'total' => $max_page,
  2209. 'current' => $page,
  2210. 'echo' => true,
  2211. 'add_fragment' => '#comments'
  2212. );
  2213. if ( $wp_rewrite->using_permalinks() )
  2214. $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . 'comment-page-%#%', 'commentpaged');
  2215. $args = wp_parse_args( $args, $defaults );
  2216. $page_links = paginate_links( $args );
  2217. if ( $args['echo'] )
  2218. echo $page_links;
  2219. else
  2220. return $page_links;
  2221. }
  2222. /**
  2223. * Retrieve the Press This bookmarklet link.
  2224. *
  2225. * Use this in 'a' element 'href' attribute.
  2226. *
  2227. * @since 2.6.0
  2228. *
  2229. * @return string The Press This bookmarklet link URL.
  2230. */
  2231. function get_shortcut_link() {
  2232. // In case of breaking changes, version this. #WP20071
  2233. $link = "javascript:
  2234. var d=document,
  2235. w=window,
  2236. e=w.getSelection,
  2237. k=d.getSelection,
  2238. x=d.selection,
  2239. s=(e?e():(k)?k():(x?x.createRange().text:0)),
  2240. f='" . admin_url('press-this.php') . "',
  2241. l=d.location,
  2242. e=encodeURIComponent,
  2243. u=f+'?u='+e(l.href)+'&t='+e(d.title)+'&s='+e(s)+'&v=4';
  2244. a=function(){if(!w.open(u,'t','toolbar=0,resizable=1,scrollbars=1,status=1,width=720,height=570'))l.href=u;};
  2245. if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0); else a();
  2246. void(0)";
  2247. $link = str_replace(array("\r", "\n", "\t"), '', $link);
  2248. /**
  2249. * Filter the Press This bookmarklet link.
  2250. *
  2251. * @since 2.6.0
  2252. *
  2253. * @param string $link The Press This bookmarklet link.
  2254. */
  2255. return apply_filters( 'shortcut_link', $link );
  2256. }
  2257. /**
  2258. * Retrieve the home url for the current site.
  2259. *
  2260. * Returns the 'home' option with the appropriate protocol, 'https' if
  2261. * {@see is_ssl()} and 'http' otherwise. If `$scheme` is 'http' or 'https',
  2262. * `is_ssl()` is overridden.
  2263. *
  2264. * @since 3.0.0
  2265. *
  2266. * @param string $path Optional. Path relative to the home url. Default empty.
  2267. * @param string $scheme Optional. Scheme to give the home url context. Accepts
  2268. * 'http', 'https', or 'relative'. Default null.
  2269. * @return string Home url link with optional path appended.
  2270. */
  2271. function home_url( $path = '', $scheme = null ) {
  2272. return get_home_url( null, $path, $scheme );
  2273. }
  2274. /**
  2275. * Retrieve the home url for a given site.
  2276. *
  2277. * Returns the 'home' option with the appropriate protocol, 'https' if
  2278. * {@see is_ssl()} and 'http' otherwise. If `$scheme` is 'http' or 'https',
  2279. * `is_ssl()` is
  2280. * overridden.
  2281. *
  2282. * @since 3.0.0
  2283. *
  2284. * @param int $blog_id Optional. Blog ID. Default null (current blog).
  2285. * @param string $path Optional. Path relative to the home URL. Default empty.
  2286. * @param string|null $orig_scheme Optional. Scheme to give the home URL context. Accepts
  2287. * 'http', 'https', 'relative', or null. Default null.
  2288. * @return string Home URL link with optional path appended.
  2289. */
  2290. function get_home_url( $blog_id = null, $path = '', $scheme = null ) {
  2291. $orig_scheme = $scheme;
  2292. if ( empty( $blog_id ) || !is_multisite() ) {
  2293. $url = get_option( 'home' );
  2294. } else {
  2295. switch_to_blog( $blog_id );
  2296. $url = get_option( 'home' );
  2297. restore_current_blog();
  2298. }
  2299. if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) {
  2300. if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $GLOBALS['pagenow'] )
  2301. $scheme = 'https';
  2302. else
  2303. $scheme = parse_url( $url, PHP_URL_SCHEME );
  2304. }
  2305. $url = set_url_scheme( $url, $scheme );
  2306. if ( $path && is_string( $path ) )
  2307. $url .= '/' . ltrim( $path, '/' );
  2308. /**
  2309. * Filter the home URL.
  2310. *
  2311. * @since 3.0.0
  2312. *
  2313. * @param string $url The complete home URL including scheme and path.
  2314. * @param string $path Path relative to the home URL. Blank string if no path is specified.
  2315. * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https', 'relative' or null.
  2316. * @param int|null $blog_id Blog ID, or null for the current blog.
  2317. */
  2318. return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id );
  2319. }
  2320. /**
  2321. * Retrieve the site url for the current site.
  2322. *
  2323. * Returns the 'site_url' option with the appropriate protocol, 'https' if
  2324. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
  2325. * overridden.
  2326. *
  2327. * @since 3.0.0
  2328. *
  2329. * @param string $path Optional. Path relative to the site url.
  2330. * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
  2331. * @return string Site url link with optional path appended.
  2332. */
  2333. function site_url( $path = '', $scheme = null ) {
  2334. return get_site_url( null, $path, $scheme );
  2335. }
  2336. /**
  2337. * Retrieve the site url for a given site.
  2338. *
  2339. * Returns the 'site_url' option with the appropriate protocol, 'https' if
  2340. * {@see is_ssl()} and 'http' otherwise. If `$scheme` is 'http' or 'https',
  2341. * `is_ssl()` is overridden.
  2342. *
  2343. * @since 3.0.0
  2344. *
  2345. * @param int $blog_id Optional. Blog ID. Default null (current site).
  2346. * @param string $path Optional. Path relative to the site url. Default empty.
  2347. * @param string $scheme Optional. Scheme to give the site url context. Accepts
  2348. * 'http', 'https', 'login', 'login_post', 'admin', or
  2349. * 'relative'. Default null.
  2350. * @return string Site url link with optional path appended.
  2351. */
  2352. function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
  2353. if ( empty( $blog_id ) || !is_multisite() ) {
  2354. $url = get_option( 'siteurl' );
  2355. } else {
  2356. switch_to_blog( $blog_id );
  2357. $url = get_option( 'siteurl' );
  2358. restore_current_blog();
  2359. }
  2360. $url = set_url_scheme( $url, $scheme );
  2361. if ( $path && is_string( $path ) )
  2362. $url .= '/' . ltrim( $path, '/' );
  2363. /**
  2364. * Filter the site URL.
  2365. *
  2366. * @since 2.7.0
  2367. *
  2368. * @param string $url The complete site URL including scheme and path.
  2369. * @param string $path Path relative to the site URL. Blank string if no path is specified.
  2370. * @param string|null $scheme Scheme to give the site URL context. Accepts 'http', 'https', 'login',
  2371. * 'login_post', 'admin', 'relative' or null.
  2372. * @param int|null $blog_id Blog ID, or null for the current blog.
  2373. */
  2374. return apply_filters( 'site_url', $url, $path, $scheme, $blog_id );
  2375. }
  2376. /**
  2377. * Retrieve the url to the admin area for the current site.
  2378. *
  2379. * @since 2.6.0
  2380. *
  2381. * @param string $path Optional path relative to the admin url.
  2382. * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 'http' or 'https' can be passed to force those schemes.
  2383. * @return string Admin url link with optional path appended.
  2384. */
  2385. function admin_url( $path = '', $scheme = 'admin' ) {
  2386. return get_admin_url( null, $path, $scheme );
  2387. }
  2388. /**
  2389. * Retrieves the url to the admin area for a given site.
  2390. *
  2391. * @since 3.0.0
  2392. *
  2393. * @param int $blog_id Optional. Blog ID. Default null (current site).
  2394. * @param string $path Optional. Path relative to the admin url. Default empty.
  2395. * @param string $scheme Optional. The scheme to use. Accepts 'http' or 'https',
  2396. * to force those schemes. Default 'admin', which obeys
  2397. * {@see force_ssl_admin()} and {@see is_ssl()}.
  2398. * @return string Admin url link with optional path appended.
  2399. */
  2400. function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) {
  2401. $url = get_site_url($blog_id, 'wp-admin/', $scheme);
  2402. if ( $path && is_string( $path ) )
  2403. $url .= ltrim( $path, '/' );
  2404. /**
  2405. * Filter the admin area URL.
  2406. *
  2407. * @since 2.8.0
  2408. *
  2409. * @param string $url The complete admin area URL including scheme and path.
  2410. * @param string $path Path relative to the admin area URL. Blank string if no path is specified.
  2411. * @param int|null $blog_id Blog ID, or null for the current blog.
  2412. */
  2413. return apply_filters( 'admin_url', $url, $path, $blog_id );
  2414. }
  2415. /**
  2416. * Retrieve the url to the includes directory.
  2417. *
  2418. * @since 2.6.0
  2419. *
  2420. * @param string $path Optional. Path relative to the includes url.
  2421. * @param string $scheme Optional. Scheme to give the includes url context.
  2422. * @return string Includes url link with optional path appended.
  2423. */
  2424. function includes_url( $path = '', $scheme = null ) {
  2425. $url = site_url( '/' . WPINC . '/', $scheme );
  2426. if ( $path && is_string( $path ) )
  2427. $url .= ltrim($path, '/');
  2428. /**
  2429. * Filter the URL to the includes directory.
  2430. *
  2431. * @since 2.8.0
  2432. *
  2433. * @param string $url The complete URL to the includes directory including scheme and path.
  2434. * @param string $path Path relative to the URL to the wp-includes directory. Blank string
  2435. * if no path is specified.
  2436. */
  2437. return apply_filters( 'includes_url', $url, $path );
  2438. }
  2439. /**
  2440. * Retrieve the url to the content directory.
  2441. *
  2442. * @since 2.6.0
  2443. *
  2444. * @param string $path Optional. Path relative to the content url.
  2445. * @return string Content url link with optional path appended.
  2446. */
  2447. function content_url($path = '') {
  2448. $url = set_url_scheme( WP_CONTENT_URL );
  2449. if ( $path && is_string( $path ) )
  2450. $url .= '/' . ltrim($path, '/');
  2451. /**
  2452. * Filter the URL to the content directory.
  2453. *
  2454. * @since 2.8.0
  2455. *
  2456. * @param string $url The complete URL to the content directory including scheme and path.
  2457. * @param string $path Path relative to the URL to the content directory. Blank string
  2458. * if no path is specified.
  2459. */
  2460. return apply_filters( 'content_url', $url, $path);
  2461. }
  2462. /**
  2463. * Retrieve a URL within the plugins or mu-plugins directory.
  2464. *
  2465. * Defaults to the plugins directory URL if no arguments are supplied.
  2466. *
  2467. * @since 2.6.0
  2468. *
  2469. * @param string $path Optional. Extra path appended to the end of the URL, including
  2470. * the relative directory if $plugin is supplied. Default empty.
  2471. * @param string $plugin Optional. A full path to a file inside a plugin or mu-plugin.
  2472. * The URL will be relative to its directory. Default empty.
  2473. * Typically this is done by passing `__FILE__` as the argument.
  2474. * @return string Plugins URL link with optional paths appended.
  2475. */
  2476. function plugins_url( $path = '', $plugin = '' ) {
  2477. $path = wp_normalize_path( $path );
  2478. $plugin = wp_normalize_path( $plugin );
  2479. $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
  2480. if ( !empty($plugin) && 0 === strpos($plugin, $mu_plugin_dir) )
  2481. $url = WPMU_PLUGIN_URL;
  2482. else
  2483. $url = WP_PLUGIN_URL;
  2484. $url = set_url_scheme( $url );
  2485. if ( !empty($plugin) && is_string($plugin) ) {
  2486. $folder = dirname(plugin_basename($plugin));
  2487. if ( '.' != $folder )
  2488. $url .= '/' . ltrim($folder, '/');
  2489. }
  2490. if ( $path && is_string( $path ) )
  2491. $url .= '/' . ltrim($path, '/');
  2492. /**
  2493. * Filter the URL to the plugins directory.
  2494. *
  2495. * @since 2.8.0
  2496. *
  2497. * @param string $url The complete URL to the plugins directory including scheme and path.
  2498. * @param string $path Path relative to the URL to the plugins directory. Blank string
  2499. * if no path is specified.
  2500. * @param string $plugin The plugin file path to be relative to. Blank string if no plugin
  2501. * is specified.
  2502. */
  2503. return apply_filters( 'plugins_url', $url, $path, $plugin );
  2504. }
  2505. /**
  2506. * Retrieve the site url for the current network.
  2507. *
  2508. * Returns the site url with the appropriate protocol, 'https' if
  2509. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
  2510. * overridden.
  2511. *
  2512. * @since 3.0.0
  2513. *
  2514. * @param string $path Optional. Path relative to the site url.
  2515. * @param string $scheme Optional. Scheme to give the site url context. See set_url_scheme().
  2516. * @return string Site url link with optional path appended.
  2517. */
  2518. function network_site_url( $path = '', $scheme = null ) {
  2519. if ( ! is_multisite() )
  2520. return site_url($path, $scheme);
  2521. $current_site = get_current_site();
  2522. if ( 'relative' == $scheme )
  2523. $url = $current_site->path;
  2524. else
  2525. $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
  2526. if ( $path && is_string( $path ) )
  2527. $url .= ltrim( $path, '/' );
  2528. /**
  2529. * Filter the network site URL.
  2530. *
  2531. * @since 3.0.0
  2532. *
  2533. * @param string $url The complete network site URL including scheme and path.
  2534. * @param string $path Path relative to the network site URL. Blank string if
  2535. * no path is specified.
  2536. * @param string|null $scheme Scheme to give the URL context. Accepts 'http', 'https',
  2537. * 'relative' or null.
  2538. */
  2539. return apply_filters( 'network_site_url', $url, $path, $scheme );
  2540. }
  2541. /**
  2542. * Retrieves the home url for the current network.
  2543. *
  2544. * Returns the home url with the appropriate protocol, 'https' {@see is_ssl()}
  2545. * and 'http' otherwise. If `$scheme` is 'http' or 'https', `is_ssl()` is
  2546. * overridden.
  2547. *
  2548. * @since 3.0.0
  2549. *
  2550. * @param string $path Optional. Path relative to the home url. Default empty.
  2551. * @param string $scheme Optional. Scheme to give the home url context. Accepts
  2552. * 'http', 'https', or 'relative'. Default null.
  2553. * @return string Home url link with optional path appended.
  2554. */
  2555. function network_home_url( $path = '', $scheme = null ) {
  2556. if ( ! is_multisite() )
  2557. return home_url($path, $scheme);
  2558. $current_site = get_current_site();
  2559. $orig_scheme = $scheme;
  2560. if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) )
  2561. $scheme = is_ssl() && ! is_admin() ? 'https' : 'http';
  2562. if ( 'relative' == $scheme )
  2563. $url = $current_site->path;
  2564. else
  2565. $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme );
  2566. if ( $path && is_string( $path ) )
  2567. $url .= ltrim( $path, '/' );
  2568. /**
  2569. * Filter the network home URL.
  2570. *
  2571. * @since 3.0.0
  2572. *
  2573. * @param string $url The complete network home URL including scheme and path.
  2574. * @param string $path Path relative to the network home URL. Blank string
  2575. * if no path is specified.
  2576. * @param string|null $orig_scheme Scheme to give the URL context. Accepts 'http', 'https',
  2577. * 'relative' or null.
  2578. */
  2579. return apply_filters( 'network_home_url', $url, $path, $orig_scheme);
  2580. }
  2581. /**
  2582. * Retrieve the url to the admin area for the network.
  2583. *
  2584. * @since 3.0.0
  2585. *
  2586. * @param string $path Optional path relative to the admin url.
  2587. * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 'http' or 'https' can be passed to force those schemes.
  2588. * @return string Admin url link with optional path appended.
  2589. */
  2590. function network_admin_url( $path = '', $scheme = 'admin' ) {
  2591. if ( ! is_multisite() )
  2592. return admin_url( $path, $scheme );
  2593. $url = network_site_url('wp-admin/network/', $scheme);
  2594. if ( $path && is_string( $path ) )
  2595. $url .= ltrim($path, '/');
  2596. /**
  2597. * Filter the network admin URL.
  2598. *
  2599. * @since 3.0.0
  2600. *
  2601. * @param string $url The complete network admin URL including scheme and path.
  2602. * @param string $path Path relative to the network admin URL. Blank string if
  2603. * no path is specified.
  2604. */
  2605. return apply_filters( 'network_admin_url', $url, $path );
  2606. }
  2607. /**
  2608. * Retrieve the url to the admin area for the current user.
  2609. *
  2610. * @since 3.0.0
  2611. *
  2612. * @param string $path Optional path relative to the admin url.
  2613. * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 'http' or 'https' can be passed to force those schemes.
  2614. * @return string Admin url link with optional path appended.
  2615. */
  2616. function user_admin_url( $path = '', $scheme = 'admin' ) {
  2617. $url = network_site_url('wp-admin/user/', $scheme);
  2618. if ( $path && is_string( $path ) )
  2619. $url .= ltrim($path, '/');
  2620. /**
  2621. * Filter the user admin URL for the current user.
  2622. *
  2623. * @since 3.1.0
  2624. *
  2625. * @param string $url The complete URL including scheme and path.
  2626. * @param string $path Path relative to the URL. Blank string if
  2627. * no path is specified.
  2628. */
  2629. return apply_filters( 'user_admin_url', $url, $path );
  2630. }
  2631. /**
  2632. * Retrieve the url to the admin area for either the current blog or the network depending on context.
  2633. *
  2634. * @since 3.1.0
  2635. *
  2636. * @param string $path Optional path relative to the admin url.
  2637. * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 'http' or 'https' can be passed to force those schemes.
  2638. * @return string Admin url link with optional path appended.
  2639. */
  2640. function self_admin_url($path = '', $scheme = 'admin') {
  2641. if ( is_network_admin() )
  2642. return network_admin_url($path, $scheme);
  2643. elseif ( is_user_admin() )
  2644. return user_admin_url($path, $scheme);
  2645. else
  2646. return admin_url($path, $scheme);
  2647. }
  2648. /**
  2649. * Set the scheme for a URL
  2650. *
  2651. * @since 3.4.0
  2652. *
  2653. * @param string $url Absolute url that includes a scheme
  2654. * @param string $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login', 'login_post', 'admin', or 'relative'.
  2655. * @return string $url URL with chosen scheme.
  2656. */
  2657. function set_url_scheme( $url, $scheme = null ) {
  2658. $orig_scheme = $scheme;
  2659. if ( ! $scheme ) {
  2660. $scheme = is_ssl() ? 'https' : 'http';
  2661. } elseif ( $scheme === 'admin' || $scheme === 'login' || $scheme === 'login_post' || $scheme === 'rpc' ) {
  2662. $scheme = is_ssl() || force_ssl_admin() ? 'https' : 'http';
  2663. } elseif ( $scheme !== 'http' && $scheme !== 'https' && $scheme !== 'relative' ) {
  2664. $scheme = is_ssl() ? 'https' : 'http';
  2665. }
  2666. $url = trim( $url );
  2667. if ( substr( $url, 0, 2 ) === '//' )
  2668. $url = 'http:' . $url;
  2669. if ( 'relative' == $scheme ) {
  2670. $url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) );
  2671. if ( $url !== '' && $url[0] === '/' )
  2672. $url = '/' . ltrim($url , "/ \t\n\r\0\x0B" );
  2673. } else {
  2674. $url = preg_replace( '#^\w+://#', $scheme . '://', $url );
  2675. }
  2676. /**
  2677. * Filter the resulting URL after setting the scheme.
  2678. *
  2679. * @since 3.4.0
  2680. *
  2681. * @param string $url The complete URL including scheme and path.
  2682. * @param string $scheme Scheme applied to the URL. One of 'http', 'https', or 'relative'.
  2683. * @param string $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login',
  2684. * 'login_post', 'admin', 'rpc', or 'relative'.
  2685. */
  2686. return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme );
  2687. }
  2688. /**
  2689. * Get the URL to the user's dashboard.
  2690. *
  2691. * If a user does not belong to any site, the global user dashboard is used. If the user belongs to the current site,
  2692. * the dashboard for the current site is returned. If the user cannot edit the current site, the dashboard to the user's
  2693. * primary blog is returned.
  2694. *
  2695. * @since 3.1.0
  2696. *
  2697. * @param int $user_id Optional. User ID. Defaults to current user.
  2698. * @param string $path Optional path relative to the dashboard. Use only paths known to both blog and user admins.
  2699. * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 'http' or 'https' can be passed to force those schemes.
  2700. * @return string Dashboard url link with optional path appended.
  2701. */
  2702. function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) {
  2703. $user_id = $user_id ? (int) $user_id : get_current_user_id();
  2704. $blogs = get_blogs_of_user( $user_id );
  2705. if ( ! is_super_admin() && empty($blogs) ) {
  2706. $url = user_admin_url( $path, $scheme );
  2707. } elseif ( ! is_multisite() ) {
  2708. $url = admin_url( $path, $scheme );
  2709. } else {
  2710. $current_blog = get_current_blog_id();
  2711. if ( $current_blog && ( is_super_admin( $user_id ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) {
  2712. $url = admin_url( $path, $scheme );
  2713. } else {
  2714. $active = get_active_blog_for_user( $user_id );
  2715. if ( $active )
  2716. $url = get_admin_url( $active->blog_id, $path, $scheme );
  2717. else
  2718. $url = user_admin_url( $path, $scheme );
  2719. }
  2720. }
  2721. /**
  2722. * Filter the dashboard URL for a user.
  2723. *
  2724. * @since 3.1.0
  2725. *
  2726. * @param string $url The complete URL including scheme and path.
  2727. * @param int $user_id The user ID.
  2728. * @param string $path Path relative to the URL. Blank string if no path is specified.
  2729. * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
  2730. * 'login_post', 'admin', 'relative' or null.
  2731. */
  2732. return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme);
  2733. }
  2734. /**
  2735. * Get the URL to the user's profile editor.
  2736. *
  2737. * @since 3.1.0
  2738. *
  2739. * @param int $user_id Optional. User ID. Defaults to current user.
  2740. * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl().
  2741. * 'http' or 'https' can be passed to force those schemes.
  2742. * @return string Dashboard url link with optional path appended.
  2743. */
  2744. function get_edit_profile_url( $user_id = 0, $scheme = 'admin' ) {
  2745. $user_id = $user_id ? (int) $user_id : get_current_user_id();
  2746. if ( is_user_admin() )
  2747. $url = user_admin_url( 'profile.php', $scheme );
  2748. elseif ( is_network_admin() )
  2749. $url = network_admin_url( 'profile.php', $scheme );
  2750. else
  2751. $url = get_dashboard_url( $user_id, 'profile.php', $scheme );
  2752. /**
  2753. * Filter the URL for a user's profile editor.
  2754. *
  2755. * @since 3.1.0
  2756. *
  2757. * @param string $url The complete URL including scheme and path.
  2758. * @param int $user_id The user ID.
  2759. * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',
  2760. * 'login_post', 'admin', 'relative' or null.
  2761. */
  2762. return apply_filters( 'edit_profile_url', $url, $user_id, $scheme);
  2763. }
  2764. /**
  2765. * Output rel=canonical for singular queries.
  2766. *
  2767. * @since 2.9.0
  2768. */
  2769. function rel_canonical() {
  2770. if ( !is_singular() )
  2771. return;
  2772. global $wp_the_query;
  2773. if ( !$id = $wp_the_query->get_queried_object_id() )
  2774. return;
  2775. $link = get_permalink( $id );
  2776. if ( $page = get_query_var('cpage') )
  2777. $link = get_comments_pagenum_link( $page );
  2778. echo "<link rel='canonical' href='$link' />\n";
  2779. }
  2780. /**
  2781. * Return a shortlink for a post, page, attachment, or blog.
  2782. *
  2783. * This function exists to provide a shortlink tag that all themes and plugins can target. A plugin must hook in to
  2784. * provide the actual shortlinks. Default shortlink support is limited to providing ?p= style links for posts.
  2785. * Plugins can short-circuit this function via the pre_get_shortlink filter or filter the output
  2786. * via the get_shortlink filter.
  2787. *
  2788. * @since 3.0.0.
  2789. *
  2790. * @param int $id A post or blog id. Default is 0, which means the current post or blog.
  2791. * @param string $context Whether the id is a 'blog' id, 'post' id, or 'media' id. If 'post', the post_type of the post is consulted. If 'query', the current query is consulted to determine the id and context. Default is 'post'.
  2792. * @param bool $allow_slugs Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this.
  2793. * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.
  2794. */
  2795. function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {
  2796. /**
  2797. * Filter whether to preempt generating a shortlink for the given post.
  2798. *
  2799. * Passing a truthy value to the filter will effectively short-circuit the
  2800. * shortlink-generation process, returning that value instead.
  2801. *
  2802. * @since 3.0.0
  2803. *
  2804. * @param bool|string $return Short-circuit return value. Either false or a URL string.
  2805. * @param int $id Post ID, or 0 for the current post.
  2806. * @param string $context The context for the link. One of 'post' or 'query',
  2807. * @param bool $allow_slugs Whether to allow post slugs in the shortlink.
  2808. */
  2809. $shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs );
  2810. if ( false !== $shortlink )
  2811. return $shortlink;
  2812. global $wp_query;
  2813. $post_id = 0;
  2814. if ( 'query' == $context && is_singular() ) {
  2815. $post_id = $wp_query->get_queried_object_id();
  2816. $post = get_post( $post_id );
  2817. } elseif ( 'post' == $context ) {
  2818. $post = get_post( $id );
  2819. if ( ! empty( $post->ID ) )
  2820. $post_id = $post->ID;
  2821. }
  2822. $shortlink = '';
  2823. // Return p= link for all public post types.
  2824. if ( ! empty( $post_id ) ) {
  2825. $post_type = get_post_type_object( $post->post_type );
  2826. if ( 'page' === $post->post_type && $post->ID == get_option( 'page_on_front' ) && 'page' == get_option( 'show_on_front' ) ) {
  2827. $shortlink = home_url( '/' );
  2828. } elseif ( $post_type->public ) {
  2829. $shortlink = home_url( '?p=' . $post_id );
  2830. }
  2831. }
  2832. /**
  2833. * Filter the shortlink for a post.
  2834. *
  2835. * @since 3.0.0
  2836. *
  2837. * @param string $shortlink Shortlink URL.
  2838. * @param int $id Post ID, or 0 for the current post.
  2839. * @param string $context The context for the link. One of 'post' or 'query',
  2840. * @param bool $allow_slugs Whether to allow post slugs in the shortlink. Not used by default.
  2841. */
  2842. return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs );
  2843. }
  2844. /**
  2845. * Inject rel=shortlink into head if a shortlink is defined for the current page.
  2846. *
  2847. * Attached to the wp_head action.
  2848. *
  2849. * @since 3.0.0
  2850. */
  2851. function wp_shortlink_wp_head() {
  2852. $shortlink = wp_get_shortlink( 0, 'query' );
  2853. if ( empty( $shortlink ) )
  2854. return;
  2855. echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n";
  2856. }
  2857. /**
  2858. * Send a Link: rel=shortlink header if a shortlink is defined for the current page.
  2859. *
  2860. * Attached to the wp action.
  2861. *
  2862. * @since 3.0.0
  2863. */
  2864. function wp_shortlink_header() {
  2865. if ( headers_sent() )
  2866. return;
  2867. $shortlink = wp_get_shortlink(0, 'query');
  2868. if ( empty($shortlink) )
  2869. return;
  2870. header('Link: <' . $shortlink . '>; rel=shortlink', false);
  2871. }
  2872. /**
  2873. * Display the Short Link for a Post
  2874. *
  2875. * Must be called from inside "The Loop"
  2876. *
  2877. * Call like the_shortlink(__('Shortlinkage FTW'))
  2878. *
  2879. * @since 3.0.0
  2880. *
  2881. * @param string $text Optional The link text or HTML to be displayed. Defaults to 'This is the short link.'
  2882. * @param string $title Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title.
  2883. * @param string $before Optional HTML to display before the link.
  2884. * @param string $after Optional HTML to display after the link.
  2885. */
  2886. function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
  2887. $post = get_post();
  2888. if ( empty( $text ) )
  2889. $text = __('This is the short link.');
  2890. if ( empty( $title ) )
  2891. $title = the_title_attribute( array( 'echo' => false ) );
  2892. $shortlink = wp_get_shortlink( $post->ID );
  2893. if ( !empty( $shortlink ) ) {
  2894. $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';
  2895. /**
  2896. * Filter the shortlink anchor tag for a post.
  2897. *
  2898. * @since 3.0.0
  2899. *
  2900. * @param string $link Shortlink anchor tag.
  2901. * @param string $shortlink Shortlink URL.
  2902. * @param string $text Shortlink's text.
  2903. * @param string $title Shortlink's title attribute.
  2904. */
  2905. $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
  2906. echo $before, $link, $after;
  2907. }
  2908. }