PageRenderTime 46ms CodeModel.GetById 8ms 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

Large files files are truncated, but you can click here to view the full file

  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

Large files files are truncated, but you can click here to view the full file