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.
Related
I'm using the WooCommerce REST API (http://woocommerce.github.io/woocommerce-rest-api-docs/#introduction) and are able to download Customers, Orders etc successfully.
My customer has added some custom fields to the checkout page, e.g.:
po_number
accounts_email
I would like to be able to GET these custom fields when requesting an ORDER, e.g.:
/wp-json/wc/v1/orders/4568
At the moment none of the custom fields are returned when I GET an Order. I've tried adding
?filter[meta]=true
to the request URL but that doesn't make any difference. Is there a way I can modify the GET URL to include all custom fields? Couldn't find any reference in the REST API docs to custom fields.
I have struggled like you did, but then it turns out using the newer v2 REST API solves the issue for me. So instead of:
/wp-json/wc/v1/orders/4568
I now use:
/wp-json/wc/v2/orders/4568
And custom fields will be included in the response with the key meta_data, even with those hidden fields starting with _.
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
I'm trying to use the value of one custom field on multiple pages and posts. I'm a newbie to PHP and custom fields so what I'm trying to accomplish could possible be done using a different method.
I have a WPMU setup with all new sites created from a default blog that includes multiple custom post type templates. When users create a new site they are redirected to their site admin dashboard.
Here comes my challenge, I want users to be able to enter information into form fields contained in metabox(es) on their admin dashboard (not post editor) that will be used on the various custom post type templates. For example, a user will need to enter their company name, address, logo, etc.
Depending on the temple, the information entered into the form fields will be used on the frontend of the site. For example, the company logo will be display where the custom field or shortcode is placed on the post template.
I'm having issues using custom fields for this because each custom field is specific to each post. In addition, I'm having issues rendering a form on the admin dashboard for this.
I'm also thinking that custom fields may not be the best way to accomplish my objective because they are usually post specific. I'm really asking for a variable that will be used globally and is static unless the user updates the form.
I'd appreciate any assistance on this.
The options API is what you will need for this:
http://codex.wordpress.org/Options_API
There's also some plugins and frameworks you can use to help such as:
https://wordpress.org/plugins/options-framework/
https://wordpress.org/plugins/redux-framework/
I'm looking for a way to redirect to a specific wordpress page without knowing the ID.
I have a shortcode that is meant to redirect to a registration page once a button is clicked.
Before, I was able to just use a page ID. Now I am using WPML and the page ID's are all different for each language.
How can I redirect to a specific wordpress page without knowing the specific page ID for the current language?
Maybe you could use get_page_by_path() or get_page_by_title().
Both functions accept Post Type as argument, it's not only restricted to "pages". And they will return a full $post object, ie, $post->ID.
Maybe a WPML function is needed, an equivalent of those. Or maybe a combination.
See the docs for Language dependent IDs.
You could add a custom field which will be shared by all versions of the same article, and then query by that instead. Something like an article GUID.
I'm using Wordpress to build my website, i want to add a form that will have: name, description, image and location on a map. that is letting the users add their own marker on a map.
and by pressing 'submit' the whole data (name, description, image, longitude, latitude) would be saved to a database.
how can i do that? which plugins should i use?
You could use Google Placemarks and give them access to the creation of the 'Placemarks' custom post type that plugin utilises. You might have to hack the featured image..
Alternatively, you're looking at full custom post type creation and google maps API integration, a custom loop that utilises of the featured image.
Plugin that comes closest to what you need is what SMacFadyen suggested - BGMP. But if you want to do that yourself you would need to look into this:
Google Maps JavaScript API
WordPress Custom Post Types
WordPress Meta Boxes
For the first one there is lots of tutorials on how to display map and InfoWindows so you shouldn't have much problem with that. For Custom Post Types there is an awesome generator which should do 99% of your work. For Meta Boxes you can go several ways:
plugin - easiest but you would depend on a plugin which I don't like very much
from scratch - tutorial - which is a lot better than plugin but requires a lot of work
one of the meta box libraries - this is the way I usually go - I'm developing my own but it's not ready yet so in the mean time I would recommend My-Meta-Box or Reusable Custom WordPress Meta Boxes
Several times, when searching how to make this work, I stumbled upon this thread. Every time, I wasn't really happy with the way existing plugins deal with this. So, I now put this together myself.
Here's an overview of my solution.
Use the plugin Custom Post Type UI to create a custom post type for the markers on the map.
Use the plugin Advanced Custom Fields to add a field to this custom post type which represents the location. The field type is of type 'Google Map'.
Copy the standard Google Maps API clustering example to the appropriate theme file in your theme.
For showing your markers, generate the list of locations by, in your theme file, requesting the posts of your newly created custom post type.
For each marker/location, pick up the 'Google Map' field and abstract the latitude and longitude.
For each marker/location, construct the appropriate array element for the array with all locations, with, at least, the latitude and the longitude, and probably also the contents of an infowindow.
This should give you a map with all locations plotted out.
To allow users to add locations:
Add a listener to your map that listens for clicks and returns the location clicked. Perhaps this listener is only running after the user has expressed his intent to add a location to the map.
When the user has clicked, open a form, for example in a modal. The form should contain hidden fields with the latitude and longitude and could, for example, also contain a static image of the relevant location. Other form fields would be the fields you need for your purpose.
Submit the form using AJAX. Refer, for example, to this tutorial. In your theme's functions.php, you can then validate the submission and add the new submission to your Wordpress database, matching your new custom post type.
Depending on how you store your new location, on your next map reload, the new location will be included. (Or you perhaps need to authorise the new location, first.)
You can also reload all locations through AJAX.
Profile Builder allows you to do just that. Create a front-end register form including the required custom fields and a map field for users to pin or enter their location.
Then using the built-in User Listing (Member Directory) module, you can display a map of all users locations.