Create custom wordpress url to query data from external database - wordpress

so, I have a Wordpress Setup with a custom homepage template. In the template I query some products from an external database. Now I want to create a detail of a product, and this is where im stuck..
I don't know how to implement this since there is no data in the WP db thus no actual url (I guess?).. I thought about making 1 page in WP called 'Detail' and make the href attribute on the homepage products like this 'www.example.com/detail/123', get the id from the url en with that id query to the database..
Is this the right approach? Hope someone can help..
thanks!

If i understand you right, you want to query content from one wordpress website to another right ?
The right way to do it is to use JSON APIs which will let u retrieve the content you need using HTTP requests.
You can set these APIs yourself, refer to the following tutorial.
Thus, you can also use this wonderful wordpress plugin :
JSON API
After installing and enabling the plugin you'll have to set your API links according to the content you need to get, for example the following api:
http://example.com/api/get_posts/?post_type=custom_post_type&count=3
queries the latest 3 custom posts along with their fields and custom fields (if present).
Find more examples here.
Now on your other website where you want to display the content you 'll need to use JSON decode to convert the JSON string into a PHP obect or array.
For example:
$json = '{"a":hello,"b":hi,"c":hey,"d":yo,"e":ola}';
$data = json_decode($json);
echo $data->{'a'}
// this should echo the value "hello"
As stated here.
I hope that helps !

I don't think he wants to query another WordPress. He only says that he fetches data from another database that contains products.
Your idea is pretty close to what you have to do. Take a look at add_rewrite_rule, add_rewrite_tag and get_query_var.
This tuts is a good example for you: http://code.tutsplus.com/articles/custom-page-template-page-based-on-url-rewrite--wp-30564

Related

Is it possible to create custom entities (entries if you take up the strapi vocabulary) in wordpress?

Edit : the thing I was looking for is called Custom Post Type in the WordPress World
Disclaimer: it is my first steps with WordPress and I realize I do not have the right vocabulary to make a proper google search, so please excuse me if this answer has been already asked many times ...
I'm working on a website that use an old preexisting WordPress as headless CMS.
On this new website, I need to display some "job offers"
On the preexisting WordPress, those job offers are just HTML blocks embedded in a page. So I can't get only those without having the embedded page HTML and CSS
If I could have created a custom REST API to get those "job offers", I would have said that those resources would look like this :
{
title : string,
description: string
}
My first idea would be to create a WordPress custom end point, but I do not know if it is possible to create custom entities (entries (?) If I take up the vocabulary of strapi)
Is this something that can be done?
I've tried :
basic google search
reading the strapi and wordpress documentation
I expect to be referred to a solution or to know what to search
The thing you're looking for are Custom post types if that can help a plugin called ACF can help with the custom fields.

How can I fetch the post slugs of all languages?

I have a react project which fetches data on wordpress via api calls (plugins installed: ACF and WPML).
I have the Event CPT and viewing the page localhost: 3000 / events / first-event, I retrieve the information via the slug.
If on this page I wanted to retrieve the same event in another language, I could add the country code in the url and make a redirect.
It is possible that the same event has different slugs based on the language and the feature does not work.
Do you have any ideas on how to make this feature?
Thank you guys!
I have temporarily modified the pathname.
<a href={window.location.pathname.replace('/','/en/')}>EN</a>
I have found the solution.
I used the icl_get_languages() function to fetch the languages, icl_object_id() to retrieve the post id of a language, get_post to retrieve all fields and update_post_meta to add a custom field to the post.
Hey I have the same setting but not with React.
If you are using the API without any modifications in the class and given that the title between language is the same try the following:
/wp-json/wp/v2/posts/?post-slug=second+post&lang=fr
and page slug
/wp-json/wp/v2/pages/?page-slug=second+post&lang=fr
However, you need to modify the API response if you want your ACF fields to be there (or use a plugin).
Also mind that post ID between translations is different. Ensure to include the lang as argument in the modification of the WP_REST_Controller. https://developer.wordpress.org/reference/classes/wp_rest_controller/get_collection_params/
Let me know if that helps.

how can we create posts and pages using wordpress REST APIs?

I have a preformatted HTML string that I want to use to create a POST or PAGE through wordpress REST APIs. The purpose is to avoid formatting after the POST or PAGE is created.
The problem is wordpress apis to create post/page do not take HTML as a parameter.
I think you can pass your content as a json format. Please check the screenshot

create a post in Wordpress with an external script

I am making a java script to create a few posts in a Wordpress blog by only inserting them into the wp_posts so table. So far I could browse the posts on web browser. Question is, is it good enough with the db operations ? does anyone know any other subsequent tables need to be updated for making a post ?
Not really, you can extend the default and existing WordPress posts.
But you can go ahead and create your own table like: https://github.com/wp-plugins/wordpress-guest-post/blob/master/wordpressguestpost.php
ended up adding REST API V2 plugin and make calls to it for creating all sorts of things. Worked like a champ

Modify custom field from front end

If I need to manage a custom field under wordpress, but not from the admin area. In other words, I need that users modify the content of post custom fields from front end. What is the way to do so?
As instances, please to imagine Stackoverflow.com as a wordpress site. Under this site, the number of upvotes/downvotes and the accept state are my 2 post custom fields. How can I make the front end user modify their values in WP database?
Please If you have another better approach to create votes system and accept system, using wordpress, it will be highly appreciated. But if my approach (using post custom field) is okey, please to guide me find documentation from web or codex to achieve my goal.
Thank you for your usual help.
I would suggest thinking about a couple things:
Each post should show the sum of up-votes and down-votes
Each user should only be able to up or down-vote once (i.e., one user can't vote the same post up a hundred times)
You think you would want to create a function for your theme that allowed users when logged in to alter the metadata of a post. However, this isn't really the case – what you need to do is to allow users to edit an element of their own metadata (see add_user_meta) that is basically just a hash. The metadata could be like array("post-666-vote" => -1, "post-777-vote" => 1) for a downvote of post 666 and upvote of post 777.
So, when loading each post, your vote-tally-renderer would look something like this:
vote = get_post_meta($user_id, "post-" . $post_id . "-vote", true);
if(vote == -1) {
// Render down-arrow voting
} elsif(vote == 1) {
// Render up-arrow voting
} else {
// Render normal arrows
}
Each arrow would probably have to make an AJAX request to update the post's metadata. This is a long tutorial (but it's not super complicated! I promise!) and about 3/4 of the way down it talks about how to use an AJAX request to modify some user's metadata. http://wp.tutsplus.com/tutorials/plugins/a-primer-on-ajax-in-the-wordpress-frontend-actually-doing-it/
The only real major change is that the PHP function that is called by the WP AJAX request would also need a callback function to update the post's metadata, and change the number of votes on the post itself. This way, when the page is reloaded, it will show the proper number of votes.

Resources