最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Accessing WordPress Functions get_permalink() in Vanilla PHP?

programmeradmin1浏览0评论

Simple question. I'm attempting to use get_permalink() in vanilla php but cannot access it. I have include_once all these "used" files:

  • wp-settings.php
  • wp-includes/load.php
  • wp-includes/link-template.php
  • wp-includes/rewrite.php
  • wp-includes/functions.php

and 10 other files. How do I access get_permalink()?

Simple question. I'm attempting to use get_permalink() in vanilla php but cannot access it. I have include_once all these "used" files:

  • wp-settings.php
  • wp-includes/load.php
  • wp-includes/link-template.php
  • wp-includes/rewrite.php
  • wp-includes/functions.php

and 10 other files. How do I access get_permalink()?

Share Improve this question edited Aug 20, 2020 at 13:37 admcfajn 1,3262 gold badges13 silver badges30 bronze badges asked Aug 20, 2020 at 5:15 stkmediastkmedia 12 bronze badges 2
  • There’s no such function as get_permalinks(). It’s get_permalink(). But why do you need this in vanilla PHP? It doesn’t make sense to use outside of WordPress, and isn’t designed to be. – Jacob Peattie Commented Aug 20, 2020 at 8:00
  • I created a feature-rich search page on a client's website, she has a Wordpress install and wanted links to her blog posts, so to create those links dynamically for her ongoing recent posts I need access to the links, but Wordpress uses Apache rewrite to make those permalinks dynamically, so I came up with this workaround. I'm not a Wordpress expert. – stkmedia Commented Aug 21, 2020 at 5:14
Add a comment  | 

2 Answers 2

Reset to default 0

The developer's reference is a great place to start. You'll find get_permalink in wp-includes/link-template.php.

You might want to look into wp-cli wp post list --post__in=1 --field=url should do the trick as discussed in wp-cli issue#2727

Also, there's the rest-api to consider. The best solution for getting the permalink might be to just post/cURL a request to WordPress' rest-api to get the permalinks you want.

This question might be a good place to start: WP JSON list all permalinks

For example, you could do something like the following: curl https://example/wp-json/wp/v2/posts/<id> or curl https://example/wp-json/wp/v2/pages/<id>

& then find the guid / permalink from there.

I worked it out. This file loads Wordpress and makes functions accessible.

include_once("wp-load.php");

then was able to

$id = $rowa["ID"];  //from MySQL fetch array
$permalink = get_permalink(number_format($id));
发布评论

评论列表(0)

  1. 暂无评论