Is that possible to create public function that we could access from another site, like a "custom API" on a WordPress site ?
For example, URL "my-wordpress.com/my_function" would return the code defined in my_function().
Thanks for answers,
Sébastien.
You can use the XMLRPC interface provided by WordPress.
See these links:
http://codex.wordpress.org/XML-RPC_Support
http://codex.wordpress.org/XML-RPC_Extending
This might be the right way to offer a custom API, but it doesn't allow you to simply call a function using a URL. Communication is done via XML that you send to the xmlrpc.php in your wordpress root.
Related
We want to use the Wordpress Rest-API to build a page decoupled from the backend. A few things still bother us though:
Within the wordpress backend one can add links in the content editor of a page or a post and one gets normally a list of all existing pages and posts of the same page to link to them. By decoupling the backend it does not know the exact urls to other pages and we need to provide this. Is there a possibility to tell wordpress what links are available?
Thanks in advance.
Cheers
For those looking for the answer:
There are two URL settings under Settings > General.
WordPress Address (URL) should be the URL for your api site. Like api.whatever.com.
Site Address (URL) should be the URL for the frontend. Like www.whatever.com.
This allows the internal linking to work properly in the Editor while keeping the backend site on a separate URL from a headless frontend.
I'm not sure what you're trying to ask. The WordPress REST API just provides a way to access posts, etc. from somewhere else part from the web site side (e.g. themes, etc). Pages would still be accessible from the admin backend and the JSON/REST API: https://developer.wordpress.org/rest-api/reference/pages/
Specifically with WordPress you could ensure that you're not rewriting the permalinks. That way they are all still site.com/?p=post-id
That way it's easy to parse fetch the new pages. Then replace the hrefs with whatever your frontend needs.
You could use the ID to fetch the slug of the new post immediately.
Or you could even keep a mapping in your presentation layer of IDs to your own slugs.
Changing the site_url doesn't work for me.
Since I also have a wordpress theme with custom rest controllers
Plus the wp-json breaks if I change the site url
My backend is hosted at digital ocean app
My frontend is hosted at vercel
I fixed it by using a wordpress plugin called make-paths-relative
But since my backend doesnt know where my frontend is I added one change within the plugin at:
frontend/class-make-paths-relative.php line 123
I added a constant FRONTEND_URL which i define in the wp-config
$relative_link = FRONTEND_URL.$link;
This changes both the permalink within my backend of a post, page or cpt
And changes internal links as wel, links within the content editor
I made a request to the plugin author as well
I'm currently working on application where I want to use data from my blog which is hosted on digital ocean. I have installed wordpress for the blog on the server with domain name abc.com. I have to call wordpress rest api to get data without authentication if possible or using application client secret key. How we can do that?.
While calling like this "https://public-api.wordpress.com/rest/v1.1/sites/abc.com/posts/?pretty=true"
this return
{
"error": "unknown_blog",
"message": "Unknown blog"
}
do you mean
abc.com/wp-json/wp/v2/posts
url like this will get the all posts title content, and if you want to get single post, you can do
abc.com/wp-json/wp/v2/posts/1
1 is post_id , you can replace with other post id
Below function is useful for register endpoint for creating API from function.php file. Refer below link for further deployment.
register_rest_route
https://developer.wordpress.org/reference/functions/register_rest_route/
Below link is very helpful for create API in WP environment.
http://v2.wp-api.org/
You can get overview from below word press popular plugin as well, but in latest word press there is no need to use this plugin as default word press provide almost all functionalities.
WP REST API (WP API)
I'm trying to write a plugin that would add these endpoints GET, POST, DELETE /api/checked to access the plugin's custom table.
Is it possible for a Wordpress plugin to expose a RESTful API? I can't seem to find any examples or documentation.
The closest thing I found was add_rewrite_rule. I was able to create http://www.blog.com/api, but how do I determine the HTTP verb?
You can use WP REST API. Just install it and then go to settings -> permalink and add /%postname%/ on the last field.
then you'll have everything you need at [url]/json-api/
Here is the doc
You can use JSON-API WordPress plugin for creating the web-services that can be used for fetching the data from Wordpress.
And the plugin will provide the webservice url something like that :
www.mysite.com/api/servicename
Plugin link : https://wordpress.org/plugins/json-api/
Hope it helps you some extent.
I have been tasked with creating an API for retrieving and adding content to Wordpress from a flash application and legacy CMS (non-PHP). My plan is to utilise the existing default xmlrpc endpoint and add any additional functionality by creating a plugin which hooks into xmlrpc_methods.
A previous attempt had been made by another developer based on the following code:
http://blog.5ubliminal.com/posts/remote-control-wordpress-blog-xmlrpc-api/
This code looks unwieldy and poorly documented to me and my preference would be to use this approach:
http://kovshenin.com/archives/custom-xml-rpc-methods-in-wordpress/
I would be grateful if anyone with experience in this area could confirm that:
I will be able to distinguish between separate blogs in an MU installation when both retrieving and posting data via XMLRPC
I will be able to retrieve and post to custom fields
writing a plugin is the way to go.
We do not have the option of using Wordpress 3 as it is still in Beta and we are under time pressure.
I would greatly appreciate appreciate any input / advice.
Many thanks,
I've worked with WordPress' XMLRPC system before (using a WP-Hive installation with multiple separate blogs similar to a WPMU set-up). The new approach you're using is definitely simpler and easier to implement (I tried the 5ubliminal one as well the first time).
Whether or not you can distinguish between separate blogs in a MU installation depends entirely on how you build your handler function. You can build it to distinguish the separate blogs, to only function on specific blogs, or to treat the entire system as a single WordPress site. It's all up to you.
By "handler function" I mean a custom function you define to handle XMLRPC requests that call a specific, custom method (not necessarily the default WordPress methods). For example, I use XMLRPC in all my plug-ins to report back installation progress and errors -
each plug-in makes an XMLRPC call to a custom handler (method) on my server.
Yes, you can retrieve and post to custom fields.
Absolutely writing a plug-in is the way to go. The only other options are to change core files (BAD idea) or to build it into your theme, in which case it could ONLY be used on MU sites using that theme. Build it as a site-wide MU plug-in that can be controlled on a site-by-site basis by the global admin.
Wordpress XMLRPC offers various functionalities which can be harvested easily. I have used IXR_Library to parse the XML requests/responses. Currently with very small piece of code i can easily posts, fetch, edit and delete Posts in Wordpress based blogs either self hosted or on wordpress.com sites.
http://www.hurricanesoftwares.com/wordpress-xmlrpc-posting-content-from-outside-wordpress-admin-panel/ (reference)
When you have multiple blogs hosted via MU you will need site ID of all those blogs which will become the first parameter for $params (in our case 0 should be replaced with site_id).
In the reference i gave above you will see the option to fetch and post to all created custom fields (unfortunately, you can't create custom fields on the fly from my script)
You are welcome to write a WP plugin to do all of this, be my guest and let me know if you need my help. I have used the same technique to post to blogger, tumblr, Wordpress and Posterous using their API's. I hope this helps.
I have a MU site related to Doctors and they will be publishing information that I cannot let outside sources and robots view.
Is there a way that when you go to the main domain url, it shows a user/pass login for WP. I would set up the user/pass in the backend. Then they would login, then be able to view the website? I can't have it load up for anyone, I need the website to be password protected to even view the website in general.
I know I can use .htaccess to have a user/pass, but it would be ideal to use the user/pass generated by WP so I don't have to mess with code when I create a user.
Any help would be greatly appreciated.
This might be what you're looking for, although I've never actually used it.
If you're into programming and customizing WordPress themes (and want to create the solution yourself) add the following code to the top of your functions.php file:
function private_blog() {
if (!is_user_logged_in()) {
auth_redirect();
}
}
add_action('template_redirect','private_blog');
If you want a plugin solution, there are plenty. Just do a search for "wordpress private" and you'll find a ton. Private Only is one that I've used successfully.