link-template.php 147 KB

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