/wp-includes/link-template.php

  1. <?php 
  2. /** 
  3. * WordPress Link Template Functions 
  4. * 
  5. * @package WordPress 
  6. * @subpackage Template 
  7. */ 
  8.  
  9. /** 
  10. * Displays the permalink for the current post. 
  11. * 
  12. * @since 1.2.0 
  13. * @since 4.4.0 Added the `$post` parameter. 
  14. * 
  15. * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`. 
  16. */ 
  17. function the_permalink( $post = 0 ) { 
  18. /** 
  19. * Filters the display of the permalink for the current post. 
  20. * 
  21. * @since 1.5.0 
  22. * @since 4.4.0 Added the `$post` parameter. 
  23. * 
  24. * @param string $permalink The permalink for the current post. 
  25. * @param int|WP_Post $post Post ID, WP_Post object, or 0. Default 0. 
  26. */ 
  27. echo esc_url( apply_filters( 'the_permalink', get_permalink( $post ), $post ) ); 
  28.  
  29. /** 
  30. * Retrieves a trailing-slashed string if the site is set for adding trailing slashes. 
  31. * 
  32. * Conditionally adds a trailing slash if the permalink structure has a trailing 
  33. * slash, strips the trailing slash if not. The string is passed through the 
  34. * {@see 'user_trailingslashit'} filter. Will remove trailing slash from string, if 
  35. * site is not set to have them. 
  36. * 
  37. * @since 2.2.0 
  38. * 
  39. * @global WP_Rewrite $wp_rewrite 
  40. * 
  41. * @param string $string URL with or without a trailing slash. 
  42. * @param string $type_of_url Optional. The type of URL being considered (e.g. single, category, etc) 
  43. * for use in the filter. Default empty string. 
  44. * @return string The URL with the trailing slash appended or stripped. 
  45. */ 
  46. function user_trailingslashit($string, $type_of_url = '') { 
  47. global $wp_rewrite; 
  48. if ( $wp_rewrite->use_trailing_slashes ) 
  49. $string = trailingslashit($string); 
  50. else 
  51. $string = untrailingslashit($string); 
  52.  
  53. /** 
  54. * Filters the trailing-slashed string, depending on whether the site is set to use trailing slashes. 
  55. * 
  56. * @since 2.2.0 
  57. * 
  58. * @param string $string URL with or without a trailing slash. 
  59. * @param string $type_of_url The type of URL being considered. Accepts 'single', 'single_trackback',  
  60. * 'single_feed', 'single_paged', 'feed', 'category', 'page', 'year',  
  61. * 'month', 'day', 'paged', 'post_type_archive'. 
  62. */ 
  63. return apply_filters( 'user_trailingslashit', $string, $type_of_url ); 
  64.  
  65. /** 
  66. * Displays the permalink anchor for the current post. 
  67. * 
  68. * The permalink mode title will use the post title for the 'a' element 'id' 
  69. * attribute. The id mode uses 'post-' with the post ID for the 'id' attribute. 
  70. * 
  71. * @since 0.71 
  72. * 
  73. * @param string $mode Optional. Permalink mode. Accepts 'title' or 'id'. Default 'id'. 
  74. */ 
  75. function permalink_anchor( $mode = 'id' ) { 
  76. $post = get_post(); 
  77. switch ( strtolower( $mode ) ) { 
  78. case 'title': 
  79. $title = sanitize_title( $post->post_title ) . '-' . $post->ID; 
  80. echo '<a id="'.$title.'"></a>'; 
  81. break; 
  82. case 'id': 
  83. default: 
  84. echo '<a id="post-' . $post->ID . '"></a>'; 
  85. break; 
  86.  
  87. /** 
  88. * Retrieves the full permalink for the current post or post ID. 
  89. * 
  90. * This function is an alias for get_permalink(). 
  91. * 
  92. * @since 3.9.0 
  93. * 
  94. * @see get_permalink() 
  95. * 
  96. * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`. 
  97. * @param bool $leavename Optional. Whether to keep post name or page name. Default false. 
  98. * 
  99. * @return string|false The permalink URL or false if post does not exist. 
  100. */ 
  101. function get_the_permalink( $post = 0, $leavename = false ) { 
  102. return get_permalink( $post, $leavename ); 
  103.  
  104. /** 
  105. * Retrieves the full permalink for the current post or post ID. 
  106. * 
  107. * @since 1.0.0 
  108. * 
  109. * @param int|WP_Post $post Optional. Post ID or post object. Default is the global `$post`. 
  110. * @param bool $leavename Optional. Whether to keep post name or page name. Default false. 
  111. * @return string|false The permalink URL or false if post does not exist. 
  112. */ 
  113. function get_permalink( $post = 0, $leavename = false ) { 
  114. $rewritecode = array( 
  115. '%year%',  
  116. '%monthnum%',  
  117. '%day%',  
  118. '%hour%',  
  119. '%minute%',  
  120. '%second%',  
  121. $leavename? '' : '%postname%',  
  122. '%post_id%',  
  123. '%category%',  
  124. '%author%',  
  125. $leavename? '' : '%pagename%',  
  126. ); 
  127.  
  128. if ( is_object( $post ) && isset( $post->filter ) && 'sample' == $post->filter ) { 
  129. $sample = true; 
  130. } else { 
  131. $post = get_post( $post ); 
  132. $sample = false; 
  133.  
  134. if ( empty($post->ID) ) 
  135. return false; 
  136.  
  137. if ( $post->post_type == 'page' ) 
  138. return get_page_link($post, $leavename, $sample); 
  139. elseif ( $post->post_type == 'attachment' ) 
  140. return get_attachment_link( $post, $leavename ); 
  141. elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) ) 
  142. return get_post_permalink($post, $leavename, $sample); 
  143.  
  144. $permalink = get_option('permalink_structure'); 
  145.  
  146. /** 
  147. * Filters the permalink structure for a post before token replacement occurs. 
  148. * 
  149. * Only applies to posts with post_type of 'post'. 
  150. * 
  151. * @since 3.0.0 
  152. * 
  153. * @param string $permalink The site's permalink structure. 
  154. * @param WP_Post $post The post in question. 
  155. * @param bool $leavename Whether to keep the post name. 
  156. */ 
  157. $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename ); 
  158.  
  159. if ( '' != $permalink && !in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future' ) ) ) { 
  160. $unixtime = strtotime($post->post_date); 
  161.  
  162. $category = ''; 
  163. if ( strpos($permalink, '%category%') !== false ) { 
  164. $cats = get_the_category($post->ID); 
  165. if ( $cats ) { 
  166. usort($cats, '_usort_terms_by_ID'); // order by ID 
  167.  
  168. /** 
  169. * Filters the category that gets used in the %category% permalink token. 
  170. * 
  171. * @since 3.5.0 
  172. * 
  173. * @param stdClass $cat The category to use in the permalink. 
  174. * @param array $cats Array of all categories associated with the post. 
  175. * @param WP_Post $post The post in question. 
  176. */ 
  177. $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post ); 
  178.  
  179. $category_object = get_term( $category_object, 'category' ); 
  180. $category = $category_object->slug; 
  181. if ( $parent = $category_object->parent ) 
  182. $category = get_category_parents($parent, false, '/', true) . $category; 
  183. // show default category in permalinks, without 
  184. // having to assign it explicitly 
  185. if ( empty($category) ) { 
  186. $default_category = get_term( get_option( 'default_category' ), 'category' ); 
  187. if ( $default_category && ! is_wp_error( $default_category ) ) { 
  188. $category = $default_category->slug; 
  189.  
  190. $author = ''; 
  191. if ( strpos($permalink, '%author%') !== false ) { 
  192. $authordata = get_userdata($post->post_author); 
  193. $author = $authordata->user_nicename; 
  194.  
  195. $date = explode(" ", date('Y m d H i s', $unixtime)); 
  196. $rewritereplace = 
  197. array( 
  198. $date[0],  
  199. $date[1],  
  200. $date[2],  
  201. $date[3],  
  202. $date[4],  
  203. $date[5],  
  204. $post->post_name,  
  205. $post->ID,  
  206. $category,  
  207. $author,  
  208. $post->post_name,  
  209. ); 
  210. $permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) ); 
  211. $permalink = user_trailingslashit($permalink, 'single'); 
  212. } else { // if they're not using the fancy permalink option 
  213. $permalink = home_url('?p=' . $post->ID); 
  214.  
  215. /** 
  216. * Filters the permalink for a post. 
  217. * 
  218. * Only applies to posts with post_type of 'post'. 
  219. * 
  220. * @since 1.5.0 
  221. * 
  222. * @param string $permalink The post's permalink. 
  223. * @param WP_Post $post The post in question. 
  224. * @param bool $leavename Whether to keep the post name. 
  225. */ 
  226. return apply_filters( 'post_link', $permalink, $post, $leavename ); 
  227.  
  228. /** 
  229. * Retrieves the permalink for a post of a custom post type. 
  230. * 
  231. * @since 3.0.0 
  232. * 
  233. * @global WP_Rewrite $wp_rewrite 
  234. * 
  235. * @param int $id Optional. Post ID. Default uses the global `$post`. 
  236. * @param bool $leavename Optional, defaults to false. Whether to keep post name. Default false. 
  237. * @param bool $sample Optional, defaults to false. Is it a sample permalink. Default false. 
  238. * @return string|WP_Error The post permalink. 
  239. */ 
  240. function get_post_permalink( $id = 0, $leavename = false, $sample = false ) { 
  241. global $wp_rewrite; 
  242.  
  243. $post = get_post($id); 
  244.  
  245. if ( is_wp_error( $post ) ) 
  246. return $post; 
  247.  
  248. $post_link = $wp_rewrite->get_extra_permastruct($post->post_type); 
  249.  
  250. $slug = $post->post_name; 
  251.  
  252. $draft_or_pending = get_post_status( $id ) && in_array( get_post_status( $id ), array( 'draft', 'pending', 'auto-draft', 'future' ) ); 
  253.  
  254. $post_type = get_post_type_object($post->post_type); 
  255.  
  256. if ( $post_type->hierarchical ) { 
  257. $slug = get_page_uri( $id ); 
  258.  
  259. if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) { 
  260. if ( ! $leavename ) { 
  261. $post_link = str_replace("%$post->post_type%", $slug, $post_link); 
  262. $post_link = home_url( user_trailingslashit($post_link) ); 
  263. } else { 
  264. if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) ) 
  265. $post_link = add_query_arg($post_type->query_var, $slug, ''); 
  266. else 
  267. $post_link = add_query_arg(array('post_type' => $post->post_type, 'p' => $post->ID), ''); 
  268. $post_link = home_url($post_link); 
  269.  
  270. /** 
  271. * Filters the permalink for a post of a custom post type. 
  272. * 
  273. * @since 3.0.0 
  274. * 
  275. * @param string $post_link The post's permalink. 
  276. * @param WP_Post $post The post in question. 
  277. * @param bool $leavename Whether to keep the post name. 
  278. * @param bool $sample Is it a sample permalink. 
  279. */ 
  280. return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample ); 
  281.  
  282. /** 
  283. * Retrieves the permalink for the current page or page ID. 
  284. * 
  285. * Respects page_on_front. Use this one. 
  286. * 
  287. * @since 1.5.0 
  288. * 
  289. * @param int|WP_Post $post Optional. Post ID or object. Default uses the global `$post`. 
  290. * @param bool $leavename Optional. Whether to keep the page name. Default false. 
  291. * @param bool $sample Optional. Whether it should be treated as a sample permalink. 
  292. * Default false. 
  293. * @return string The page permalink. 
  294. */ 
  295. function get_page_link( $post = false, $leavename = false, $sample = false ) { 
  296. $post = get_post( $post ); 
  297.  
  298. if ( 'page' == get_option( 'show_on_front' ) && $post->ID == get_option( 'page_on_front' ) ) 
  299. $link = home_url('/'); 
  300. else 
  301. $link = _get_page_link( $post, $leavename, $sample ); 
  302.  
  303. /** 
  304. * Filters the permalink for a page. 
  305. * 
  306. * @since 1.5.0 
  307. * 
  308. * @param string $link The page's permalink. 
  309. * @param int $post_id The ID of the page. 
  310. * @param bool $sample Is it a sample permalink. 
  311. */ 
  312. return apply_filters( 'page_link', $link, $post->ID, $sample ); 
  313.  
  314. /** 
  315. * Retrieves the page permalink. 
  316. * 
  317. * Ignores page_on_front. Internal use only. 
  318. * 
  319. * @since 2.1.0 
  320. * @access private 
  321. * 
  322. * @global WP_Rewrite $wp_rewrite 
  323. * 
  324. * @param int|WP_Post $post Optional. Post ID or object. Default uses the global `$post`. 
  325. * @param bool $leavename Optional. Whether to keep the page name. Default false. 
  326. * @param bool $sample Optional. Whether it should be treated as a sample permalink. 
  327. * Default false. 
  328. * @return string The page permalink. 
  329. */ 
  330. function _get_page_link( $post = false, $leavename = false, $sample = false ) { 
  331. global $wp_rewrite; 
  332.  
  333. $post = get_post( $post ); 
  334.  
  335. $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ); 
  336.  
  337. $link = $wp_rewrite->get_page_permastruct(); 
  338.  
  339. if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) { 
  340. if ( ! $leavename ) { 
  341. $link = str_replace('%pagename%', get_page_uri( $post ), $link); 
  342.  
  343. $link = home_url($link); 
  344. $link = user_trailingslashit($link, 'page'); 
  345. } else { 
  346. $link = home_url( '?page_id=' . $post->ID ); 
  347.  
  348. /** 
  349. * Filters the permalink for a non-page_on_front page. 
  350. * 
  351. * @since 2.1.0 
  352. * 
  353. * @param string $link The page's permalink. 
  354. * @param int $post_id The ID of the page. 
  355. */ 
  356. return apply_filters( '_get_page_link', $link, $post->ID ); 
  357.  
  358. /** 
  359. * Retrieves the permalink for an attachment. 
  360. * 
  361. * This can be used in the WordPress Loop or outside of it. 
  362. * 
  363. * @since 2.0.0 
  364. * 
  365. * @global WP_Rewrite $wp_rewrite 
  366. * 
  367. * @param int|object $post Optional. Post ID or object. Default uses the global `$post`. 
  368. * @param bool $leavename Optional. Whether to keep the page name. Default false. 
  369. * @return string The attachment permalink. 
  370. */ 
  371. function get_attachment_link( $post = null, $leavename = false ) { 
  372. global $wp_rewrite; 
  373.  
  374. $link = false; 
  375.  
  376. $post = get_post( $post ); 
  377. $parent = ( $post->post_parent > 0 && $post->post_parent != $post->ID ) ? get_post( $post->post_parent ) : false; 
  378. if ( $parent && ! in_array( $parent->post_type, get_post_types() ) ) { 
  379. $parent = false; 
  380.  
  381. if ( $wp_rewrite->using_permalinks() && $parent ) { 
  382. if ( 'page' == $parent->post_type ) 
  383. $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front 
  384. else 
  385. $parentlink = get_permalink( $post->post_parent ); 
  386.  
  387. if ( is_numeric($post->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') ) 
  388. $name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker 
  389. else 
  390. $name = $post->post_name; 
  391.  
  392. if ( strpos($parentlink, '?') === false ) 
  393. $link = user_trailingslashit( trailingslashit($parentlink) . '%postname%' ); 
  394.  
  395. if ( ! $leavename ) 
  396. $link = str_replace( '%postname%', $name, $link ); 
  397. } elseif ( $wp_rewrite->using_permalinks() && ! $leavename ) { 
  398. $link = home_url( user_trailingslashit( $post->post_name ) ); 
  399.  
  400. if ( ! $link ) 
  401. $link = home_url( '/?attachment_id=' . $post->ID ); 
  402.  
  403. /** 
  404. * Filters the permalink for an attachment. 
  405. * 
  406. * @since 2.0.0 
  407. * 
  408. * @param string $link The attachment's permalink. 
  409. * @param int $post_id Attachment ID. 
  410. */ 
  411. return apply_filters( 'attachment_link', $link, $post->ID ); 
  412.  
  413. /** 
  414. * Retrieves the permalink for the year archives. 
  415. * 
  416. * @since 1.5.0 
  417. * 
  418. * @global WP_Rewrite $wp_rewrite 
  419. * 
  420. * @param int|bool $year False for current year or year for permalink. 
  421. * @return string The permalink for the specified year archive. 
  422. */ 
  423. function get_year_link( $year ) { 
  424. global $wp_rewrite; 
  425. if ( !$year ) 
  426. $year = gmdate('Y', current_time('timestamp')); 
  427. $yearlink = $wp_rewrite->get_year_permastruct(); 
  428. if ( !empty($yearlink) ) { 
  429. $yearlink = str_replace('%year%', $year, $yearlink); 
  430. $yearlink = home_url( user_trailingslashit( $yearlink, 'year' ) ); 
  431. } else { 
  432. $yearlink = home_url( '?m=' . $year ); 
  433.  
  434. /** 
  435. * Filters the year archive permalink. 
  436. * 
  437. * @since 1.5.0 
  438. * 
  439. * @param string $yearlink Permalink for the year archive. 
  440. * @param int $year Year for the archive. 
  441. */ 
  442. return apply_filters( 'year_link', $yearlink, $year ); 
  443.  
  444. /** 
  445. * Retrieves the permalink for the month archives with year. 
  446. * 
  447. * @since 1.0.0 
  448. * 
  449. * @global WP_Rewrite $wp_rewrite 
  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. * @return string The permalink for the specified month and year archive. 
  454. */ 
  455. function get_month_link($year, $month) { 
  456. global $wp_rewrite; 
  457. if ( !$year ) 
  458. $year = gmdate('Y', current_time('timestamp')); 
  459. if ( !$month ) 
  460. $month = gmdate('m', current_time('timestamp')); 
  461. $monthlink = $wp_rewrite->get_month_permastruct(); 
  462. if ( !empty($monthlink) ) { 
  463. $monthlink = str_replace('%year%', $year, $monthlink); 
  464. $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink); 
  465. $monthlink = home_url( user_trailingslashit( $monthlink, 'month' ) ); 
  466. } else { 
  467. $monthlink = home_url( '?m=' . $year . zeroise( $month, 2 ) ); 
  468.  
  469. /** 
  470. * Filters the month archive permalink. 
  471. * 
  472. * @since 1.5.0 
  473. * 
  474. * @param string $monthlink Permalink for the month archive. 
  475. * @param int $year Year for the archive. 
  476. * @param int $month The month for the archive. 
  477. */ 
  478. return apply_filters( 'month_link', $monthlink, $year, $month ); 
  479.  
  480. /** 
  481. * Retrieves the permalink for the day archives with year and month. 
  482. * 
  483. * @since 1.0.0 
  484. * 
  485. * @global WP_Rewrite $wp_rewrite 
  486. * 
  487. * @param bool|int $year False for current year. Integer of year. 
  488. * @param bool|int $month False for current month. Integer of month. 
  489. * @param bool|int $day False for current day. Integer of day. 
  490. * @return string The permalink for the specified day, month, and year archive. 
  491. */ 
  492. function get_day_link($year, $month, $day) { 
  493. global $wp_rewrite; 
  494. if ( !$year ) 
  495. $year = gmdate('Y', current_time('timestamp')); 
  496. if ( !$month ) 
  497. $month = gmdate('m', current_time('timestamp')); 
  498. if ( !$day ) 
  499. $day = gmdate('j', current_time('timestamp')); 
  500.  
  501. $daylink = $wp_rewrite->get_day_permastruct(); 
  502. if ( !empty($daylink) ) { 
  503. $daylink = str_replace('%year%', $year, $daylink); 
  504. $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink); 
  505. $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink); 
  506. $daylink = home_url( user_trailingslashit( $daylink, 'day' ) ); 
  507. } else { 
  508. $daylink = home_url( '?m=' . $year . zeroise( $month, 2 ) . zeroise( $day, 2 ) ); 
  509.  
  510. /** 
  511. * Filters the day archive permalink. 
  512. * 
  513. * @since 1.5.0 
  514. * 
  515. * @param string $daylink Permalink for the day archive. 
  516. * @param int $year Year for the archive. 
  517. * @param int $month Month for the archive. 
  518. * @param int $day The day for the archive. 
  519. */ 
  520. return apply_filters( 'day_link', $daylink, $year, $month, $day ); 
  521.  
  522. /** 
  523. * Displays the permalink for the feed type. 
  524. * 
  525. * @since 3.0.0 
  526. * 
  527. * @param string $anchor The link's anchor text. 
  528. * @param string $feed Optional. Feed type. Default empty. 
  529. */ 
  530. function the_feed_link( $anchor, $feed = '' ) { 
  531. $link = '<a href="' . esc_url( get_feed_link( $feed ) ) . '">' . $anchor . '</a>'; 
  532.  
  533. /** 
  534. * Filters the feed link anchor tag. 
  535. * 
  536. * @since 3.0.0 
  537. * 
  538. * @param string $link The complete anchor tag for a feed link. 
  539. * @param string $feed The feed type, or an empty string for the 
  540. * default feed type. 
  541. */ 
  542. echo apply_filters( 'the_feed_link', $link, $feed ); 
  543.  
  544. /** 
  545. * Retrieves the permalink for the feed type. 
  546. * 
  547. * @since 1.5.0 
  548. * 
  549. * @global WP_Rewrite $wp_rewrite 
  550. * 
  551. * @param string $feed Optional. Feed type. Default empty. 
  552. * @return string The feed permalink. 
  553. */ 
  554. function get_feed_link( $feed = '' ) { 
  555. global $wp_rewrite; 
  556.  
  557. $permalink = $wp_rewrite->get_feed_permastruct(); 
  558. if ( '' != $permalink ) { 
  559. if ( false !== strpos($feed, 'comments_') ) { 
  560. $feed = str_replace('comments_', '', $feed); 
  561. $permalink = $wp_rewrite->get_comment_feed_permastruct(); 
  562.  
  563. if ( get_default_feed() == $feed ) 
  564. $feed = ''; 
  565.  
  566. $permalink = str_replace('%feed%', $feed, $permalink); 
  567. $permalink = preg_replace('#/+#', '/', "/$permalink"); 
  568. $output = home_url( user_trailingslashit($permalink, 'feed') ); 
  569. } else { 
  570. if ( empty($feed) ) 
  571. $feed = get_default_feed(); 
  572.  
  573. if ( false !== strpos($feed, 'comments_') ) 
  574. $feed = str_replace('comments_', 'comments-', $feed); 
  575.  
  576. $output = home_url("?feed={$feed}"); 
  577.  
  578. /** 
  579. * Filters the feed type permalink. 
  580. * 
  581. * @since 1.5.0 
  582. * 
  583. * @param string $output The feed permalink. 
  584. * @param string $feed Feed type. 
  585. */ 
  586. return apply_filters( 'feed_link', $output, $feed ); 
  587.  
  588. /** 
  589. * Retrieves the permalink for the post comments feed. 
  590. * 
  591. * @since 2.2.0 
  592. * 
  593. * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`. 
  594. * @param string $feed Optional. Feed type. Default empty. 
  595. * @return string The permalink for the comments feed for the given post. 
  596. */ 
  597. function get_post_comments_feed_link( $post_id = 0, $feed = '' ) { 
  598. $post_id = absint( $post_id ); 
  599.  
  600. if ( ! $post_id ) 
  601. $post_id = get_the_ID(); 
  602.  
  603. if ( empty( $feed ) ) 
  604. $feed = get_default_feed(); 
  605.  
  606. $post = get_post( $post_id ); 
  607. $unattached = 'attachment' === $post->post_type && 0 === (int) $post->post_parent; 
  608.  
  609. if ( '' != get_option('permalink_structure') ) { 
  610. if ( 'page' == get_option('show_on_front') && $post_id == get_option('page_on_front') ) 
  611. $url = _get_page_link( $post_id ); 
  612. else 
  613. $url = get_permalink($post_id); 
  614.  
  615. if ( $unattached ) { 
  616. $url = home_url( '/feed/' ); 
  617. if ( $feed !== get_default_feed() ) { 
  618. $url .= "$feed/"; 
  619. $url = add_query_arg( 'attachment_id', $post_id, $url ); 
  620. } else { 
  621. $url = trailingslashit($url) . 'feed'; 
  622. if ( $feed != get_default_feed() ) 
  623. $url .= "/$feed"; 
  624. $url = user_trailingslashit($url, 'single_feed'); 
  625. } else { 
  626. if ( $unattached ) { 
  627. $url = add_query_arg( array( 'feed' => $feed, 'attachment_id' => $post_id ), home_url( '/' ) ); 
  628. } elseif ( 'page' == $post->post_type ) { 
  629. $url = add_query_arg( array( 'feed' => $feed, 'page_id' => $post_id ), home_url( '/' ) ); 
  630. } else { 
  631. $url = add_query_arg( array( 'feed' => $feed, 'p' => $post_id ), home_url( '/' ) ); 
  632.  
  633. /** 
  634. * Filters the post comments feed permalink. 
  635. * 
  636. * @since 1.5.1 
  637. * 
  638. * @param string $url Post comments feed permalink. 
  639. */ 
  640. return apply_filters( 'post_comments_feed_link', $url ); 
  641.  
  642. /** 
  643. * Displays the comment feed link for a post. 
  644. * 
  645. * Prints out the comment feed link for a post. Link text is placed in the 
  646. * anchor. If no link text is specified, default text is used. If no post ID is 
  647. * specified, the current post is used. 
  648. * 
  649. * @since 2.5.0 
  650. * 
  651. * @param string $link_text Optional. Descriptive link text. Default 'Comments Feed'. 
  652. * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`. 
  653. * @param string $feed Optional. Feed format. Default empty. 
  654. */ 
  655. function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) { 
  656. $url = get_post_comments_feed_link( $post_id, $feed ); 
  657. if ( empty( $link_text ) ) { 
  658. $link_text = __('Comments Feed'); 
  659.  
  660. $link = '<a href="' . esc_url( $url ) . '">' . $link_text . '</a>'; 
  661. /** 
  662. * Filters the post comment feed link anchor tag. 
  663. * 
  664. * @since 2.8.0 
  665. * 
  666. * @param string $link The complete anchor tag for the comment feed link. 
  667. * @param int $post_id Post ID. 
  668. * @param string $feed The feed type, or an empty string for the default feed type. 
  669. */ 
  670. echo apply_filters( 'post_comments_feed_link_html', $link, $post_id, $feed ); 
  671.  
  672. /** 
  673. * Retrieves the feed link for a given author. 
  674. * 
  675. * Returns a link to the feed for all posts by a given author. A specific feed 
  676. * can be requested or left blank to get the default feed. 
  677. * 
  678. * @since 2.5.0 
  679. * 
  680. * @param int $author_id Author ID. 
  681. * @param string $feed Optional. Feed type. Default empty. 
  682. * @return string Link to the feed for the author specified by $author_id. 
  683. */ 
  684. function get_author_feed_link( $author_id, $feed = '' ) { 
  685. $author_id = (int) $author_id; 
  686. $permalink_structure = get_option('permalink_structure'); 
  687.  
  688. if ( empty($feed) ) 
  689. $feed = get_default_feed(); 
  690.  
  691. if ( '' == $permalink_structure ) { 
  692. $link = home_url("?feed=$feed&author=" . $author_id); 
  693. } else { 
  694. $link = get_author_posts_url($author_id); 
  695. if ( $feed == get_default_feed() ) 
  696. $feed_link = 'feed'; 
  697. else 
  698. $feed_link = "feed/$feed"; 
  699.  
  700. $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed'); 
  701.  
  702. /** 
  703. * Filters the feed link for a given author. 
  704. * 
  705. * @since 1.5.1 
  706. * 
  707. * @param string $link The author feed link. 
  708. * @param string $feed Feed type. 
  709. */ 
  710. $link = apply_filters( 'author_feed_link', $link, $feed ); 
  711.  
  712. return $link; 
  713.  
  714. /** 
  715. * Retrieves the feed link for a category. 
  716. * 
  717. * Returns a link to the feed for all posts in a given category. A specific feed 
  718. * can be requested or left blank to get the default feed. 
  719. * 
  720. * @since 2.5.0 
  721. * 
  722. * @param int $cat_id Category ID. 
  723. * @param string $feed Optional. Feed type. Default empty. 
  724. * @return string Link to the feed for the category specified by $cat_id. 
  725. */ 
  726. function get_category_feed_link( $cat_id, $feed = '' ) { 
  727. return get_term_feed_link( $cat_id, 'category', $feed ); 
  728.  
  729. /** 
  730. * Retrieves the feed link for a term. 
  731. * 
  732. * Returns a link to the feed for all posts in a given term. A specific feed 
  733. * can be requested or left blank to get the default feed. 
  734. * 
  735. * @since 3.0.0 
  736. * 
  737. * @param int $term_id Term ID. 
  738. * @param string $taxonomy Optional. Taxonomy of `$term_id`. Default 'category'. 
  739. * @param string $feed Optional. Feed type. Default empty. 
  740. * @return string|false Link to the feed for the term specified by $term_id and $taxonomy. 
  741. */ 
  742. function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) { 
  743. $term_id = (int) $term_id; 
  744.  
  745. $term = get_term( $term_id, $taxonomy ); 
  746.  
  747. if ( empty( $term ) || is_wp_error( $term ) ) 
  748. return false; 
  749.  
  750. if ( empty( $feed ) ) 
  751. $feed = get_default_feed(); 
  752.  
  753. $permalink_structure = get_option( 'permalink_structure' ); 
  754.  
  755. if ( '' == $permalink_structure ) { 
  756. if ( 'category' == $taxonomy ) { 
  757. $link = home_url("?feed=$feed&cat=$term_id"); 
  758. elseif ( 'post_tag' == $taxonomy ) { 
  759. $link = home_url("?feed=$feed&tag=$term->slug"); 
  760. } else { 
  761. $t = get_taxonomy( $taxonomy ); 
  762. $link = home_url("?feed=$feed&$t->query_var=$term->slug"); 
  763. } else { 
  764. $link = get_term_link( $term_id, $term->taxonomy ); 
  765. if ( $feed == get_default_feed() ) 
  766. $feed_link = 'feed'; 
  767. else 
  768. $feed_link = "feed/$feed"; 
  769.  
  770. $link = trailingslashit( $link ) . user_trailingslashit( $feed_link, 'feed' ); 
  771.  
  772. if ( 'category' == $taxonomy ) { 
  773. /** 
  774. * Filters the category feed link. 
  775. * 
  776. * @since 1.5.1 
  777. * 
  778. * @param string $link The category feed link. 
  779. * @param string $feed Feed type. 
  780. */ 
  781. $link = apply_filters( 'category_feed_link', $link, $feed ); 
  782. } elseif ( 'post_tag' == $taxonomy ) { 
  783. /** 
  784. * Filters the post tag feed link. 
  785. * 
  786. * @since 2.3.0 
  787. * 
  788. * @param string $link The tag feed link. 
  789. * @param string $feed Feed type. 
  790. */ 
  791. $link = apply_filters( 'tag_feed_link', $link, $feed ); 
  792. } else { 
  793. /** 
  794. * Filters the feed link for a taxonomy other than 'category' or 'post_tag'. 
  795. * 
  796. * @since 3.0.0 
  797. * 
  798. * @param string $link The taxonomy feed link. 
  799. * @param string $feed Feed type. 
  800. * @param string $feed The taxonomy name. 
  801. */ 
  802. $link = apply_filters( 'taxonomy_feed_link', $link, $feed, $taxonomy ); 
  803.  
  804. return $link; 
  805.  
  806. /** 
  807. * Retrieves the permalink for a tag feed. 
  808. * 
  809. * @since 2.3.0 
  810. * 
  811. * @param int $tag_id Tag ID. 
  812. * @param string $feed Optional. Feed type. Default empty. 
  813. * @return string The feed permalink for the given tag. 
  814. */ 
  815. function get_tag_feed_link( $tag_id, $feed = '' ) { 
  816. return get_term_feed_link( $tag_id, 'post_tag', $feed ); 
  817.  
  818. /** 
  819. * Retrieves the edit link for a tag. 
  820. * 
  821. * @since 2.7.0 
  822. * 
  823. * @param int $tag_id Tag ID. 
  824. * @param string $taxonomy Optional. Taxonomy slug. Default 'post_tag'. 
  825. * @return string The edit tag link URL for the given tag. 
  826. */ 
  827. function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) { 
  828. /** 
  829. * Filters the edit link for a tag (or term in another taxonomy). 
  830. * 
  831. * @since 2.7.0 
  832. * 
  833. * @param string $link The term edit link. 
  834. */ 
  835. return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag_id, $taxonomy ) ); 
  836.  
  837. /** 
  838. * Displays or retrieves the edit link for a tag with formatting. 
  839. * 
  840. * @since 2.7.0 
  841. * 
  842. * @param string $link Optional. Anchor text. Default empty. 
  843. * @param string $before Optional. Display before edit link. Default empty. 
  844. * @param string $after Optional. Display after edit link. Default empty. 
  845. * @param WP_Term $tag Optional. Term object. If null, the queried object will be inspected. 
  846. * Default null. 
  847. */ 
  848. function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) { 
  849. $link = edit_term_link( $link, '', '', $tag, false ); 
  850.  
  851. /** 
  852. * Filters the anchor tag for the edit link for a tag (or term in another taxonomy). 
  853. * 
  854. * @since 2.7.0 
  855. * 
  856. * @param string $link The anchor tag for the edit link. 
  857. */ 
  858. echo $before . apply_filters( 'edit_tag_link', $link ) . $after; 
  859.  
  860. /** 
  861. * Retrieves the URL for editing a given term. 
  862. * 
  863. * @since 3.1.0 
  864. * @since 4.5.0 The `$taxonomy` argument was made optional. 
  865. * 
  866. * @param int $term_id Term ID. 
  867. * @param string $taxonomy Optional. Taxonomy. Defaults to the taxonomy of the term identified 
  868. * by `$term_id`. 
  869. * @param string $object_type Optional. The object type. Used to highlight the proper post type 
  870. * menu on the linked page. Defaults to the first object_type associated 
  871. * with the taxonomy. 
  872. * @return string|null The edit term link URL for the given term, or null on failure. 
  873. */ 
  874. function get_edit_term_link( $term_id, $taxonomy = '', $object_type = '' ) { 
  875. $term = get_term( $term_id, $taxonomy ); 
  876. if ( ! $term || is_wp_error( $term ) ) { 
  877. return; 
  878.  
  879. $tax = get_taxonomy( $term->taxonomy ); 
  880. if ( ! $tax || ! current_user_can( $tax->cap->edit_terms ) ) { 
  881. return; 
  882.  
  883. $args = array( 
  884. 'taxonomy' => $taxonomy,  
  885. 'tag_ID' => $term->term_id,  
  886. ); 
  887.  
  888. if ( $object_type ) { 
  889. $args['post_type'] = $object_type; 
  890. } elseif ( ! empty( $tax->object_type ) ) { 
  891. $args['post_type'] = reset( $tax->object_type ); 
  892.  
  893. if ( $tax->show_ui ) { 
  894. $location = add_query_arg( $args, admin_url( 'term.php' ) ); 
  895. } else { 
  896. $location = ''; 
  897.  
  898. /** 
  899. * Filters the edit link for a term. 
  900. * 
  901. * @since 3.1.0 
  902. * 
  903. * @param string $location The edit link. 
  904. * @param int $term_id Term ID. 
  905. * @param string $taxonomy Taxonomy name. 
  906. * @param string $object_type The object type (eg. the post type). 
  907. */ 
  908. return apply_filters( 'get_edit_term_link', $location, $term_id, $taxonomy, $object_type ); 
  909.  
  910. /** 
  911. * Displays or retrieves the edit term link with formatting. 
  912. * 
  913. * @since 3.1.0 
  914. * 
  915. * @param string $link Optional. Anchor text. Default empty. 
  916. * @param string $before Optional. Display before edit link. Default empty. 
  917. * @param string $after Optional. Display after edit link. Default empty. 
  918. * @param object $term Optional. Term object. If null, the queried object will be inspected. Default null. 
  919. * @param bool $echo Optional. Whether or not to echo the return. Default true. 
  920. * @return string|void HTML content. 
  921. */ 
  922. function edit_term_link( $link = '', $before = '', $after = '', $term = null, $echo = true ) { 
  923. if ( is_null( $term ) ) 
  924. $term = get_queried_object(); 
  925.  
  926. if ( ! $term ) 
  927. return; 
  928.  
  929. $tax = get_taxonomy( $term->taxonomy ); 
  930. if ( ! current_user_can( $tax->cap->edit_terms ) ) 
  931. return; 
  932.  
  933. if ( empty( $link ) ) 
  934. $link = __('Edit This'); 
  935.  
  936. $link = '<a href="' . get_edit_term_link( $term->term_id, $term->taxonomy ) . '">' . $link . '</a>'; 
  937.  
  938. /** 
  939. * Filters the anchor tag for the edit link of a term. 
  940. * 
  941. * @since 3.1.0 
  942. * 
  943. * @param string $link The anchor tag for the edit link. 
  944. * @param int $term_id Term ID. 
  945. */ 
  946. $link = $before . apply_filters( 'edit_term_link', $link, $term->term_id ) . $after; 
  947.  
  948. if ( $echo ) 
  949. echo $link; 
  950. else 
  951. return $link; 
  952.  
  953. /** 
  954. * Retrieves the permalink for a search. 
  955. * 
  956. * @since 3.0.0 
  957. * 
  958. * @global WP_Rewrite $wp_rewrite 
  959. * 
  960. * @param string $query Optional. The query string to use. If empty the current query is used. Default empty. 
  961. * @return string The search permalink. 
  962. */ 
  963. function get_search_link( $query = '' ) { 
  964. global $wp_rewrite; 
  965.  
  966. if ( empty($query) ) 
  967. $search = get_search_query( false ); 
  968. else 
  969. $search = stripslashes($query); 
  970.  
  971. $permastruct = $wp_rewrite->get_search_permastruct(); 
  972.  
  973. if ( empty( $permastruct ) ) { 
  974. $link = home_url('?s=' . urlencode($search) ); 
  975. } else { 
  976. $search = urlencode($search); 
  977. $search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it un-encoded. 
  978. $link = str_replace( '%search%', $search, $permastruct ); 
  979. $link = home_url( user_trailingslashit( $link, 'search' ) ); 
  980.  
  981. /** 
  982. * Filters the search permalink. 
  983. * 
  984. * @since 3.0.0 
  985. * 
  986. * @param string $link Search permalink. 
  987. * @param string $search The URL-encoded search term. 
  988. */ 
  989. return apply_filters( 'search_link', $link, $search ); 
  990.  
  991. /** 
  992. * Retrieves the permalink for the search results feed. 
  993. * 
  994. * @since 2.5.0 
  995. * 
  996. * @global WP_Rewrite $wp_rewrite 
  997. * 
  998. * @param string $search_query Optional. Search query. Default empty. 
  999. * @param string $feed Optional. Feed type. Default empty. 
  1000. * @return string The search results feed permalink. 
  1001. */ 
  1002. function get_search_feed_link($search_query = '', $feed = '') { 
  1003. global $wp_rewrite; 
  1004. $link = get_search_link($search_query); 
  1005.  
  1006. if ( empty($feed) ) 
  1007. $feed = get_default_feed(); 
  1008.  
  1009. $permastruct = $wp_rewrite->get_search_permastruct(); 
  1010.  
  1011. if ( empty($permastruct) ) { 
  1012. $link = add_query_arg('feed', $feed, $link); 
  1013. } else { 
  1014. $link = trailingslashit($link); 
  1015. $link .= "feed/$feed/"; 
  1016.  
  1017. /** 
  1018. * Filters the search feed link. 
  1019. * 
  1020. * @since 2.5.0 
  1021. * 
  1022. * @param string $link Search feed link. 
  1023. * @param string $feed Feed type. 
  1024. * @param string $type The search type. One of 'posts' or 'comments'. 
  1025. */ 
  1026. return apply_filters( 'search_feed_link', $link, $feed, 'posts' ); 
  1027.  
  1028. /** 
  1029. * Retrieves the permalink for the search results comments feed. 
  1030. * 
  1031. * @since 2.5.0 
  1032. * 
  1033. * @global WP_Rewrite $wp_rewrite 
  1034. * 
  1035. * @param string $search_query Optional. Search query. Default empty. 
  1036. * @param string $feed Optional. Feed type. Default empty. 
  1037. * @return string The comments feed search results permalink. 
  1038. */ 
  1039. function get_search_comments_feed_link($search_query = '', $feed = '') { 
  1040. global $wp_rewrite; 
  1041.  
  1042. if ( empty($feed) ) 
  1043. $feed = get_default_feed(); 
  1044.  
  1045. $link = get_search_feed_link($search_query, $feed); 
  1046.  
  1047. $permastruct = $wp_rewrite->get_search_permastruct(); 
  1048.  
  1049. if ( empty($permastruct) ) 
  1050. $link = add_query_arg('feed', 'comments-' . $feed, $link); 
  1051. else 
  1052. $link = add_query_arg('withcomments', 1, $link); 
  1053.  
  1054. /** This filter is documented in wp-includes/link-template.php */ 
  1055. return apply_filters( 'search_feed_link', $link, $feed, 'comments' ); 
  1056.  
  1057. /** 
  1058. * Retrieves the permalink for a post type archive. 
  1059. * 
  1060. * @since 3.1.0 
  1061. * @since 4.5.0 Support for posts was added. 
  1062. * 
  1063. * @global WP_Rewrite $wp_rewrite 
  1064. * 
  1065. * @param string $post_type Post type. 
  1066. * @return string|false The post type archive permalink. 
  1067. */ 
  1068. function get_post_type_archive_link( $post_type ) { 
  1069. global $wp_rewrite; 
  1070. if ( ! $post_type_obj = get_post_type_object( $post_type ) ) 
  1071. return false; 
  1072.  
  1073. if ( 'post' === $post_type ) { 
  1074. $show_on_front = get_option( 'show_on_front' ); 
  1075. $page_for_posts = get_option( 'page_for_posts' ); 
  1076.  
  1077. if ( 'page' == $show_on_front && $page_for_posts ) { 
  1078. $link = get_permalink( $page_for_posts ); 
  1079. } else { 
  1080. $link = get_home_url(); 
  1081. /** This filter is documented in wp-includes/link-template.php */ 
  1082. return apply_filters( 'post_type_archive_link', $link, $post_type ); 
  1083.  
  1084. if ( ! $post_type_obj->has_archive ) 
  1085. return false; 
  1086.  
  1087. if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) { 
  1088. $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive; 
  1089. if ( $post_type_obj->rewrite['with_front'] ) 
  1090. $struct = $wp_rewrite->front . $struct; 
  1091. else 
  1092. $struct = $wp_rewrite->root . $struct; 
  1093. $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) ); 
  1094. } else { 
  1095. $link = home_url( '?post_type=' . $post_type ); 
  1096.  
  1097. /** 
  1098. * Filters the post type archive permalink. 
  1099. * 
  1100. * @since 3.1.0 
  1101. * 
  1102. * @param string $link The post type archive permalink. 
  1103. * @param string $post_type Post type name. 
  1104. */ 
  1105. return apply_filters( 'post_type_archive_link', $link, $post_type ); 
  1106.  
  1107. /** 
  1108. * Retrieves the permalink for a post type archive feed. 
  1109. * 
  1110. * @since 3.1.0 
  1111. * 
  1112. * @param string $post_type Post type 
  1113. * @param string $feed Optional. Feed type. Default empty. 
  1114. * @return string|false The post type feed permalink. 
  1115. */ 
  1116. function get_post_type_archive_feed_link( $post_type, $feed = '' ) { 
  1117. $default_feed = get_default_feed(); 
  1118. if ( empty( $feed ) ) 
  1119. $feed = $default_feed; 
  1120.  
  1121. if ( ! $link = get_post_type_archive_link( $post_type ) ) 
  1122. return false; 
  1123.  
  1124. $post_type_obj = get_post_type_object( $post_type ); 
  1125. if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) && $post_type_obj->rewrite['feeds'] ) { 
  1126. $link = trailingslashit( $link ); 
  1127. $link .= 'feed/'; 
  1128. if ( $feed != $default_feed ) 
  1129. $link .= "$feed/"; 
  1130. } else { 
  1131. $link = add_query_arg( 'feed', $feed, $link ); 
  1132.  
  1133. /** 
  1134. * Filters the post type archive feed link. 
  1135. * 
  1136. * @since 3.1.0 
  1137. * 
  1138. * @param string $link The post type archive feed link. 
  1139. * @param string $feed Feed type. 
  1140. */ 
  1141. return apply_filters( 'post_type_archive_feed_link', $link, $feed ); 
  1142.  
  1143. /** 
  1144. * Retrieves the URL used for the post preview. 
  1145. * 
  1146. * Allows additional query args to be appended. 
  1147. * 
  1148. * @since 4.4.0 
  1149. * 
  1150. * @param int|WP_Post $post Optional. Post ID or `WP_Post` object. Defaults to global `$post`. 
  1151. * @param array $query_args Optional. Array of additional query args to be appended to the link. 
  1152. * Default empty array. 
  1153. * @param string $preview_link Optional. Base preview link to be used if it should differ from the 
  1154. * post permalink. Default empty. 
  1155. * @return string|null URL used for the post preview, or null if the post does not exist. 
  1156. */ 
  1157. function get_preview_post_link( $post = null, $query_args = array(), $preview_link = '' ) { 
  1158. $post = get_post( $post ); 
  1159. if ( ! $post ) { 
  1160. return; 
  1161.  
  1162. $post_type_object = get_post_type_object( $post->post_type ); 
  1163. if ( is_post_type_viewable( $post_type_object ) ) { 
  1164. if ( ! $preview_link ) { 
  1165. $preview_link = set_url_scheme( get_permalink( $post ) ); 
  1166.  
  1167. $query_args['preview'] = 'true'; 
  1168. $preview_link = add_query_arg( $query_args, $preview_link ); 
  1169.  
  1170. /** 
  1171. * Filters the URL used for a post preview. 
  1172. * 
  1173. * @since 2.0.5 
  1174. * @since 4.0.0 Added the `$post` parameter. 
  1175. * 
  1176. * @param string $preview_link URL used for the post preview. 
  1177. * @param WP_Post $post Post object. 
  1178. */ 
  1179. return apply_filters( 'preview_post_link', $preview_link, $post ); 
  1180.  
  1181. /** 
  1182. * Retrieves the edit post link for post. 
  1183. * 
  1184. * Can be used within the WordPress loop or outside of it. Can be used with 
  1185. * pages, posts, attachments, and revisions. 
  1186. * 
  1187. * @since 2.3.0 
  1188. * 
  1189. * @param int $id Optional. Post ID. Default is the ID of the global `$post`. 
  1190. * @param string $context Optional. How to output the '&' character. Default '&'. 
  1191. * @return string|null The edit post link for the given post. null if the post type is invalid or does 
  1192. * not allow an editing UI. 
  1193. */ 
  1194. function get_edit_post_link( $id = 0, $context = 'display' ) { 
  1195. if ( ! $post = get_post( $id ) ) 
  1196. return; 
  1197.  
  1198. if ( 'revision' === $post->post_type ) 
  1199. $action = ''; 
  1200. elseif ( 'display' == $context ) 
  1201. $action = '&action=edit'; 
  1202. else 
  1203. $action = '&action=edit'; 
  1204.  
  1205. $post_type_object = get_post_type_object( $post->post_type ); 
  1206. if ( !$post_type_object ) 
  1207. return; 
  1208.  
  1209. if ( !current_user_can( 'edit_post', $post->ID ) ) 
  1210. return; 
  1211.  
  1212. if ( $post_type_object->_edit_link ) { 
  1213. $link = admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) ); 
  1214. } else { 
  1215. $link = ''; 
  1216.  
  1217. /** 
  1218. * Filters the post edit link. 
  1219. * 
  1220. * @since 2.3.0 
  1221. * 
  1222. * @param string $link The edit link. 
  1223. * @param int $post_id Post ID. 
  1224. * @param string $context The link context. If set to 'display' then ampersands 
  1225. * are encoded. 
  1226. */ 
  1227. return apply_filters( 'get_edit_post_link', $link, $post->ID, $context ); 
  1228.  
  1229. /** 
  1230. * Displays the edit post link for post. 
  1231. * 
  1232. * @since 1.0.0 
  1233. * @since 4.4.0 The `$class` argument was added. 
  1234. * 
  1235. * @param string $text Optional. Anchor text. If null, default is 'Edit This'. Default null. 
  1236. * @param string $before Optional. Display before edit link. Default empty. 
  1237. * @param string $after Optional. Display after edit link. Default empty. 
  1238. * @param int $id Optional. Post ID. Default is the ID of the global `$post`. 
  1239. * @param string $class Optional. Add custom class to link. Default 'post-edit-link'. 
  1240. */ 
  1241. function edit_post_link( $text = null, $before = '', $after = '', $id = 0, $class = 'post-edit-link' ) { 
  1242. if ( ! $post = get_post( $id ) ) { 
  1243. return; 
  1244.  
  1245. if ( ! $url = get_edit_post_link( $post->ID ) ) { 
  1246. return; 
  1247.  
  1248. if ( null === $text ) { 
  1249. $text = __( 'Edit This' ); 
  1250.  
  1251. $link = '<a class="' . esc_attr( $class ) . '" href="' . esc_url( $url ) . '">' . $text . '</a>'; 
  1252.  
  1253. /** 
  1254. * Filters the post edit link anchor tag. 
  1255. * 
  1256. * @since 2.3.0 
  1257. * 
  1258. * @param string $link Anchor tag for the edit link. 
  1259. * @param int $post_id Post ID. 
  1260. * @param string $text Anchor text. 
  1261. */ 
  1262. echo $before . apply_filters( 'edit_post_link', $link, $post->ID, $text ) . $after; 
  1263.  
  1264. /** 
  1265. * Retrieves the delete posts link for post. 
  1266. * 
  1267. * Can be used within the WordPress loop or outside of it, with any post type. 
  1268. * 
  1269. * @since 2.9.0 
  1270. * 
  1271. * @param int $id Optional. Post ID. Default is the ID of the global `$post`. 
  1272. * @param string $deprecated Not used. 
  1273. * @param bool $force_delete Optional. Whether to bypass trash and force deletion. Default false. 
  1274. * @return string|void The delete post link URL for the given post. 
  1275. */ 
  1276. function get_delete_post_link( $id = 0, $deprecated = '', $force_delete = false ) { 
  1277. if ( ! empty( $deprecated ) ) 
  1278. _deprecated_argument( __FUNCTION__, '3.0.0' ); 
  1279.  
  1280. if ( !$post = get_post( $id ) ) 
  1281. return; 
  1282.  
  1283. $post_type_object = get_post_type_object( $post->post_type ); 
  1284. if ( !$post_type_object ) 
  1285. return; 
  1286.  
  1287. if ( !current_user_can( 'delete_post', $post->ID ) ) 
  1288. return; 
  1289.  
  1290. $action = ( $force_delete || !EMPTY_TRASH_DAYS ) ? 'delete' : 'trash'; 
  1291.  
  1292. $delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) ); 
  1293.  
  1294. /** 
  1295. * Filters the post delete link. 
  1296. * 
  1297. * @since 2.9.0 
  1298. * 
  1299. * @param string $link The delete link. 
  1300. * @param int $post_id Post ID. 
  1301. * @param bool $force_delete Whether to bypass the trash and force deletion. Default false. 
  1302. */ 
  1303. return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete ); 
  1304.  
  1305. /** 
  1306. * Retrieves the edit comment link. 
  1307. * 
  1308. * @since 2.3.0 
  1309. * 
  1310. * @param int|WP_Comment $comment_id Optional. Comment ID or WP_Comment object. 
  1311. * @return string|void The edit comment link URL for the given comment. 
  1312. */ 
  1313. function get_edit_comment_link( $comment_id = 0 ) { 
  1314. $comment = get_comment( $comment_id ); 
  1315.  
  1316. if ( !current_user_can( 'edit_comment', $comment->comment_ID ) ) 
  1317. return; 
  1318.  
  1319. $location = admin_url('comment.php?action=editcomment&c=') . $comment->comment_ID; 
  1320.  
  1321. /** 
  1322. * Filters the comment edit link. 
  1323. * 
  1324. * @since 2.3.0 
  1325. * 
  1326. * @param string $location The edit link. 
  1327. */ 
  1328. return apply_filters( 'get_edit_comment_link', $location ); 
  1329.  
  1330. /** 
  1331. * Displays the edit comment link with formatting. 
  1332. * 
  1333. * @since 1.0.0 
  1334. * 
  1335. * @param string $text Optional. Anchor text. If null, default is 'Edit This'. Default null. 
  1336. * @param string $before Optional. Display before edit link. Default empty. 
  1337. * @param string $after Optional. Display after edit link. Default empty. 
  1338. */ 
  1339. function edit_comment_link( $text = null, $before = '', $after = '' ) { 
  1340. $comment = get_comment(); 
  1341.  
  1342. if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) { 
  1343. return; 
  1344.  
  1345. if ( null === $text ) { 
  1346. $text = __( 'Edit This' ); 
  1347.  
  1348. $link = '<a class="comment-edit-link" href="' . esc_url( get_edit_comment_link( $comment ) ) . '">' . $text . '</a>'; 
  1349.  
  1350. /** 
  1351. * Filters the comment edit link anchor tag. 
  1352. * 
  1353. * @since 2.3.0 
  1354. * 
  1355. * @param string $link Anchor tag for the edit link. 
  1356. * @param int $comment_id Comment ID. 
  1357. * @param string $text Anchor text. 
  1358. */ 
  1359. echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID, $text ) . $after; 
  1360.  
  1361. /** 
  1362. * Displays the edit bookmark link. 
  1363. * 
  1364. * @since 2.7.0 
  1365. * 
  1366. * @param int|stdClass $link Optional. Bookmark ID. Default is the id of the current bookmark. 
  1367. * @return string|void The edit bookmark link URL. 
  1368. */ 
  1369. function get_edit_bookmark_link( $link = 0 ) { 
  1370. $link = get_bookmark( $link ); 
  1371.  
  1372. if ( !current_user_can('manage_links') ) 
  1373. return; 
  1374.  
  1375. $location = admin_url('link.php?action=edit&link_id=') . $link->link_id; 
  1376.  
  1377. /** 
  1378. * Filters the bookmark edit link. 
  1379. * 
  1380. * @since 2.7.0 
  1381. * 
  1382. * @param string $location The edit link. 
  1383. * @param int $link_id Bookmark ID. 
  1384. */ 
  1385. return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id ); 
  1386.  
  1387. /** 
  1388. * Displays the edit bookmark link anchor content. 
  1389. * 
  1390. * @since 2.7.0 
  1391. * 
  1392. * @param string $link Optional. Anchor text. Default empty. 
  1393. * @param string $before Optional. Display before edit link. Default empty. 
  1394. * @param string $after Optional. Display after edit link. Default empty. 
  1395. * @param int $bookmark Optional. Bookmark ID. Default is the current bookmark. 
  1396. */ 
  1397. function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) { 
  1398. $bookmark = get_bookmark($bookmark); 
  1399.  
  1400. if ( !current_user_can('manage_links') ) 
  1401. return; 
  1402.  
  1403. if ( empty($link) ) 
  1404. $link = __('Edit This'); 
  1405.  
  1406. $link = '<a href="' . esc_url( get_edit_bookmark_link( $bookmark ) ) . '">' . $link . '</a>'; 
  1407.  
  1408. /** 
  1409. * Filters the bookmark edit link anchor tag. 
  1410. * 
  1411. * @since 2.7.0 
  1412. * 
  1413. * @param string $link Anchor tag for the edit link. 
  1414. * @param int $link_id Bookmark ID. 
  1415. */ 
  1416. echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after; 
  1417.  
  1418. /** 
  1419. * Retrieves the edit user link. 
  1420. * 
  1421. * @since 3.5.0 
  1422. * 
  1423. * @param int $user_id Optional. User ID. Defaults to the current user. 
  1424. * @return string URL to edit user page or empty string. 
  1425. */ 
  1426. function get_edit_user_link( $user_id = null ) { 
  1427. if ( ! $user_id ) 
  1428. $user_id = get_current_user_id(); 
  1429.  
  1430. if ( empty( $user_id ) || ! current_user_can( 'edit_user', $user_id ) ) 
  1431. return ''; 
  1432.  
  1433. $user = get_userdata( $user_id ); 
  1434.  
  1435. if ( ! $user ) 
  1436. return ''; 
  1437.  
  1438. if ( get_current_user_id() == $user->ID ) 
  1439. $link = get_edit_profile_url( $user->ID ); 
  1440. else 
  1441. $link = add_query_arg( 'user_id', $user->ID, self_admin_url( 'user-edit.php' ) ); 
  1442.  
  1443. /** 
  1444. * Filters the user edit link. 
  1445. * 
  1446. * @since 3.5.0 
  1447. * 
  1448. * @param string $link The edit link. 
  1449. * @param int $user_id User ID. 
  1450. */ 
  1451. return apply_filters( 'get_edit_user_link', $link, $user->ID ); 
  1452.  
  1453. // Navigation links 
  1454.  
  1455. /** 
  1456. * Retrieves the previous post that is adjacent to the current post. 
  1457. * 
  1458. * @since 1.5.0 
  1459. * 
  1460. * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. Default false. 
  1461. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty. 
  1462. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1463. * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no 
  1464. * corresponding post exists. 
  1465. */ 
  1466. function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
  1467. return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy ); 
  1468.  
  1469. /** 
  1470. * Retrieves the next post that is adjacent to the current post. 
  1471. * 
  1472. * @since 1.5.0 
  1473. * 
  1474. * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. Default false. 
  1475. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty. 
  1476. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1477. * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no 
  1478. * corresponding post exists. 
  1479. */ 
  1480. function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
  1481. return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy ); 
  1482.  
  1483. /** 
  1484. * Retrieves the adjacent post. 
  1485. * 
  1486. * Can either be next or previous post. 
  1487. * 
  1488. * @since 2.5.0 
  1489. * 
  1490. * @global wpdb $wpdb WordPress database abstraction object. 
  1491. * 
  1492. * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. Default false. 
  1493. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty. 
  1494. * @param bool $previous Optional. Whether to retrieve previous post. Default true 
  1495. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1496. * @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no 
  1497. * corresponding post exists. 
  1498. */ 
  1499. function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 
  1500. global $wpdb; 
  1501.  
  1502. if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) ) 
  1503. return null; 
  1504.  
  1505. $current_post_date = $post->post_date; 
  1506.  
  1507. $join = ''; 
  1508. $where = ''; 
  1509. $adjacent = $previous ? 'previous' : 'next'; 
  1510.  
  1511. if ( $in_same_term || ! empty( $excluded_terms ) ) { 
  1512. if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) { 
  1513. // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and " 
  1514. if ( false !== strpos( $excluded_terms, ' and ' ) ) { 
  1515. _deprecated_argument( __FUNCTION__, '3.3.0', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) ); 
  1516. $excluded_terms = explode( ' and ', $excluded_terms ); 
  1517. } else { 
  1518. $excluded_terms = explode( ', ', $excluded_terms ); 
  1519.  
  1520. $excluded_terms = array_map( 'intval', $excluded_terms ); 
  1521.  
  1522. if ( $in_same_term ) { 
  1523. $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"; 
  1524. $where .= $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy ); 
  1525.  
  1526. if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) ) 
  1527. return ''; 
  1528. $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); 
  1529.  
  1530. // Remove any exclusions from the term array to include. 
  1531. $term_array = array_diff( $term_array, (array) $excluded_terms ); 
  1532. $term_array = array_map( 'intval', $term_array ); 
  1533.  
  1534. if ( ! $term_array || is_wp_error( $term_array ) ) 
  1535. return ''; 
  1536.  
  1537. $where .= " AND tt.term_id IN (" . implode( ', ', $term_array ) . ")"; 
  1538.  
  1539. /** 
  1540. * Filters the IDs of terms excluded from adjacent post queries. 
  1541. * 
  1542. * The dynamic portion of the hook name, `$adjacent`, refers to the type 
  1543. * of adjacency, 'next' or 'previous'. 
  1544. * 
  1545. * @since 4.4.0 
  1546. * 
  1547. * @param string $excluded_terms Array of excluded term IDs. 
  1548. */ 
  1549. $excluded_terms = apply_filters( "get_{$adjacent}_post_excluded_terms", $excluded_terms ); 
  1550.  
  1551. if ( ! empty( $excluded_terms ) ) { 
  1552. $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( ', ', array_map( 'intval', $excluded_terms ) ) . ') )'; 
  1553.  
  1554. // 'post_status' clause depends on the current user. 
  1555. if ( is_user_logged_in() ) { 
  1556. $user_id = get_current_user_id(); 
  1557.  
  1558. $post_type_object = get_post_type_object( $post->post_type ); 
  1559. if ( empty( $post_type_object ) ) { 
  1560. $post_type_cap = $post->post_type; 
  1561. $read_private_cap = 'read_private_' . $post_type_cap . 's'; 
  1562. } else { 
  1563. $read_private_cap = $post_type_object->cap->read_private_posts; 
  1564.  
  1565. /** 
  1566. * Results should include private posts belonging to the current user, or private posts where the 
  1567. * current user has the 'read_private_posts' cap. 
  1568. */ 
  1569. $private_states = get_post_stati( array( 'private' => true ) ); 
  1570. $where .= " AND ( p.post_status = 'publish'"; 
  1571. foreach ( (array) $private_states as $state ) { 
  1572. if ( current_user_can( $read_private_cap ) ) { 
  1573. $where .= $wpdb->prepare( " OR p.post_status = %s", $state ); 
  1574. } else { 
  1575. $where .= $wpdb->prepare( " OR (p.post_author = %d AND p.post_status = %s)", $user_id, $state ); 
  1576. $where .= " )"; 
  1577. } else { 
  1578. $where .= " AND p.post_status = 'publish'"; 
  1579.  
  1580. $op = $previous ? '<' : '>'; 
  1581. $order = $previous ? 'DESC' : 'ASC'; 
  1582.  
  1583. /** 
  1584. * Filters the JOIN clause in the SQL for an adjacent post query. 
  1585. * 
  1586. * The dynamic portion of the hook name, `$adjacent`, refers to the type 
  1587. * of adjacency, 'next' or 'previous'. 
  1588. * 
  1589. * @since 2.5.0 
  1590. * @since 4.4.0 Added the `$taxonomy` and `$post` parameters. 
  1591. * 
  1592. * @param string $join The JOIN clause in the SQL. 
  1593. * @param bool $in_same_term Whether post should be in a same taxonomy term. 
  1594. * @param array $excluded_terms Array of excluded term IDs. 
  1595. * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true. 
  1596. * @param WP_Post $post WP_Post object. 
  1597. */ 
  1598. $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms, $taxonomy, $post ); 
  1599.  
  1600. /** 
  1601. * Filters the WHERE clause in the SQL for an adjacent post query. 
  1602. * 
  1603. * The dynamic portion of the hook name, `$adjacent`, refers to the type 
  1604. * of adjacency, 'next' or 'previous'. 
  1605. * 
  1606. * @since 2.5.0 
  1607. * @since 4.4.0 Added the `$taxonomy` and `$post` parameters. 
  1608. * 
  1609. * @param string $where The `WHERE` clause in the SQL. 
  1610. * @param bool $in_same_term Whether post should be in a same taxonomy term. 
  1611. * @param array $excluded_terms Array of excluded term IDs. 
  1612. * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true. 
  1613. * @param WP_Post $post WP_Post object. 
  1614. */ 
  1615. $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s $where", $current_post_date, $post->post_type ), $in_same_term, $excluded_terms, $taxonomy, $post ); 
  1616.  
  1617. /** 
  1618. * Filters the ORDER BY clause in the SQL for an adjacent post query. 
  1619. * 
  1620. * The dynamic portion of the hook name, `$adjacent`, refers to the type 
  1621. * of adjacency, 'next' or 'previous'. 
  1622. * 
  1623. * @since 2.5.0 
  1624. * @since 4.4.0 Added the `$post` parameter. 
  1625. * 
  1626. * @param string $order_by The `ORDER BY` clause in the SQL. 
  1627. * @param WP_Post $post WP_Post object. 
  1628. */ 
  1629. $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1", $post ); 
  1630.  
  1631. $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort"; 
  1632. $query_key = 'adjacent_post_' . md5( $query ); 
  1633. $result = wp_cache_get( $query_key, 'counts' ); 
  1634. if ( false !== $result ) { 
  1635. if ( $result ) 
  1636. $result = get_post( $result ); 
  1637. return $result; 
  1638.  
  1639. $result = $wpdb->get_var( $query ); 
  1640. if ( null === $result ) 
  1641. $result = ''; 
  1642.  
  1643. wp_cache_set( $query_key, $result, 'counts' ); 
  1644.  
  1645. if ( $result ) 
  1646. $result = get_post( $result ); 
  1647.  
  1648. return $result; 
  1649.  
  1650. /** 
  1651. * Retrieves the adjacent post relational link. 
  1652. * 
  1653. * Can either be next or previous post relational link. 
  1654. * 
  1655. * @since 2.8.0 
  1656. * 
  1657. * @param string $title Optional. Link title format. Default '%title'. 
  1658. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false. 
  1659. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty. 
  1660. * @param bool $previous Optional. Whether to display link to previous or next post. Default true. 
  1661. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1662. * @return string|void The adjacent post relational link URL. 
  1663. */ 
  1664. function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 
  1665. if ( $previous && is_attachment() && $post = get_post() ) 
  1666. $post = get_post( $post->post_parent ); 
  1667. else 
  1668. $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); 
  1669.  
  1670. if ( empty( $post ) ) 
  1671. return; 
  1672.  
  1673. $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) ); 
  1674.  
  1675. if ( empty( $post_title ) ) 
  1676. $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' ); 
  1677.  
  1678. $date = mysql2date( get_option( 'date_format' ), $post->post_date ); 
  1679.  
  1680. $title = str_replace( '%title', $post_title, $title ); 
  1681. $title = str_replace( '%date', $date, $title ); 
  1682.  
  1683. $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='"; 
  1684. $link .= esc_attr( $title ); 
  1685. $link .= "' href='" . get_permalink( $post ) . "' />\n"; 
  1686.  
  1687. $adjacent = $previous ? 'previous' : 'next'; 
  1688.  
  1689. /** 
  1690. * Filters the adjacent post relational link. 
  1691. * 
  1692. * The dynamic portion of the hook name, `$adjacent`, refers to the type 
  1693. * of adjacency, 'next' or 'previous'. 
  1694. * 
  1695. * @since 2.8.0 
  1696. * 
  1697. * @param string $link The relational link. 
  1698. */ 
  1699. return apply_filters( "{$adjacent}_post_rel_link", $link ); 
  1700.  
  1701. /** 
  1702. * Displays the relational links for the posts adjacent to the current post. 
  1703. * 
  1704. * @since 2.8.0 
  1705. * 
  1706. * @param string $title Optional. Link title format. Default '%title'. 
  1707. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false. 
  1708. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty. 
  1709. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1710. */ 
  1711. function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
  1712. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy ); 
  1713. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy ); 
  1714.  
  1715. /** 
  1716. * Displays relational links for the posts adjacent to the current post for single post pages. 
  1717. * 
  1718. * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins 
  1719. * or theme templates. 
  1720. * 
  1721. * @since 3.0.0 
  1722. * 
  1723. * @see adjacent_posts_rel_link() 
  1724. */ 
  1725. function adjacent_posts_rel_link_wp_head() { 
  1726. if ( ! is_single() || is_attachment() ) { 
  1727. return; 
  1728. adjacent_posts_rel_link(); 
  1729.  
  1730. /** 
  1731. * Displays the relational link for the next post adjacent to the current post. 
  1732. * 
  1733. * @since 2.8.0 
  1734. * 
  1735. * @see get_adjacent_post_rel_link() 
  1736. * 
  1737. * @param string $title Optional. Link title format. Default '%title'. 
  1738. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false. 
  1739. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty. 
  1740. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1741. */ 
  1742. function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
  1743. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy ); 
  1744.  
  1745. /** 
  1746. * Displays the relational link for the previous post adjacent to the current post. 
  1747. * 
  1748. * @since 2.8.0 
  1749. * 
  1750. * @see get_adjacent_post_rel_link() 
  1751. * 
  1752. * @param string $title Optional. Link title format. Default '%title'. 
  1753. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false. 
  1754. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default true. 
  1755. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1756. */ 
  1757. function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
  1758. echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy ); 
  1759.  
  1760. /** 
  1761. * Retrieves the boundary post. 
  1762. * 
  1763. * Boundary being either the first or last post by publish date within the constraints specified 
  1764. * by $in_same_term or $excluded_terms. 
  1765. * 
  1766. * @since 2.8.0 
  1767. * 
  1768. * @param bool $in_same_term Optional. Whether returned post should be in a same taxonomy term. 
  1769. * Default false. 
  1770. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. 
  1771. * Default empty. 
  1772. * @param bool $start Optional. Whether to retrieve first or last post. Default true 
  1773. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1774. * @return null|array Array containing the boundary post object if successful, null otherwise. 
  1775. */ 
  1776. function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) { 
  1777. $post = get_post(); 
  1778. if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) ) 
  1779. return null; 
  1780.  
  1781. $query_args = array( 
  1782. 'posts_per_page' => 1,  
  1783. 'order' => $start ? 'ASC' : 'DESC',  
  1784. 'update_post_term_cache' => false,  
  1785. 'update_post_meta_cache' => false 
  1786. ); 
  1787.  
  1788. $term_array = array(); 
  1789.  
  1790. if ( ! is_array( $excluded_terms ) ) { 
  1791. if ( ! empty( $excluded_terms ) ) 
  1792. $excluded_terms = explode( ', ', $excluded_terms ); 
  1793. else 
  1794. $excluded_terms = array(); 
  1795.  
  1796. if ( $in_same_term || ! empty( $excluded_terms ) ) { 
  1797. if ( $in_same_term ) 
  1798. $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); 
  1799.  
  1800. if ( ! empty( $excluded_terms ) ) { 
  1801. $excluded_terms = array_map( 'intval', $excluded_terms ); 
  1802. $excluded_terms = array_diff( $excluded_terms, $term_array ); 
  1803.  
  1804. $inverse_terms = array(); 
  1805. foreach ( $excluded_terms as $excluded_term ) 
  1806. $inverse_terms[] = $excluded_term * -1; 
  1807. $excluded_terms = $inverse_terms; 
  1808.  
  1809. $query_args[ 'tax_query' ] = array( array( 
  1810. 'taxonomy' => $taxonomy,  
  1811. 'terms' => array_merge( $term_array, $excluded_terms ) 
  1812. ) ); 
  1813.  
  1814. return get_posts( $query_args ); 
  1815.  
  1816. /** 
  1817. * Retrieves the previous post link that is adjacent to the current post. 
  1818. * 
  1819. * @since 3.7.0 
  1820. * 
  1821. * @param string $format Optional. Link anchor format. Default '« %link'. 
  1822. * @param string $link Optional. Link permalink format. Default '%title%'. 
  1823. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false. 
  1824. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty. 
  1825. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1826. * @return string The link URL of the previous post in relation to the current post. 
  1827. */ 
  1828. function get_previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
  1829. return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy ); 
  1830.  
  1831. /** 
  1832. * Displays the previous post link that is adjacent to the current post. 
  1833. * 
  1834. * @since 1.5.0 
  1835. * 
  1836. * @see get_previous_post_link() 
  1837. * 
  1838. * @param string $format Optional. Link anchor format. Default '« %link'. 
  1839. * @param string $link Optional. Link permalink format. Default '%title'. 
  1840. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false. 
  1841. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty. 
  1842. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1843. */ 
  1844. function previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
  1845. echo get_previous_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy ); 
  1846.  
  1847. /** 
  1848. * Retrieves the next post link that is adjacent to the current post. 
  1849. * 
  1850. * @since 3.7.0 
  1851. * 
  1852. * @param string $format Optional. Link anchor format. Default '« %link'. 
  1853. * @param string $link Optional. Link permalink format. Default '%title'. 
  1854. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false. 
  1855. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty. 
  1856. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1857. * @return string The link URL of the next post in relation to the current post. 
  1858. */ 
  1859. function get_next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
  1860. return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy ); 
  1861.  
  1862. /** 
  1863. * Displays the next post link that is adjacent to the current post. 
  1864. * 
  1865. * @since 1.5.0 
  1866. * @see get_next_post_link() 
  1867. * 
  1868. * @param string $format Optional. Link anchor format. Default '« %link'. 
  1869. * @param string $link Optional. Link permalink format. Default '%title' 
  1870. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false. 
  1871. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty. 
  1872. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1873. */ 
  1874. function next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { 
  1875. echo get_next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy ); 
  1876.  
  1877. /** 
  1878. * Retrieves the adjacent post link. 
  1879. * 
  1880. * Can be either next post link or previous. 
  1881. * 
  1882. * @since 3.7.0 
  1883. * 
  1884. * @param string $format Link anchor format. 
  1885. * @param string $link Link permalink format. 
  1886. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false. 
  1887. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs. Default empty. 
  1888. * @param bool $previous Optional. Whether to display link to previous or next post. Default true. 
  1889. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1890. * @return string The link URL of the previous or next post in relation to the current post. 
  1891. */ 
  1892. function get_adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 
  1893. if ( $previous && is_attachment() ) 
  1894. $post = get_post( get_post()->post_parent ); 
  1895. else 
  1896. $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); 
  1897.  
  1898. if ( ! $post ) { 
  1899. $output = ''; 
  1900. } else { 
  1901. $title = $post->post_title; 
  1902.  
  1903. if ( empty( $post->post_title ) ) 
  1904. $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' ); 
  1905.  
  1906. /** This filter is documented in wp-includes/post-template.php */ 
  1907. $title = apply_filters( 'the_title', $title, $post->ID ); 
  1908.  
  1909. $date = mysql2date( get_option( 'date_format' ), $post->post_date ); 
  1910. $rel = $previous ? 'prev' : 'next'; 
  1911.  
  1912. $string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">'; 
  1913. $inlink = str_replace( '%title', $title, $link ); 
  1914. $inlink = str_replace( '%date', $date, $inlink ); 
  1915. $inlink = $string . $inlink . '</a>'; 
  1916.  
  1917. $output = str_replace( '%link', $inlink, $format ); 
  1918.  
  1919. $adjacent = $previous ? 'previous' : 'next'; 
  1920.  
  1921. /** 
  1922. * Filters the adjacent post link. 
  1923. * 
  1924. * The dynamic portion of the hook name, `$adjacent`, refers to the type 
  1925. * of adjacency, 'next' or 'previous'. 
  1926. * 
  1927. * @since 2.6.0 
  1928. * @since 4.2.0 Added the `$adjacent` parameter. 
  1929. * 
  1930. * @param string $output The adjacent post link. 
  1931. * @param string $format Link anchor format. 
  1932. * @param string $link Link permalink format. 
  1933. * @param WP_Post $post The adjacent post. 
  1934. * @param string $adjacent Whether the post is previous or next. 
  1935. */ 
  1936. return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post, $adjacent ); 
  1937.  
  1938. /** 
  1939. * Displays the adjacent post link. 
  1940. * 
  1941. * Can be either next post link or previous. 
  1942. * 
  1943. * @since 2.5.0 
  1944. * 
  1945. * @param string $format Link anchor format. 
  1946. * @param string $link Link permalink format. 
  1947. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. Default false. 
  1948. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded category IDs. Default empty. 
  1949. * @param bool $previous Optional. Whether to display link to previous or next post. Default true. 
  1950. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  1951. */ 
  1952. function adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 
  1953. echo get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, $previous, $taxonomy ); 
  1954.  
  1955. /** 
  1956. * Retrieves the link for a page number. 
  1957. * 
  1958. * @since 1.5.0 
  1959. * 
  1960. * @global WP_Rewrite $wp_rewrite 
  1961. * 
  1962. * @param int $pagenum Optional. Page ID. Default 1. 
  1963. * @param bool $escape Optional. Whether to escape the URL for display, with esc_url(). Defaults to true. 
  1964. * Otherwise, prepares the URL with esc_url_raw(). 
  1965. * @return string The link URL for the given page number. 
  1966. */ 
  1967. function get_pagenum_link($pagenum = 1, $escape = true ) { 
  1968. global $wp_rewrite; 
  1969.  
  1970. $pagenum = (int) $pagenum; 
  1971.  
  1972. $request = remove_query_arg( 'paged' ); 
  1973.  
  1974. $home_root = parse_url(home_url()); 
  1975. $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : ''; 
  1976. $home_root = preg_quote( $home_root, '|' ); 
  1977.  
  1978. $request = preg_replace('|^'. $home_root . '|i', '', $request); 
  1979. $request = preg_replace('|^/+|', '', $request); 
  1980.  
  1981. if ( !$wp_rewrite->using_permalinks() || is_admin() ) { 
  1982. $base = trailingslashit( get_bloginfo( 'url' ) ); 
  1983.  
  1984. if ( $pagenum > 1 ) { 
  1985. $result = add_query_arg( 'paged', $pagenum, $base . $request ); 
  1986. } else { 
  1987. $result = $base . $request; 
  1988. } else { 
  1989. $qs_regex = '|\?.*?$|'; 
  1990. preg_match( $qs_regex, $request, $qs_match ); 
  1991.  
  1992. if ( !empty( $qs_match[0] ) ) { 
  1993. $query_string = $qs_match[0]; 
  1994. $request = preg_replace( $qs_regex, '', $request ); 
  1995. } else { 
  1996. $query_string = ''; 
  1997.  
  1998. $request = preg_replace( "|$wp_rewrite->pagination_base/\d+/?$|", '', $request); 
  1999. $request = preg_replace( '|^' . preg_quote( $wp_rewrite->index, '|' ) . '|i', '', $request); 
  2000. $request = ltrim($request, '/'); 
  2001.  
  2002. $base = trailingslashit( get_bloginfo( 'url' ) ); 
  2003.  
  2004. if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) ) 
  2005. $base .= $wp_rewrite->index . '/'; 
  2006.  
  2007. if ( $pagenum > 1 ) { 
  2008. $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $wp_rewrite->pagination_base . "/" . $pagenum, 'paged' ); 
  2009.  
  2010. $result = $base . $request . $query_string; 
  2011.  
  2012. /** 
  2013. * Filters the page number link for the current request. 
  2014. * 
  2015. * @since 2.5.0 
  2016. * 
  2017. * @param string $result The page number link. 
  2018. */ 
  2019. $result = apply_filters( 'get_pagenum_link', $result ); 
  2020.  
  2021. if ( $escape ) 
  2022. return esc_url( $result ); 
  2023. else 
  2024. return esc_url_raw( $result ); 
  2025.  
  2026. /** 
  2027. * Retrieves the next posts page link. 
  2028. * 
  2029. * Backported from 2.1.3 to 2.0.10. 
  2030. * 
  2031. * @since 2.0.10 
  2032. * 
  2033. * @global int $paged 
  2034. * 
  2035. * @param int $max_page Optional. Max pages. Default 0. 
  2036. * @return string|void The link URL for next posts page. 
  2037. */ 
  2038. function get_next_posts_page_link($max_page = 0) { 
  2039. global $paged; 
  2040.  
  2041. if ( !is_single() ) { 
  2042. if ( !$paged ) 
  2043. $paged = 1; 
  2044. $nextpage = intval($paged) + 1; 
  2045. if ( !$max_page || $max_page >= $nextpage ) 
  2046. return get_pagenum_link($nextpage); 
  2047.  
  2048. /** 
  2049. * Displays or retrieves the next posts page link. 
  2050. * 
  2051. * @since 0.71 
  2052. * 
  2053. * @param int $max_page Optional. Max pages. Default 0. 
  2054. * @param bool $echo Optional. Whether to echo the link. Default true. 
  2055. * @return string|void The link URL for next posts page if `$echo = false`. 
  2056. */ 
  2057. function next_posts( $max_page = 0, $echo = true ) { 
  2058. $output = esc_url( get_next_posts_page_link( $max_page ) ); 
  2059.  
  2060. if ( $echo ) 
  2061. echo $output; 
  2062. else 
  2063. return $output; 
  2064.  
  2065. /** 
  2066. * Retrieves the next posts page link. 
  2067. * 
  2068. * @since 2.7.0 
  2069. * 
  2070. * @global int $paged 
  2071. * @global WP_Query $wp_query 
  2072. * 
  2073. * @param string $label Content for link text. 
  2074. * @param int $max_page Optional. Max pages. Default 0. 
  2075. * @return string|void HTML-formatted next posts page link. 
  2076. */ 
  2077. function get_next_posts_link( $label = null, $max_page = 0 ) { 
  2078. global $paged, $wp_query; 
  2079.  
  2080. if ( !$max_page ) 
  2081. $max_page = $wp_query->max_num_pages; 
  2082.  
  2083. if ( !$paged ) 
  2084. $paged = 1; 
  2085.  
  2086. $nextpage = intval($paged) + 1; 
  2087.  
  2088. if ( null === $label ) 
  2089. $label = __( 'Next Page »' ); 
  2090.  
  2091. if ( !is_single() && ( $nextpage <= $max_page ) ) { 
  2092. /** 
  2093. * Filters the anchor tag attributes for the next posts page link. 
  2094. * 
  2095. * @since 2.7.0 
  2096. * 
  2097. * @param string $attributes Attributes for the anchor tag. 
  2098. */ 
  2099. $attr = apply_filters( 'next_posts_link_attributes', '' ); 
  2100.  
  2101. return '<a href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1, 8};)/i', '&$1', $label) . '</a>'; 
  2102.  
  2103. /** 
  2104. * Displays the next posts page link. 
  2105. * 
  2106. * @since 0.71 
  2107. * 
  2108. * @param string $label Content for link text. 
  2109. * @param int $max_page Optional. Max pages. Default 0. 
  2110. */ 
  2111. function next_posts_link( $label = null, $max_page = 0 ) { 
  2112. echo get_next_posts_link( $label, $max_page ); 
  2113.  
  2114. /** 
  2115. * Retrieves the previous posts page link. 
  2116. * 
  2117. * Will only return string, if not on a single page or post. 
  2118. * 
  2119. * Backported to 2.0.10 from 2.1.3. 
  2120. * 
  2121. * @since 2.0.10 
  2122. * 
  2123. * @global int $paged 
  2124. * 
  2125. * @return string|void The link for the previous posts page. 
  2126. */ 
  2127. function get_previous_posts_page_link() { 
  2128. global $paged; 
  2129.  
  2130. if ( !is_single() ) { 
  2131. $nextpage = intval($paged) - 1; 
  2132. if ( $nextpage < 1 ) 
  2133. $nextpage = 1; 
  2134. return get_pagenum_link($nextpage); 
  2135.  
  2136. /** 
  2137. * Displays or retrieves the previous posts page link. 
  2138. * 
  2139. * @since 0.71 
  2140. * 
  2141. * @param bool $echo Optional. Whether to echo the link. Default true. 
  2142. * @return string|void The previous posts page link if `$echo = false`. 
  2143. */ 
  2144. function previous_posts( $echo = true ) { 
  2145. $output = esc_url( get_previous_posts_page_link() ); 
  2146.  
  2147. if ( $echo ) 
  2148. echo $output; 
  2149. else 
  2150. return $output; 
  2151.  
  2152. /** 
  2153. * Retrieves the previous posts page link. 
  2154. * 
  2155. * @since 2.7.0 
  2156. * 
  2157. * @global int $paged 
  2158. * 
  2159. * @param string $label Optional. Previous page link text. 
  2160. * @return string|void HTML-formatted previous page link. 
  2161. */ 
  2162. function get_previous_posts_link( $label = null ) { 
  2163. global $paged; 
  2164.  
  2165. if ( null === $label ) 
  2166. $label = __( '« Previous Page' ); 
  2167.  
  2168. if ( !is_single() && $paged > 1 ) { 
  2169. /** 
  2170. * Filters the anchor tag attributes for the previous posts page link. 
  2171. * 
  2172. * @since 2.7.0 
  2173. * 
  2174. * @param string $attributes Attributes for the anchor tag. 
  2175. */ 
  2176. $attr = apply_filters( 'previous_posts_link_attributes', '' ); 
  2177. return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1, 8};)/i', '&$1', $label ) .'</a>'; 
  2178.  
  2179. /** 
  2180. * Displays the previous posts page link. 
  2181. * 
  2182. * @since 0.71 
  2183. * 
  2184. * @param string $label Optional. Previous page link text. 
  2185. */ 
  2186. function previous_posts_link( $label = null ) { 
  2187. echo get_previous_posts_link( $label ); 
  2188.  
  2189. /** 
  2190. * Retrieves the post pages link navigation for previous and next pages. 
  2191. * 
  2192. * @since 2.8.0 
  2193. * 
  2194. * @global WP_Query $wp_query 
  2195. * 
  2196. * @param string|array $args { 
  2197. * Optional. Arguments to build the post pages link navigation. 
  2198. * 
  2199. * @type string $sep Separator character. Default '—'. 
  2200. * @type string $prelabel Link text to display for the previous page link. 
  2201. * Default '« Previous Page'. 
  2202. * @type string $nxtlabel Link text to display for the next page link. 
  2203. * Default 'Next Page »'. 
  2204. * } 
  2205. * @return string The posts link navigation. 
  2206. */ 
  2207. function get_posts_nav_link( $args = array() ) { 
  2208. global $wp_query; 
  2209.  
  2210. $return = ''; 
  2211.  
  2212. if ( !is_singular() ) { 
  2213. $defaults = array( 
  2214. 'sep' => ' — ',  
  2215. 'prelabel' => __('« Previous Page'),  
  2216. 'nxtlabel' => __('Next Page »'),  
  2217. ); 
  2218. $args = wp_parse_args( $args, $defaults ); 
  2219.  
  2220. $max_num_pages = $wp_query->max_num_pages; 
  2221. $paged = get_query_var('paged'); 
  2222.  
  2223. //only have sep if there's both prev and next results 
  2224. if ($paged < 2 || $paged >= $max_num_pages) { 
  2225. $args['sep'] = ''; 
  2226.  
  2227. if ( $max_num_pages > 1 ) { 
  2228. $return = get_previous_posts_link($args['prelabel']); 
  2229. $return .= preg_replace('/&([^#])(?![a-z]{1, 8};)/i', '&$1', $args['sep']); 
  2230. $return .= get_next_posts_link($args['nxtlabel']); 
  2231. return $return; 
  2232.  
  2233.  
  2234. /** 
  2235. * Displays the post pages link navigation for previous and next pages. 
  2236. * 
  2237. * @since 0.71 
  2238. * 
  2239. * @param string $sep Optional. Separator for posts navigation links. Default empty. 
  2240. * @param string $prelabel Optional. Label for previous pages. Default empty. 
  2241. * @param string $nxtlabel Optional Label for next pages. Default empty. 
  2242. */ 
  2243. function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) { 
  2244. $args = array_filter( compact('sep', 'prelabel', 'nxtlabel') ); 
  2245. echo get_posts_nav_link($args); 
  2246.  
  2247. /** 
  2248. * Retrieves the navigation to next/previous post, when applicable. 
  2249. * 
  2250. * @since 4.1.0 
  2251. * @since 4.4.0 Introduced the `in_same_term`, `excluded_terms`, and `taxonomy` arguments. 
  2252. * 
  2253. * @param array $args { 
  2254. * Optional. Default post navigation arguments. Default empty array. 
  2255. * 
  2256. * @type string $prev_text Anchor text to display in the previous post link. Default '%title'. 
  2257. * @type string $next_text Anchor text to display in the next post link. Default '%title'. 
  2258. * @type bool $in_same_term Whether link should be in a same taxonomy term. Default false. 
  2259. * @type array|string $excluded_terms Array or comma-separated list of excluded term IDs. Default empty. 
  2260. * @type string $taxonomy Taxonomy, if `$in_same_term` is true. Default 'category'. 
  2261. * @type string $screen_reader_text Screen reader text for nav element. Default 'Post navigation'. 
  2262. * } 
  2263. * @return string Markup for post links. 
  2264. */ 
  2265. function get_the_post_navigation( $args = array() ) { 
  2266. $args = wp_parse_args( $args, array( 
  2267. 'prev_text' => '%title',  
  2268. 'next_text' => '%title',  
  2269. 'in_same_term' => false,  
  2270. 'excluded_terms' => '',  
  2271. 'taxonomy' => 'category',  
  2272. 'screen_reader_text' => __( 'Post navigation' ),  
  2273. ) ); 
  2274.  
  2275. $navigation = ''; 
  2276.  
  2277. $previous = get_previous_post_link( 
  2278. '<div class="nav-previous">%link</div>',  
  2279. $args['prev_text'],  
  2280. $args['in_same_term'],  
  2281. $args['excluded_terms'],  
  2282. $args['taxonomy'] 
  2283. ); 
  2284.  
  2285. $next = get_next_post_link( 
  2286. '<div class="nav-next">%link</div>',  
  2287. $args['next_text'],  
  2288. $args['in_same_term'],  
  2289. $args['excluded_terms'],  
  2290. $args['taxonomy'] 
  2291. ); 
  2292.  
  2293. // Only add markup if there's somewhere to navigate to. 
  2294. if ( $previous || $next ) { 
  2295. $navigation = _navigation_markup( $previous . $next, 'post-navigation', $args['screen_reader_text'] ); 
  2296.  
  2297. return $navigation; 
  2298.  
  2299. /** 
  2300. * Displays the navigation to next/previous post, when applicable. 
  2301. * 
  2302. * @since 4.1.0 
  2303. * 
  2304. * @param array $args Optional. See get_the_post_navigation() for available arguments. 
  2305. * Default empty array. 
  2306. */ 
  2307. function the_post_navigation( $args = array() ) { 
  2308. echo get_the_post_navigation( $args ); 
  2309.  
  2310. /** 
  2311. * Returns the navigation to next/previous set of posts, when applicable. 
  2312. * 
  2313. * @since 4.1.0 
  2314. * 
  2315. * @global WP_Query $wp_query WordPress Query object. 
  2316. * 
  2317. * @param array $args { 
  2318. * Optional. Default posts navigation arguments. Default empty array. 
  2319. * 
  2320. * @type string $prev_text Anchor text to display in the previous posts link. 
  2321. * Default 'Older posts'. 
  2322. * @type string $next_text Anchor text to display in the next posts link. 
  2323. * Default 'Newer posts'. 
  2324. * @type string $screen_reader_text Screen reader text for nav element. 
  2325. * Default 'Posts navigation'. 
  2326. * } 
  2327. * @return string Markup for posts links. 
  2328. */ 
  2329. function get_the_posts_navigation( $args = array() ) { 
  2330. $navigation = ''; 
  2331.  
  2332. // Don't print empty markup if there's only one page. 
  2333. if ( $GLOBALS['wp_query']->max_num_pages > 1 ) { 
  2334. $args = wp_parse_args( $args, array( 
  2335. 'prev_text' => __( 'Older posts' ),  
  2336. 'next_text' => __( 'Newer posts' ),  
  2337. 'screen_reader_text' => __( 'Posts navigation' ),  
  2338. ) ); 
  2339.  
  2340. $next_link = get_previous_posts_link( $args['next_text'] ); 
  2341. $prev_link = get_next_posts_link( $args['prev_text'] ); 
  2342.  
  2343. if ( $prev_link ) { 
  2344. $navigation .= '<div class="nav-previous">' . $prev_link . '</div>'; 
  2345.  
  2346. if ( $next_link ) { 
  2347. $navigation .= '<div class="nav-next">' . $next_link . '</div>'; 
  2348.  
  2349. $navigation = _navigation_markup( $navigation, 'posts-navigation', $args['screen_reader_text'] ); 
  2350.  
  2351. return $navigation; 
  2352.  
  2353. /** 
  2354. * Displays the navigation to next/previous set of posts, when applicable. 
  2355. * 
  2356. * @since 4.1.0 
  2357. * 
  2358. * @param array $args Optional. See get_the_posts_navigation() for available arguments. 
  2359. * Default empty array. 
  2360. */ 
  2361. function the_posts_navigation( $args = array() ) { 
  2362. echo get_the_posts_navigation( $args ); 
  2363.  
  2364. /** 
  2365. * Retrieves a paginated navigation to next/previous set of posts, when applicable. 
  2366. * 
  2367. * @since 4.1.0 
  2368. * 
  2369. * @param array $args { 
  2370. * Optional. Default pagination arguments, see paginate_links(). 
  2371. * 
  2372. * @type string $screen_reader_text Screen reader text for navigation element. 
  2373. * Default 'Posts navigation'. 
  2374. * } 
  2375. * @return string Markup for pagination links. 
  2376. */ 
  2377. function get_the_posts_pagination( $args = array() ) { 
  2378. $navigation = ''; 
  2379.  
  2380. // Don't print empty markup if there's only one page. 
  2381. if ( $GLOBALS['wp_query']->max_num_pages > 1 ) { 
  2382. $args = wp_parse_args( $args, array( 
  2383. 'mid_size' => 1,  
  2384. 'prev_text' => _x( 'Previous', 'previous post' ),  
  2385. 'next_text' => _x( 'Next', 'next post' ),  
  2386. 'screen_reader_text' => __( 'Posts navigation' ),  
  2387. ) ); 
  2388.  
  2389. // Make sure we get a string back. Plain is the next best thing. 
  2390. if ( isset( $args['type'] ) && 'array' == $args['type'] ) { 
  2391. $args['type'] = 'plain'; 
  2392.  
  2393. // Set up paginated links. 
  2394. $links = paginate_links( $args ); 
  2395.  
  2396. if ( $links ) { 
  2397. $navigation = _navigation_markup( $links, 'pagination', $args['screen_reader_text'] ); 
  2398.  
  2399. return $navigation; 
  2400.  
  2401. /** 
  2402. * Displays a paginated navigation to next/previous set of posts, when applicable. 
  2403. * 
  2404. * @since 4.1.0 
  2405. * 
  2406. * @param array $args Optional. See get_the_posts_pagination() for available arguments. 
  2407. * Default empty array. 
  2408. */ 
  2409. function the_posts_pagination( $args = array() ) { 
  2410. echo get_the_posts_pagination( $args ); 
  2411.  
  2412. /** 
  2413. * Wraps passed links in navigational markup. 
  2414. * 
  2415. * @since 4.1.0 
  2416. * @access private 
  2417. * 
  2418. * @param string $links Navigational links. 
  2419. * @param string $class Optional. Custom class for nav element. Default: 'posts-navigation'. 
  2420. * @param string $screen_reader_text Optional. Screen reader text for nav element. Default: 'Posts navigation'. 
  2421. * @return string Navigation template tag. 
  2422. */ 
  2423. function _navigation_markup( $links, $class = 'posts-navigation', $screen_reader_text = '' ) { 
  2424. if ( empty( $screen_reader_text ) ) { 
  2425. $screen_reader_text = __( 'Posts navigation' ); 
  2426.  
  2427. $template = ' 
  2428. <nav class="navigation %1$s" role="navigation"> 
  2429. <h2 class="screen-reader-text">%2$s</h2> 
  2430. <div class="nav-links">%3$s</div> 
  2431. </nav>'; 
  2432.  
  2433. /** 
  2434. * Filters the navigation markup template. 
  2435. * 
  2436. * Note: The filtered template HTML must contain specifiers for the navigation 
  2437. * class (%1$s), the screen-reader-text value (%2$s), and placement of the 
  2438. * navigation links (%3$s): 
  2439. * 
  2440. * <nav class="navigation %1$s" role="navigation"> 
  2441. * <h2 class="screen-reader-text">%2$s</h2> 
  2442. * <div class="nav-links">%3$s</div> 
  2443. * </nav> 
  2444. * 
  2445. * @since 4.4.0 
  2446. * 
  2447. * @param string $template The default template. 
  2448. * @param string $class The class passed by the calling function. 
  2449. * @return string Navigation template. 
  2450. */ 
  2451. $template = apply_filters( 'navigation_markup_template', $template, $class ); 
  2452.  
  2453. return sprintf( $template, sanitize_html_class( $class ), esc_html( $screen_reader_text ), $links ); 
  2454.  
  2455. /** 
  2456. * Retrieves the comments page number link. 
  2457. * 
  2458. * @since 2.7.0 
  2459. * 
  2460. * @global WP_Rewrite $wp_rewrite 
  2461. * 
  2462. * @param int $pagenum Optional. Page number. Default 1. 
  2463. * @param int $max_page Optional. The maximum number of comment pages. Default 0. 
  2464. * @return string The comments page number link URL. 
  2465. */ 
  2466. function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) { 
  2467. global $wp_rewrite; 
  2468.  
  2469. $pagenum = (int) $pagenum; 
  2470.  
  2471. $result = get_permalink(); 
  2472.  
  2473. if ( 'newest' == get_option('default_comments_page') ) { 
  2474. if ( $pagenum != $max_page ) { 
  2475. if ( $wp_rewrite->using_permalinks() ) 
  2476. $result = user_trailingslashit( trailingslashit($result) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged'); 
  2477. else 
  2478. $result = add_query_arg( 'cpage', $pagenum, $result ); 
  2479. } elseif ( $pagenum > 1 ) { 
  2480. if ( $wp_rewrite->using_permalinks() ) 
  2481. $result = user_trailingslashit( trailingslashit($result) . $wp_rewrite->comments_pagination_base . '-' . $pagenum, 'commentpaged'); 
  2482. else 
  2483. $result = add_query_arg( 'cpage', $pagenum, $result ); 
  2484.  
  2485. $result .= '#comments'; 
  2486.  
  2487. /** 
  2488. * Filters the comments page number link for the current request. 
  2489. * 
  2490. * @since 2.7.0 
  2491. * 
  2492. * @param string $result The comments page number link. 
  2493. */ 
  2494. return apply_filters( 'get_comments_pagenum_link', $result ); 
  2495.  
  2496. /** 
  2497. * Retrieves the link to the next comments page. 
  2498. * 
  2499. * @since 2.7.1 
  2500. * 
  2501. * @global WP_Query $wp_query 
  2502. * 
  2503. * @param string $label Optional. Label for link text. Default empty. 
  2504. * @param int $max_page Optional. Max page. Default 0. 
  2505. * @return string|void HTML-formatted link for the next page of comments. 
  2506. */ 
  2507. function get_next_comments_link( $label = '', $max_page = 0 ) { 
  2508. global $wp_query; 
  2509.  
  2510. if ( ! is_singular() ) 
  2511. return; 
  2512.  
  2513. $page = get_query_var('cpage'); 
  2514.  
  2515. if ( ! $page ) { 
  2516. $page = 1; 
  2517.  
  2518. $nextpage = intval($page) + 1; 
  2519.  
  2520. if ( empty($max_page) ) 
  2521. $max_page = $wp_query->max_num_comment_pages; 
  2522.  
  2523. if ( empty($max_page) ) 
  2524. $max_page = get_comment_pages_count(); 
  2525.  
  2526. if ( $nextpage > $max_page ) 
  2527. return; 
  2528.  
  2529. if ( empty($label) ) 
  2530. $label = __('Newer Comments »'); 
  2531.  
  2532. /** 
  2533. * Filters the anchor tag attributes for the next comments page link. 
  2534. * 
  2535. * @since 2.7.0 
  2536. * 
  2537. * @param string $attributes Attributes for the anchor tag. 
  2538. */ 
  2539. 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', '&$1', $label) .'</a>'; 
  2540.  
  2541. /** 
  2542. * Displays the link to the next comments page. 
  2543. * 
  2544. * @since 2.7.0 
  2545. * 
  2546. * @param string $label Optional. Label for link text. Default empty. 
  2547. * @param int $max_page Optional. Max page. Default 0. 
  2548. */ 
  2549. function next_comments_link( $label = '', $max_page = 0 ) { 
  2550. echo get_next_comments_link( $label, $max_page ); 
  2551.  
  2552. /** 
  2553. * Retrieves the link to the previous comments page. 
  2554. * 
  2555. * @since 2.7.1 
  2556. * 
  2557. * @param string $label Optional. Label for comments link text. Default empty. 
  2558. * @return string|void HTML-formatted link for the previous page of comments. 
  2559. */ 
  2560. function get_previous_comments_link( $label = '' ) { 
  2561. if ( ! is_singular() ) 
  2562. return; 
  2563.  
  2564. $page = get_query_var('cpage'); 
  2565.  
  2566. if ( intval($page) <= 1 ) 
  2567. return; 
  2568.  
  2569. $prevpage = intval($page) - 1; 
  2570.  
  2571. if ( empty($label) ) 
  2572. $label = __('« Older Comments'); 
  2573.  
  2574. /** 
  2575. * Filters the anchor tag attributes for the previous comments page link. 
  2576. * 
  2577. * @since 2.7.0 
  2578. * 
  2579. * @param string $attributes Attributes for the anchor tag. 
  2580. */ 
  2581. return '<a href="' . esc_url( get_comments_pagenum_link( $prevpage ) ) . '" ' . apply_filters( 'previous_comments_link_attributes', '' ) . '>' . preg_replace('/&([^#])(?![a-z]{1, 8};)/i', '&$1', $label) .'</a>'; 
  2582.  
  2583. /** 
  2584. * Displays the link to the previous comments page. 
  2585. * 
  2586. * @since 2.7.0 
  2587. * 
  2588. * @param string $label Optional. Label for comments link text. Default empty. 
  2589. */ 
  2590. function previous_comments_link( $label = '' ) { 
  2591. echo get_previous_comments_link( $label ); 
  2592.  
  2593. /** 
  2594. * Displays or retrieves pagination links for the comments on the current post. 
  2595. * 
  2596. * @see paginate_links() 
  2597. * @since 2.7.0 
  2598. * 
  2599. * @global WP_Rewrite $wp_rewrite 
  2600. * 
  2601. * @param string|array $args Optional args. See paginate_links(). Default empty array. 
  2602. * @return string|void Markup for pagination links. 
  2603. */ 
  2604. function paginate_comments_links( $args = array() ) { 
  2605. global $wp_rewrite; 
  2606.  
  2607. if ( ! is_singular() ) 
  2608. return; 
  2609.  
  2610. $page = get_query_var('cpage'); 
  2611. if ( !$page ) 
  2612. $page = 1; 
  2613. $max_page = get_comment_pages_count(); 
  2614. $defaults = array( 
  2615. 'base' => add_query_arg( 'cpage', '%#%' ),  
  2616. 'format' => '',  
  2617. 'total' => $max_page,  
  2618. 'current' => $page,  
  2619. 'echo' => true,  
  2620. 'add_fragment' => '#comments' 
  2621. ); 
  2622. if ( $wp_rewrite->using_permalinks() ) 
  2623. $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . $wp_rewrite->comments_pagination_base . '-%#%', 'commentpaged'); 
  2624.  
  2625. $args = wp_parse_args( $args, $defaults ); 
  2626. $page_links = paginate_links( $args ); 
  2627.  
  2628. if ( $args['echo'] ) 
  2629. echo $page_links; 
  2630. else 
  2631. return $page_links; 
  2632.  
  2633. /** 
  2634. * Retrieves navigation to next/previous set of comments, when applicable. 
  2635. * 
  2636. * @since 4.4.0 
  2637. * 
  2638. * @param array $args { 
  2639. * Optional. Default comments navigation arguments. 
  2640. * 
  2641. * @type string $prev_text Anchor text to display in the previous comments link. 
  2642. * Default 'Older comments'. 
  2643. * @type string $next_text Anchor text to display in the next comments link. 
  2644. * Default 'Newer comments'. 
  2645. * @type string $screen_reader_text Screen reader text for nav element. Default 'Comments navigation'. 
  2646. * } 
  2647. * @return string Markup for comments links. 
  2648. */ 
  2649. function get_the_comments_navigation( $args = array() ) { 
  2650. $navigation = ''; 
  2651.  
  2652. // Are there comments to navigate through? 
  2653. if ( get_comment_pages_count() > 1 ) { 
  2654. $args = wp_parse_args( $args, array( 
  2655. 'prev_text' => __( 'Older comments' ),  
  2656. 'next_text' => __( 'Newer comments' ),  
  2657. 'screen_reader_text' => __( 'Comments navigation' ),  
  2658. ) ); 
  2659.  
  2660. $prev_link = get_previous_comments_link( $args['prev_text'] ); 
  2661. $next_link = get_next_comments_link( $args['next_text'] ); 
  2662.  
  2663. if ( $prev_link ) { 
  2664. $navigation .= '<div class="nav-previous">' . $prev_link . '</div>'; 
  2665.  
  2666. if ( $next_link ) { 
  2667. $navigation .= '<div class="nav-next">' . $next_link . '</div>'; 
  2668.  
  2669. $navigation = _navigation_markup( $navigation, 'comment-navigation', $args['screen_reader_text'] ); 
  2670.  
  2671. return $navigation; 
  2672.  
  2673. /** 
  2674. * Displays navigation to next/previous set of comments, when applicable. 
  2675. * 
  2676. * @since 4.4.0 
  2677. * 
  2678. * @param array $args See get_the_comments_navigation() for available arguments. Default empty array. 
  2679. */ 
  2680. function the_comments_navigation( $args = array() ) { 
  2681. echo get_the_comments_navigation( $args ); 
  2682.  
  2683. /** 
  2684. * Retrieves a paginated navigation to next/previous set of comments, when applicable. 
  2685. * 
  2686. * @since 4.4.0 
  2687. * 
  2688. * @see paginate_comments_links() 
  2689. * 
  2690. * @param array $args { 
  2691. * Optional. Default pagination arguments. 
  2692. * 
  2693. * @type string $screen_reader_text Screen reader text for nav element. Default 'Comments navigation'. 
  2694. * } 
  2695. * @return string Markup for pagination links. 
  2696. */ 
  2697. function get_the_comments_pagination( $args = array() ) { 
  2698. $navigation = ''; 
  2699. $args = wp_parse_args( $args, array( 
  2700. 'screen_reader_text' => __( 'Comments navigation' ),  
  2701. ) ); 
  2702. $args['echo'] = false; 
  2703.  
  2704. // Make sure we get plain links, so we get a string we can work with. 
  2705. $args['type'] = 'plain'; 
  2706.  
  2707. $links = paginate_comments_links( $args ); 
  2708.  
  2709. if ( $links ) { 
  2710. $navigation = _navigation_markup( $links, 'comments-pagination', $args['screen_reader_text'] ); 
  2711.  
  2712. return $navigation; 
  2713.  
  2714. /** 
  2715. * Displays a paginated navigation to next/previous set of comments, when applicable. 
  2716. * 
  2717. * @since 4.4.0 
  2718. * 
  2719. * @param array $args See get_the_comments_pagination() for available arguments. Default empty array. 
  2720. */ 
  2721. function the_comments_pagination( $args = array() ) { 
  2722. echo get_the_comments_pagination( $args ); 
  2723.  
  2724. /** 
  2725. * Retrieves the Press This bookmarklet link. 
  2726. * 
  2727. * @since 2.6.0 
  2728. * 
  2729. * @global bool $is_IE Whether the browser matches an Internet Explorer user agent. 
  2730. * @global string $wp_version WP version. 
  2731. * 
  2732. * @global bool $is_IE 
  2733. * @global string $wp_version 
  2734. * @global WP_Press_This $wp_press_this 
  2735. * 
  2736. * @return string The Press This bookmarklet link URL. 
  2737. */ 
  2738. function get_shortcut_link() { 
  2739. global $is_IE, $wp_version; 
  2740.  
  2741. include_once( ABSPATH . 'wp-admin/includes/class-wp-press-this.php' ); 
  2742. $bookmarklet_version = $GLOBALS['wp_press_this']->version; 
  2743. $link = ''; 
  2744.  
  2745. if ( $is_IE ) { 
  2746. /** 
  2747. * Return the old/shorter bookmarklet code for MSIE 8 and lower,  
  2748. * since they only support a max length of ~2000 characters for 
  2749. * bookmark[let] URLs, which is way to small for our smarter one. 
  2750. * Do update the version number so users do not get the "upgrade your 
  2751. * bookmarklet" notice when using PT in those browsers. 
  2752. */ 
  2753. $ua = $_SERVER['HTTP_USER_AGENT']; 
  2754.  
  2755. if ( ! empty( $ua ) && preg_match( '/\bMSIE (\d)/', $ua, $matches ) && (int) $matches[1] <= 8 ) { 
  2756. $url = wp_json_encode( admin_url( 'press-this.php' ) ); 
  2757.  
  2758. $link = 'javascript:var d=document, w=window, e=w.getSelection, k=d.getSelection, x=d.selection, ' . 
  2759. 's=(e?e():(k)?k():(x?x.createRange().text:0)), f=' . $url . ', l=d.location, e=encodeURIComponent, ' . 
  2760. 'u=f+"?u="+e(l.href)+"&t="+e(d.title)+"&s="+e(s)+"&v=' . $bookmarklet_version . '";' . 
  2761. 'a=function() {if(!w.open(u, "t", "toolbar=0, resizable=1, scrollbars=1, status=1, width=600, height=700"))l.href=u;};' . 
  2762. 'if(/Firefox/.test(navigator.userAgent))setTimeout(a, 0);else a();void(0)'; 
  2763.  
  2764. if ( empty( $link ) ) { 
  2765. $src = @file_get_contents( ABSPATH . 'wp-admin/js/bookmarklet.min.js' ); 
  2766.  
  2767. if ( $src ) { 
  2768. $url = wp_json_encode( admin_url( 'press-this.php' ) . '?v=' . $bookmarklet_version ); 
  2769. $link = 'javascript:' . str_replace( 'window.pt_url', $url, $src ); 
  2770.  
  2771. $link = str_replace( array( "\r", "\n", "\t" ), '', $link ); 
  2772.  
  2773. /** 
  2774. * Filters the Press This bookmarklet link. 
  2775. * 
  2776. * @since 2.6.0 
  2777. * 
  2778. * @param string $link The Press This bookmarklet link. 
  2779. */ 
  2780. return apply_filters( 'shortcut_link', $link ); 
  2781.  
  2782. /** 
  2783. * Retrieves the URL for the current site where the front end is accessible. 
  2784. * 
  2785. * Returns the 'home' option with the appropriate protocol, 'https' if 
  2786. * is_ssl() and 'http' otherwise. If `$scheme` is 'http' or 'https',  
  2787. * `is_ssl()` is overridden. 
  2788. * 
  2789. * @since 3.0.0 
  2790. * 
  2791. * @param string $path Optional. Path relative to the home URL. Default empty. 
  2792. * @param string|null $scheme Optional. Scheme to give the home URL context. Accepts 
  2793. * 'http', 'https', 'relative', 'rest', or null. Default null. 
  2794. * @return string Home URL link with optional path appended. 
  2795. */ 
  2796. function home_url( $path = '', $scheme = null ) { 
  2797. return get_home_url( null, $path, $scheme ); 
  2798.  
  2799. /** 
  2800. * Retrieves the URL for a given site where the front end is accessible. 
  2801. * 
  2802. * Returns the 'home' option with the appropriate protocol, 'https' if 
  2803. * is_ssl() and 'http' otherwise. If `$scheme` is 'http' or 'https',  
  2804. * `is_ssl()` is overridden. 
  2805. * 
  2806. * @since 3.0.0 
  2807. * 
  2808. * @global string $pagenow 
  2809. * 
  2810. * @param int $blog_id Optional. Site ID. Default null (current site). 
  2811. * @param string $path Optional. Path relative to the home URL. Default empty. 
  2812. * @param string|null $scheme Optional. Scheme to give the home URL context. Accepts 
  2813. * 'http', 'https', 'relative', 'rest', or null. Default null. 
  2814. * @return string Home URL link with optional path appended. 
  2815. */ 
  2816. function get_home_url( $blog_id = null, $path = '', $scheme = null ) { 
  2817. global $pagenow; 
  2818.  
  2819. $orig_scheme = $scheme; 
  2820.  
  2821. if ( empty( $blog_id ) || !is_multisite() ) { 
  2822. $url = get_option( 'home' ); 
  2823. } else { 
  2824. switch_to_blog( $blog_id ); 
  2825. $url = get_option( 'home' ); 
  2826. restore_current_blog(); 
  2827.  
  2828. if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) { 
  2829. if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $pagenow ) 
  2830. $scheme = 'https'; 
  2831. else 
  2832. $scheme = parse_url( $url, PHP_URL_SCHEME ); 
  2833.  
  2834. $url = set_url_scheme( $url, $scheme ); 
  2835.  
  2836. if ( $path && is_string( $path ) ) 
  2837. $url .= '/' . ltrim( $path, '/' ); 
  2838.  
  2839. /** 
  2840. * Filters the home URL. 
  2841. * 
  2842. * @since 3.0.0 
  2843. * 
  2844. * @param string $url The complete home URL including scheme and path. 
  2845. * @param string $path Path relative to the home URL. Blank string if no path is specified. 
  2846. * @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https',  
  2847. * 'relative', 'rest', or null. 
  2848. * @param int|null $blog_id Site ID, or null for the current site. 
  2849. */ 
  2850. return apply_filters( 'home_url', $url, $path, $orig_scheme, $blog_id ); 
  2851.  
  2852. /** 
  2853. * Retrieves the URL for the current site where WordPress application files 
  2854. * (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible. 
  2855. * 
  2856. * Returns the 'site_url' option with the appropriate protocol, 'https' if 
  2857. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is 
  2858. * overridden. 
  2859. * 
  2860. * @since 3.0.0 
  2861. * 
  2862. * @param string $path Optional. Path relative to the site URL. Default empty. 
  2863. * @param string $scheme Optional. Scheme to give the site URL context. See set_url_scheme(). 
  2864. * @return string Site URL link with optional path appended. 
  2865. */ 
  2866. function site_url( $path = '', $scheme = null ) { 
  2867. return get_site_url( null, $path, $scheme ); 
  2868.  
  2869. /** 
  2870. * Retrieves the URL for a given site where WordPress application files 
  2871. * (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible. 
  2872. * 
  2873. * Returns the 'site_url' option with the appropriate protocol, 'https' if 
  2874. * is_ssl() and 'http' otherwise. If `$scheme` is 'http' or 'https',  
  2875. * `is_ssl()` is overridden. 
  2876. * 
  2877. * @since 3.0.0 
  2878. * 
  2879. * @param int $blog_id Optional. Site ID. Default null (current site). 
  2880. * @param string $path Optional. Path relative to the site URL. Default empty. 
  2881. * @param string $scheme Optional. Scheme to give the site URL context. Accepts 
  2882. * 'http', 'https', 'login', 'login_post', 'admin', or 
  2883. * 'relative'. Default null. 
  2884. * @return string Site URL link with optional path appended. 
  2885. */ 
  2886. function get_site_url( $blog_id = null, $path = '', $scheme = null ) { 
  2887. if ( empty( $blog_id ) || !is_multisite() ) { 
  2888. $url = get_option( 'siteurl' ); 
  2889. } else { 
  2890. switch_to_blog( $blog_id ); 
  2891. $url = get_option( 'siteurl' ); 
  2892. restore_current_blog(); 
  2893.  
  2894. $url = set_url_scheme( $url, $scheme ); 
  2895.  
  2896. if ( $path && is_string( $path ) ) 
  2897. $url .= '/' . ltrim( $path, '/' ); 
  2898.  
  2899. /** 
  2900. * Filters the site URL. 
  2901. * 
  2902. * @since 2.7.0 
  2903. * 
  2904. * @param string $url The complete site URL including scheme and path. 
  2905. * @param string $path Path relative to the site URL. Blank string if no path is specified. 
  2906. * @param string|null $scheme Scheme to give the site URL context. Accepts 'http', 'https', 'login',  
  2907. * 'login_post', 'admin', 'relative' or null. 
  2908. * @param int|null $blog_id Site ID, or null for the current site. 
  2909. */ 
  2910. return apply_filters( 'site_url', $url, $path, $scheme, $blog_id ); 
  2911.  
  2912. /** 
  2913. * Retrieves the URL to the admin area for the current site. 
  2914. * 
  2915. * @since 2.6.0 
  2916. * 
  2917. * @param string $path Optional path relative to the admin URL. 
  2918. * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() and is_ssl(). 
  2919. * 'http' or 'https' can be passed to force those schemes. 
  2920. * @return string Admin URL link with optional path appended. 
  2921. */ 
  2922. function admin_url( $path = '', $scheme = 'admin' ) { 
  2923. return get_admin_url( null, $path, $scheme ); 
  2924.  
  2925. /** 
  2926. * Retrieves the URL to the admin area for a given site. 
  2927. * 
  2928. * @since 3.0.0 
  2929. * 
  2930. * @param int $blog_id Optional. Site ID. Default null (current site). 
  2931. * @param string $path Optional. Path relative to the admin URL. Default empty. 
  2932. * @param string $scheme Optional. The scheme to use. Accepts 'http' or 'https',  
  2933. * to force those schemes. Default 'admin', which obeys 
  2934. * force_ssl_admin() and is_ssl(). 
  2935. * @return string Admin URL link with optional path appended. 
  2936. */ 
  2937. function get_admin_url( $blog_id = null, $path = '', $scheme = 'admin' ) { 
  2938. $url = get_site_url($blog_id, 'wp-admin/', $scheme); 
  2939.  
  2940. if ( $path && is_string( $path ) ) 
  2941. $url .= ltrim( $path, '/' ); 
  2942.  
  2943. /** 
  2944. * Filters the admin area URL. 
  2945. * 
  2946. * @since 2.8.0 
  2947. * 
  2948. * @param string $url The complete admin area URL including scheme and path. 
  2949. * @param string $path Path relative to the admin area URL. Blank string if no path is specified. 
  2950. * @param int|null $blog_id Site ID, or null for the current site. 
  2951. */ 
  2952. return apply_filters( 'admin_url', $url, $path, $blog_id ); 
  2953.  
  2954. /** 
  2955. * Retrieves the URL to the includes directory. 
  2956. * 
  2957. * @since 2.6.0 
  2958. * 
  2959. * @param string $path Optional. Path relative to the includes URL. Default empty. 
  2960. * @param string $scheme Optional. Scheme to give the includes URL context. Accepts 
  2961. * 'http', 'https', or 'relative'. Default null. 
  2962. * @return string Includes URL link with optional path appended. 
  2963. */ 
  2964. function includes_url( $path = '', $scheme = null ) { 
  2965. $url = site_url( '/' . WPINC . '/', $scheme ); 
  2966.  
  2967. if ( $path && is_string( $path ) ) 
  2968. $url .= ltrim($path, '/'); 
  2969.  
  2970. /** 
  2971. * Filters the URL to the includes directory. 
  2972. * 
  2973. * @since 2.8.0 
  2974. * 
  2975. * @param string $url The complete URL to the includes directory including scheme and path. 
  2976. * @param string $path Path relative to the URL to the wp-includes directory. Blank string 
  2977. * if no path is specified. 
  2978. */ 
  2979. return apply_filters( 'includes_url', $url, $path ); 
  2980.  
  2981. /** 
  2982. * Retrieves the URL to the content directory. 
  2983. * 
  2984. * @since 2.6.0 
  2985. * 
  2986. * @param string $path Optional. Path relative to the content URL. Default empty. 
  2987. * @return string Content URL link with optional path appended. 
  2988. */ 
  2989. function content_url( $path = '' ) { 
  2990. $url = set_url_scheme( WP_CONTENT_URL ); 
  2991.  
  2992. if ( $path && is_string( $path ) ) 
  2993. $url .= '/' . ltrim($path, '/'); 
  2994.  
  2995. /** 
  2996. * Filters the URL to the content directory. 
  2997. * 
  2998. * @since 2.8.0 
  2999. * 
  3000. * @param string $url The complete URL to the content directory including scheme and path. 
  3001. * @param string $path Path relative to the URL to the content directory. Blank string 
  3002. * if no path is specified. 
  3003. */ 
  3004. return apply_filters( 'content_url', $url, $path); 
  3005.  
  3006. /** 
  3007. * Retrieves a URL within the plugins or mu-plugins directory. 
  3008. * 
  3009. * Defaults to the plugins directory URL if no arguments are supplied. 
  3010. * 
  3011. * @since 2.6.0 
  3012. * 
  3013. * @param string $path Optional. Extra path appended to the end of the URL, including 
  3014. * the relative directory if $plugin is supplied. Default empty. 
  3015. * @param string $plugin Optional. A full path to a file inside a plugin or mu-plugin. 
  3016. * The URL will be relative to its directory. Default empty. 
  3017. * Typically this is done by passing `__FILE__` as the argument. 
  3018. * @return string Plugins URL link with optional paths appended. 
  3019. */ 
  3020. function plugins_url( $path = '', $plugin = '' ) { 
  3021.  
  3022. $path = wp_normalize_path( $path ); 
  3023. $plugin = wp_normalize_path( $plugin ); 
  3024. $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR ); 
  3025.  
  3026. if ( !empty($plugin) && 0 === strpos($plugin, $mu_plugin_dir) ) 
  3027. $url = WPMU_PLUGIN_URL; 
  3028. else 
  3029. $url = WP_PLUGIN_URL; 
  3030.  
  3031.  
  3032. $url = set_url_scheme( $url ); 
  3033.  
  3034. if ( !empty($plugin) && is_string($plugin) ) { 
  3035. $folder = dirname(plugin_basename($plugin)); 
  3036. if ( '.' != $folder ) 
  3037. $url .= '/' . ltrim($folder, '/'); 
  3038.  
  3039. if ( $path && is_string( $path ) ) 
  3040. $url .= '/' . ltrim($path, '/'); 
  3041.  
  3042. /** 
  3043. * Filters the URL to the plugins directory. 
  3044. * 
  3045. * @since 2.8.0 
  3046. * 
  3047. * @param string $url The complete URL to the plugins directory including scheme and path. 
  3048. * @param string $path Path relative to the URL to the plugins directory. Blank string 
  3049. * if no path is specified. 
  3050. * @param string $plugin The plugin file path to be relative to. Blank string if no plugin 
  3051. * is specified. 
  3052. */ 
  3053. return apply_filters( 'plugins_url', $url, $path, $plugin ); 
  3054.  
  3055. /** 
  3056. * Retrieves the site URL for the current network. 
  3057. * 
  3058. * Returns the site URL with the appropriate protocol, 'https' if 
  3059. * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is 
  3060. * overridden. 
  3061. * 
  3062. * @since 3.0.0 
  3063. * 
  3064. * @see set_url_scheme() 
  3065. * 
  3066. * @param string $path Optional. Path relative to the site URL. Default empty. 
  3067. * @param string $scheme Optional. Scheme to give the site URL context. Accepts 
  3068. * 'http', 'https', or 'relative'. Default null. 
  3069. * @return string Site URL link with optional path appended. 
  3070. */ 
  3071. function network_site_url( $path = '', $scheme = null ) { 
  3072. if ( ! is_multisite() ) 
  3073. return site_url($path, $scheme); 
  3074.  
  3075. $current_site = get_current_site(); 
  3076.  
  3077. if ( 'relative' == $scheme ) 
  3078. $url = $current_site->path; 
  3079. else 
  3080. $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme ); 
  3081.  
  3082. if ( $path && is_string( $path ) ) 
  3083. $url .= ltrim( $path, '/' ); 
  3084.  
  3085. /** 
  3086. * Filters the network site URL. 
  3087. * 
  3088. * @since 3.0.0 
  3089. * 
  3090. * @param string $url The complete network site URL including scheme and path. 
  3091. * @param string $path Path relative to the network site URL. Blank string if 
  3092. * no path is specified. 
  3093. * @param string|null $scheme Scheme to give the URL context. Accepts 'http', 'https',  
  3094. * 'relative' or null. 
  3095. */ 
  3096. return apply_filters( 'network_site_url', $url, $path, $scheme ); 
  3097.  
  3098. /** 
  3099. * Retrieves the home URL for the current network. 
  3100. * 
  3101. * Returns the home URL with the appropriate protocol, 'https' is_ssl() 
  3102. * and 'http' otherwise. If `$scheme` is 'http' or 'https', `is_ssl()` is 
  3103. * overridden. 
  3104. * 
  3105. * @since 3.0.0 
  3106. * 
  3107. * @param string $path Optional. Path relative to the home URL. Default empty. 
  3108. * @param string $scheme Optional. Scheme to give the home URL context. Accepts 
  3109. * 'http', 'https', or 'relative'. Default null. 
  3110. * @return string Home URL link with optional path appended. 
  3111. */ 
  3112. function network_home_url( $path = '', $scheme = null ) { 
  3113. if ( ! is_multisite() ) 
  3114. return home_url($path, $scheme); 
  3115.  
  3116. $current_site = get_current_site(); 
  3117. $orig_scheme = $scheme; 
  3118.  
  3119. if ( ! in_array( $scheme, array( 'http', 'https', 'relative' ) ) ) 
  3120. $scheme = is_ssl() && ! is_admin() ? 'https' : 'http'; 
  3121.  
  3122. if ( 'relative' == $scheme ) 
  3123. $url = $current_site->path; 
  3124. else 
  3125. $url = set_url_scheme( 'http://' . $current_site->domain . $current_site->path, $scheme ); 
  3126.  
  3127. if ( $path && is_string( $path ) ) 
  3128. $url .= ltrim( $path, '/' ); 
  3129.  
  3130. /** 
  3131. * Filters the network home URL. 
  3132. * 
  3133. * @since 3.0.0 
  3134. * 
  3135. * @param string $url The complete network home URL including scheme and path. 
  3136. * @param string $path Path relative to the network home URL. Blank string 
  3137. * if no path is specified. 
  3138. * @param string|null $orig_scheme Scheme to give the URL context. Accepts 'http', 'https',  
  3139. * 'relative' or null. 
  3140. */ 
  3141. return apply_filters( 'network_home_url', $url, $path, $orig_scheme); 
  3142.  
  3143. /** 
  3144. * Retrieves the URL to the admin area for the network. 
  3145. * 
  3146. * @since 3.0.0 
  3147. * 
  3148. * @param string $path Optional path relative to the admin URL. Default empty. 
  3149. * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin() 
  3150. * and is_ssl(). 'http' or 'https' can be passed to force those schemes. 
  3151. * @return string Admin URL link with optional path appended. 
  3152. */ 
  3153. function network_admin_url( $path = '', $scheme = 'admin' ) { 
  3154. if ( ! is_multisite() ) 
  3155. return admin_url( $path, $scheme ); 
  3156.  
  3157. $url = network_site_url('wp-admin/network/', $scheme); 
  3158.  
  3159. if ( $path && is_string( $path ) ) 
  3160. $url .= ltrim($path, '/'); 
  3161.  
  3162. /** 
  3163. * Filters the network admin URL. 
  3164. * 
  3165. * @since 3.0.0 
  3166. * 
  3167. * @param string $url The complete network admin URL including scheme and path. 
  3168. * @param string $path Path relative to the network admin URL. Blank string if 
  3169. * no path is specified. 
  3170. */ 
  3171. return apply_filters( 'network_admin_url', $url, $path ); 
  3172.  
  3173. /** 
  3174. * Retrieves the URL to the admin area for the current user. 
  3175. * 
  3176. * @since 3.0.0 
  3177. * 
  3178. * @param string $path Optional. Path relative to the admin URL. Default empty. 
  3179. * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin() 
  3180. * and is_ssl(). 'http' or 'https' can be passed to force those schemes. 
  3181. * @return string Admin URL link with optional path appended. 
  3182. */ 
  3183. function user_admin_url( $path = '', $scheme = 'admin' ) { 
  3184. $url = network_site_url('wp-admin/user/', $scheme); 
  3185.  
  3186. if ( $path && is_string( $path ) ) 
  3187. $url .= ltrim($path, '/'); 
  3188.  
  3189. /** 
  3190. * Filters the user admin URL for the current user. 
  3191. * 
  3192. * @since 3.1.0 
  3193. * 
  3194. * @param string $url The complete URL including scheme and path. 
  3195. * @param string $path Path relative to the URL. Blank string if 
  3196. * no path is specified. 
  3197. */ 
  3198. return apply_filters( 'user_admin_url', $url, $path ); 
  3199.  
  3200. /** 
  3201. * Retrieves the URL to the admin area for either the current site or the network depending on context. 
  3202. * 
  3203. * @since 3.1.0 
  3204. * 
  3205. * @param string $path Optional. Path relative to the admin URL. Default empty. 
  3206. * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin() 
  3207. * and is_ssl(). 'http' or 'https' can be passed to force those schemes. 
  3208. * @return string Admin URL link with optional path appended. 
  3209. */ 
  3210. function self_admin_url( $path = '', $scheme = 'admin' ) { 
  3211. if ( is_network_admin() ) 
  3212. return network_admin_url($path, $scheme); 
  3213. elseif ( is_user_admin() ) 
  3214. return user_admin_url($path, $scheme); 
  3215. else 
  3216. return admin_url($path, $scheme); 
  3217.  
  3218. /** 
  3219. * Sets the scheme for a URL. 
  3220. * 
  3221. * @since 3.4.0 
  3222. * @since 4.4.0 The 'rest' scheme was added. 
  3223. * 
  3224. * @param string $url Absolute URL that includes a scheme 
  3225. * @param string|null $scheme Optional. Scheme to give $url. Currently 'http', 'https', 'login',  
  3226. * 'login_post', 'admin', 'relative', 'rest', 'rpc', or null. Default null. 
  3227. * @return string $url URL with chosen scheme. 
  3228. */ 
  3229. function set_url_scheme( $url, $scheme = null ) { 
  3230. $orig_scheme = $scheme; 
  3231.  
  3232. if ( ! $scheme ) { 
  3233. $scheme = is_ssl() ? 'https' : 'http'; 
  3234. } elseif ( $scheme === 'admin' || $scheme === 'login' || $scheme === 'login_post' || $scheme === 'rpc' ) { 
  3235. $scheme = is_ssl() || force_ssl_admin() ? 'https' : 'http'; 
  3236. } elseif ( $scheme !== 'http' && $scheme !== 'https' && $scheme !== 'relative' ) { 
  3237. $scheme = is_ssl() ? 'https' : 'http'; 
  3238.  
  3239. $url = trim( $url ); 
  3240. if ( substr( $url, 0, 2 ) === '//' ) 
  3241. $url = 'http:' . $url; 
  3242.  
  3243. if ( 'relative' == $scheme ) { 
  3244. $url = ltrim( preg_replace( '#^\w+://[^/]*#', '', $url ) ); 
  3245. if ( $url !== '' && $url[0] === '/' ) 
  3246. $url = '/' . ltrim($url , "/ \t\n\r\0\x0B" ); 
  3247. } else { 
  3248. $url = preg_replace( '#^\w+://#', $scheme . '://', $url ); 
  3249.  
  3250. /** 
  3251. * Filters the resulting URL after setting the scheme. 
  3252. * 
  3253. * @since 3.4.0 
  3254. * 
  3255. * @param string $url The complete URL including scheme and path. 
  3256. * @param string $scheme Scheme applied to the URL. One of 'http', 'https', or 'relative'. 
  3257. * @param string|null $orig_scheme Scheme requested for the URL. One of 'http', 'https', 'login',  
  3258. * 'login_post', 'admin', 'relative', 'rest', 'rpc', or null. 
  3259. */ 
  3260. return apply_filters( 'set_url_scheme', $url, $scheme, $orig_scheme ); 
  3261.  
  3262. /** 
  3263. * Retrieves the URL to the user's dashboard. 
  3264. * 
  3265. * If a user does not belong to any site, the global user dashboard is used. If the user 
  3266. * belongs to the current site, the dashboard for the current site is returned. If the user 
  3267. * cannot edit the current site, the dashboard to the user's primary site is returned. 
  3268. * 
  3269. * @since 3.1.0 
  3270. * 
  3271. * @param int $user_id Optional. User ID. Defaults to current user. 
  3272. * @param string $path Optional path relative to the dashboard. Use only paths known to 
  3273. * both site and user admins. Default empty. 
  3274. * @param string $scheme The scheme to use. Default is 'admin', which obeys force_ssl_admin() 
  3275. * and is_ssl(). 'http' or 'https' can be passed to force those schemes. 
  3276. * @return string Dashboard URL link with optional path appended. 
  3277. */ 
  3278. function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) { 
  3279. $user_id = $user_id ? (int) $user_id : get_current_user_id(); 
  3280.  
  3281. $blogs = get_blogs_of_user( $user_id ); 
  3282. if ( ! is_super_admin() && empty($blogs) ) { 
  3283. $url = user_admin_url( $path, $scheme ); 
  3284. } elseif ( ! is_multisite() ) { 
  3285. $url = admin_url( $path, $scheme ); 
  3286. } else { 
  3287. $current_blog = get_current_blog_id(); 
  3288. if ( $current_blog && ( is_super_admin( $user_id ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) { 
  3289. $url = admin_url( $path, $scheme ); 
  3290. } else { 
  3291. $active = get_active_blog_for_user( $user_id ); 
  3292. if ( $active ) 
  3293. $url = get_admin_url( $active->blog_id, $path, $scheme ); 
  3294. else 
  3295. $url = user_admin_url( $path, $scheme ); 
  3296.  
  3297. /** 
  3298. * Filters the dashboard URL for a user. 
  3299. * 
  3300. * @since 3.1.0 
  3301. * 
  3302. * @param string $url The complete URL including scheme and path. 
  3303. * @param int $user_id The user ID. 
  3304. * @param string $path Path relative to the URL. Blank string if no path is specified. 
  3305. * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',  
  3306. * 'login_post', 'admin', 'relative' or null. 
  3307. */ 
  3308. return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme); 
  3309.  
  3310. /** 
  3311. * Retrieves the URL to the user's profile editor. 
  3312. * 
  3313. * @since 3.1.0 
  3314. * 
  3315. * @param int $user_id Optional. User ID. Defaults to current user. 
  3316. * @param string $scheme Optional. The scheme to use. Default is 'admin', which obeys force_ssl_admin() 
  3317. * and is_ssl(). 'http' or 'https' can be passed to force those schemes. 
  3318. * @return string Dashboard URL link with optional path appended. 
  3319. */ 
  3320. function get_edit_profile_url( $user_id = 0, $scheme = 'admin' ) { 
  3321. $user_id = $user_id ? (int) $user_id : get_current_user_id(); 
  3322.  
  3323. if ( is_user_admin() ) 
  3324. $url = user_admin_url( 'profile.php', $scheme ); 
  3325. elseif ( is_network_admin() ) 
  3326. $url = network_admin_url( 'profile.php', $scheme ); 
  3327. else 
  3328. $url = get_dashboard_url( $user_id, 'profile.php', $scheme ); 
  3329.  
  3330. /** 
  3331. * Filters the URL for a user's profile editor. 
  3332. * 
  3333. * @since 3.1.0 
  3334. * 
  3335. * @param string $url The complete URL including scheme and path. 
  3336. * @param int $user_id The user ID. 
  3337. * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',  
  3338. * 'login_post', 'admin', 'relative' or null. 
  3339. */ 
  3340. return apply_filters( 'edit_profile_url', $url, $user_id, $scheme); 
  3341.  
  3342. /** 
  3343. * Returns the canonical URL for a post. 
  3344. * 
  3345. * When the post is the same as the current requested page the function will handle the 
  3346. * pagination arguments too. 
  3347. * 
  3348. * @since 4.6.0 
  3349. * 
  3350. * @param int|WP_Post $post Optional. Post ID or object. Default is global `$post`. 
  3351. * @return string|false The canonical URL, or false if the post does not exist or has not 
  3352. * been published yet. 
  3353. */ 
  3354. function wp_get_canonical_url( $post = null ) { 
  3355. $post = get_post( $post ); 
  3356.  
  3357. if ( ! $post ) { 
  3358. return false; 
  3359.  
  3360. if ( 'publish' !== $post->post_status ) { 
  3361. return false; 
  3362.  
  3363. $canonical_url = get_permalink( $post ); 
  3364.  
  3365. // If a canonical is being generated for the current page, make sure it has pagination if needed. 
  3366. if ( $post->ID === get_queried_object_id() ) { 
  3367. $page = get_query_var( 'page', 0 ); 
  3368. if ( $page >= 2 ) { 
  3369. if ( '' == get_option( 'permalink_structure' ) ) { 
  3370. $canonical_url = add_query_arg( 'page', $page, $canonical_url ); 
  3371. } else { 
  3372. $canonical_url = trailingslashit( $canonical_url ) . user_trailingslashit( $page, 'single_paged' ); 
  3373.  
  3374. $cpage = get_query_var( 'cpage', 0 ); 
  3375. if ( $cpage ) { 
  3376. $canonical_url = get_comments_pagenum_link( $cpage ); 
  3377.  
  3378. /** 
  3379. * Filters the canonical URL for a post. 
  3380. * 
  3381. * @since 4.6.0 
  3382. * 
  3383. * @param string $string The post's canonical URL. 
  3384. * @param WP_Post $post Post object. 
  3385. */ 
  3386. return apply_filters( 'get_canonical_url', $canonical_url, $post ); 
  3387.  
  3388. /** 
  3389. * Outputs rel=canonical for singular queries. 
  3390. * 
  3391. * @since 2.9.0 
  3392. * @since 4.6.0 Adjusted to use wp_get_canonical_url(). 
  3393. */ 
  3394. function rel_canonical() { 
  3395. if ( ! is_singular() ) { 
  3396. return; 
  3397.  
  3398. $id = get_queried_object_id(); 
  3399.  
  3400. if ( 0 === $id ) { 
  3401. return; 
  3402.  
  3403. $url = wp_get_canonical_url( $id ); 
  3404.  
  3405. if ( ! empty( $url ) ) { 
  3406. echo '<link rel="canonical" href="' . esc_url( $url ) . '" />' . "\n"; 
  3407.  
  3408. /** 
  3409. * Returns a shortlink for a post, page, attachment, or site. 
  3410. * 
  3411. * This function exists to provide a shortlink tag that all themes and plugins can target. 
  3412. * A plugin must hook in to provide the actual shortlinks. Default shortlink support is 
  3413. * limited to providing ?p= style links for posts. Plugins can short-circuit this function 
  3414. * via the {@see 'pre_get_shortlink'} filter or filter the output via the {@see 'get_shortlink'} 
  3415. * filter. 
  3416. * 
  3417. * @since 3.0.0. 
  3418. * 
  3419. * @param int $id Optional. A post or site id. Default is 0, which means the current post or site. 
  3420. * @param string $context Optional. Whether the id is a 'site' id, 'post' id, or 'media' id. If 'post',  
  3421. * the post_type of the post is consulted. If 'query', the current query is consulted 
  3422. * to determine the id and context. Default 'post'. 
  3423. * @param bool $allow_slugs Optional. Whether to allow post slugs in the shortlink. It is up to the plugin how 
  3424. * and whether to honor this. Default true. 
  3425. * @return string A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks 
  3426. * are not enabled. 
  3427. */ 
  3428. function wp_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) { 
  3429. /** 
  3430. * Filters whether to preempt generating a shortlink for the given post. 
  3431. * 
  3432. * Passing a truthy value to the filter will effectively short-circuit the 
  3433. * shortlink-generation process, returning that value instead. 
  3434. * 
  3435. * @since 3.0.0 
  3436. * 
  3437. * @param bool|string $return Short-circuit return value. Either false or a URL string. 
  3438. * @param int $id Post ID, or 0 for the current post. 
  3439. * @param string $context The context for the link. One of 'post' or 'query',  
  3440. * @param bool $allow_slugs Whether to allow post slugs in the shortlink. 
  3441. */ 
  3442. $shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs ); 
  3443.  
  3444. if ( false !== $shortlink ) { 
  3445. return $shortlink; 
  3446.  
  3447. $post_id = 0; 
  3448. if ( 'query' == $context && is_singular() ) { 
  3449. $post_id = get_queried_object_id(); 
  3450. $post = get_post( $post_id ); 
  3451. } elseif ( 'post' == $context ) { 
  3452. $post = get_post( $id ); 
  3453. if ( ! empty( $post->ID ) ) 
  3454. $post_id = $post->ID; 
  3455.  
  3456. $shortlink = ''; 
  3457.  
  3458. // Return p= link for all public post types. 
  3459. if ( ! empty( $post_id ) ) { 
  3460. $post_type = get_post_type_object( $post->post_type ); 
  3461.  
  3462. if ( 'page' === $post->post_type && $post->ID == get_option( 'page_on_front' ) && 'page' == get_option( 'show_on_front' ) ) { 
  3463. $shortlink = home_url( '/' ); 
  3464. } elseif ( $post_type->public ) { 
  3465. $shortlink = home_url( '?p=' . $post_id ); 
  3466.  
  3467. /** 
  3468. * Filters the shortlink for a post. 
  3469. * 
  3470. * @since 3.0.0 
  3471. * 
  3472. * @param string $shortlink Shortlink URL. 
  3473. * @param int $id Post ID, or 0 for the current post. 
  3474. * @param string $context The context for the link. One of 'post' or 'query',  
  3475. * @param bool $allow_slugs Whether to allow post slugs in the shortlink. Not used by default. 
  3476. */ 
  3477. return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs ); 
  3478.  
  3479. /** 
  3480. * Injects rel=shortlink into the head if a shortlink is defined for the current page. 
  3481. * 
  3482. * Attached to the {@see 'wp_head'} action. 
  3483. * 
  3484. * @since 3.0.0 
  3485. */ 
  3486. function wp_shortlink_wp_head() { 
  3487. $shortlink = wp_get_shortlink( 0, 'query' ); 
  3488.  
  3489. if ( empty( $shortlink ) ) 
  3490. return; 
  3491.  
  3492. echo "<link rel='shortlink' href='" . esc_url( $shortlink ) . "' />\n"; 
  3493.  
  3494. /** 
  3495. * Sends a Link: rel=shortlink header if a shortlink is defined for the current page. 
  3496. * 
  3497. * Attached to the {@see 'wp'} action. 
  3498. * 
  3499. * @since 3.0.0 
  3500. */ 
  3501. function wp_shortlink_header() { 
  3502. if ( headers_sent() ) 
  3503. return; 
  3504.  
  3505. $shortlink = wp_get_shortlink(0, 'query'); 
  3506.  
  3507. if ( empty($shortlink) ) 
  3508. return; 
  3509.  
  3510. header('Link: <' . $shortlink . '>; rel=shortlink', false); 
  3511.  
  3512. /** 
  3513. * Displays the shortlink for a post. 
  3514. * 
  3515. * Must be called from inside "The Loop" 
  3516. * 
  3517. * Call like the_shortlink( __( 'Shortlinkage FTW' ) ) 
  3518. * 
  3519. * @since 3.0.0 
  3520. * 
  3521. * @param string $text Optional The link text or HTML to be displayed. Defaults to 'This is the short link.' 
  3522. * @param string $title Optional The tooltip for the link. Must be sanitized. Defaults to the sanitized post title. 
  3523. * @param string $before Optional HTML to display before the link. Default empty. 
  3524. * @param string $after Optional HTML to display after the link. Default empty. 
  3525. */ 
  3526. function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) { 
  3527. $post = get_post(); 
  3528.  
  3529. if ( empty( $text ) ) 
  3530. $text = __('This is the short link.'); 
  3531.  
  3532. if ( empty( $title ) ) 
  3533. $title = the_title_attribute( array( 'echo' => false ) ); 
  3534.  
  3535. $shortlink = wp_get_shortlink( $post->ID ); 
  3536.  
  3537. if ( !empty( $shortlink ) ) { 
  3538. $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>'; 
  3539.  
  3540. /** 
  3541. * Filters the short link anchor tag for a post. 
  3542. * 
  3543. * @since 3.0.0 
  3544. * 
  3545. * @param string $link Shortlink anchor tag. 
  3546. * @param string $shortlink Shortlink URL. 
  3547. * @param string $text Shortlink's text. 
  3548. * @param string $title Shortlink's title attribute. 
  3549. */ 
  3550. $link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title ); 
  3551. echo $before, $link, $after; 
  3552.  
  3553.  
  3554. /** 
  3555. * Retrieves the avatar URL. 
  3556. * 
  3557. * @since 4.2.0 
  3558. * 
  3559. * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash,  
  3560. * user email, WP_User object, WP_Post object, or WP_Comment object. 
  3561. * @param array $args { 
  3562. * Optional. Arguments to return instead of the default arguments. 
  3563. * 
  3564. * @type int $size Height and width of the avatar in pixels. Default 96. 
  3565. * @type string $default URL for the default image or a default type. Accepts '404' (return 
  3566. * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),  
  3567. * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',  
  3568. * or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or 
  3569. * 'gravatar_default' (the Gravatar logo). Default is the value of the 
  3570. * 'avatar_default' option, with a fallback of 'mystery'. 
  3571. * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false. 
  3572. * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are 
  3573. * judged in that order. Default is the value of the 'avatar_rating' option. 
  3574. * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values. 
  3575. * Default null. 
  3576. * @type array $processed_args When the function returns, the value will be the processed/sanitized $args 
  3577. * plus a "found_avatar" guess. Pass as a reference. Default null. 
  3578. * } 
  3579. * @return false|string The URL of the avatar we found, or false if we couldn't find an avatar. 
  3580. */ 
  3581. function get_avatar_url( $id_or_email, $args = null ) { 
  3582. $args = get_avatar_data( $id_or_email, $args ); 
  3583. return $args['url']; 
  3584.  
  3585. /** 
  3586. * Retrieves default data about the avatar. 
  3587. * 
  3588. * @since 4.2.0 
  3589. * 
  3590. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,  
  3591. * user email, WP_User object, WP_Post object, or WP_Comment object. 
  3592. * @param array $args { 
  3593. * Optional. Arguments to return instead of the default arguments. 
  3594. * 
  3595. * @type int $size Height and width of the avatar image file in pixels. Default 96. 
  3596. * @type int $height Display height of the avatar in pixels. Defaults to $size. 
  3597. * @type int $width Display width of the avatar in pixels. Defaults to $size. 
  3598. * @type string $default URL for the default image or a default type. Accepts '404' (return 
  3599. * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),  
  3600. * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',  
  3601. * or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), or 
  3602. * 'gravatar_default' (the Gravatar logo). Default is the value of the 
  3603. * 'avatar_default' option, with a fallback of 'mystery'. 
  3604. * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false. 
  3605. * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are 
  3606. * judged in that order. Default is the value of the 'avatar_rating' option. 
  3607. * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values. 
  3608. * Default null. 
  3609. * @type array $processed_args When the function returns, the value will be the processed/sanitized $args 
  3610. * plus a "found_avatar" guess. Pass as a reference. Default null. 
  3611. * @type string $extra_attr HTML attributes to insert in the IMG element. Is not sanitized. Default empty. 
  3612. * } 
  3613. * @return array $processed_args { 
  3614. * Along with the arguments passed in `$args`, this will contain a couple of extra arguments. 
  3615. * 
  3616. * @type bool $found_avatar True if we were able to find an avatar for this user,  
  3617. * false or not set if we couldn't. 
  3618. * @type string $url The URL of the avatar we found. 
  3619. * } 
  3620. */ 
  3621. function get_avatar_data( $id_or_email, $args = null ) { 
  3622. $args = wp_parse_args( $args, array( 
  3623. 'size' => 96,  
  3624. 'height' => null,  
  3625. 'width' => null,  
  3626. 'default' => get_option( 'avatar_default', 'mystery' ),  
  3627. 'force_default' => false,  
  3628. 'rating' => get_option( 'avatar_rating' ),  
  3629. 'scheme' => null,  
  3630. 'processed_args' => null, // if used, should be a reference 
  3631. 'extra_attr' => '',  
  3632. ) ); 
  3633.  
  3634. if ( is_numeric( $args['size'] ) ) { 
  3635. $args['size'] = absint( $args['size'] ); 
  3636. if ( ! $args['size'] ) { 
  3637. $args['size'] = 96; 
  3638. } else { 
  3639. $args['size'] = 96; 
  3640.  
  3641. if ( is_numeric( $args['height'] ) ) { 
  3642. $args['height'] = absint( $args['height'] ); 
  3643. if ( ! $args['height'] ) { 
  3644. $args['height'] = $args['size']; 
  3645. } else { 
  3646. $args['height'] = $args['size']; 
  3647.  
  3648. if ( is_numeric( $args['width'] ) ) { 
  3649. $args['width'] = absint( $args['width'] ); 
  3650. if ( ! $args['width'] ) { 
  3651. $args['width'] = $args['size']; 
  3652. } else { 
  3653. $args['width'] = $args['size']; 
  3654.  
  3655. if ( empty( $args['default'] ) ) { 
  3656. $args['default'] = get_option( 'avatar_default', 'mystery' ); 
  3657.  
  3658. switch ( $args['default'] ) { 
  3659. case 'mm' : 
  3660. case 'mystery' : 
  3661. case 'mysteryman' : 
  3662. $args['default'] = 'mm'; 
  3663. break; 
  3664. case 'gravatar_default' : 
  3665. $args['default'] = false; 
  3666. break; 
  3667.  
  3668. $args['force_default'] = (bool) $args['force_default']; 
  3669.  
  3670. $args['rating'] = strtolower( $args['rating'] ); 
  3671.  
  3672. $args['found_avatar'] = false; 
  3673.  
  3674. /** 
  3675. * Filters whether to retrieve the avatar URL early. 
  3676. * 
  3677. * Passing a non-null value in the 'url' member of the return array will 
  3678. * effectively short circuit get_avatar_data(), passing the value through 
  3679. * the {@see 'get_avatar_data'} filter and returning early. 
  3680. * 
  3681. * @since 4.2.0 
  3682. * 
  3683. * @param array $args Arguments passed to get_avatar_data(), after processing. 
  3684. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,  
  3685. * user email, WP_User object, WP_Post object, or WP_Comment object. 
  3686. */ 
  3687. $args = apply_filters( 'pre_get_avatar_data', $args, $id_or_email ); 
  3688.  
  3689. if ( isset( $args['url'] ) && ! is_null( $args['url'] ) ) { 
  3690. /** This filter is documented in wp-includes/link-template.php */ 
  3691. return apply_filters( 'get_avatar_data', $args, $id_or_email ); 
  3692.  
  3693. $email_hash = ''; 
  3694. $user = $email = false; 
  3695.  
  3696. if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) { 
  3697. $id_or_email = get_comment( $id_or_email ); 
  3698.  
  3699. // Process the user identifier. 
  3700. if ( is_numeric( $id_or_email ) ) { 
  3701. $user = get_user_by( 'id', absint( $id_or_email ) ); 
  3702. } elseif ( is_string( $id_or_email ) ) { 
  3703. if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) { 
  3704. // md5 hash 
  3705. list( $email_hash ) = explode( '@', $id_or_email ); 
  3706. } else { 
  3707. // email address 
  3708. $email = $id_or_email; 
  3709. } elseif ( $id_or_email instanceof WP_User ) { 
  3710. // User Object 
  3711. $user = $id_or_email; 
  3712. } elseif ( $id_or_email instanceof WP_Post ) { 
  3713. // Post Object 
  3714. $user = get_user_by( 'id', (int) $id_or_email->post_author ); 
  3715. } elseif ( $id_or_email instanceof WP_Comment ) { 
  3716. /** 
  3717. * Filters the list of allowed comment types for retrieving avatars. 
  3718. * 
  3719. * @since 3.0.0 
  3720. * 
  3721. * @param array $types An array of content types. Default only contains 'comment'. 
  3722. */ 
  3723. $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); 
  3724. if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) { 
  3725. $args['url'] = false; 
  3726. /** This filter is documented in wp-includes/link-template.php */ 
  3727. return apply_filters( 'get_avatar_data', $args, $id_or_email ); 
  3728.  
  3729. if ( ! empty( $id_or_email->user_id ) ) { 
  3730. $user = get_user_by( 'id', (int) $id_or_email->user_id ); 
  3731. if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) { 
  3732. $email = $id_or_email->comment_author_email; 
  3733.  
  3734. if ( ! $email_hash ) { 
  3735. if ( $user ) { 
  3736. $email = $user->user_email; 
  3737.  
  3738. if ( $email ) { 
  3739. $email_hash = md5( strtolower( trim( $email ) ) ); 
  3740.  
  3741. if ( $email_hash ) { 
  3742. $args['found_avatar'] = true; 
  3743. $gravatar_server = hexdec( $email_hash[0] ) % 3; 
  3744. } else { 
  3745. $gravatar_server = rand( 0, 2 ); 
  3746.  
  3747. $url_args = array( 
  3748. 's' => $args['size'],  
  3749. 'd' => $args['default'],  
  3750. 'f' => $args['force_default'] ? 'y' : false,  
  3751. 'r' => $args['rating'],  
  3752. ); 
  3753.  
  3754. if ( is_ssl() ) { 
  3755. $url = 'https://secure.gravatar.com/avatar/' . $email_hash; 
  3756. } else { 
  3757. $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $email_hash ); 
  3758.  
  3759. $url = add_query_arg( 
  3760. rawurlencode_deep( array_filter( $url_args ) ),  
  3761. set_url_scheme( $url, $args['scheme'] ) 
  3762. ); 
  3763.  
  3764. /** 
  3765. * Filters the avatar URL. 
  3766. * 
  3767. * @since 4.2.0 
  3768. * 
  3769. * @param string $url The URL of the avatar. 
  3770. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,  
  3771. * user email, WP_User object, WP_Post object, or WP_Comment object. 
  3772. * @param array $args Arguments passed to get_avatar_data(), after processing. 
  3773. */ 
  3774. $args['url'] = apply_filters( 'get_avatar_url', $url, $id_or_email, $args ); 
  3775.  
  3776. /** 
  3777. * Filters the avatar data. 
  3778. * 
  3779. * @since 4.2.0 
  3780. * 
  3781. * @param array $args Arguments passed to get_avatar_data(), after processing. 
  3782. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,  
  3783. * user email, WP_User object, WP_Post object, or WP_Comment object. 
  3784. */ 
  3785. return apply_filters( 'get_avatar_data', $args, $id_or_email ); 
.