Create product with external attachment URL using REST API - wordpress

I am following documentation for create a product, https://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-product .
I want to create a product with the images which are saved in an external server.
when I use the following code the API uploading the src image to the current server,
'images' => [
[
'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg'
],
]
But I don't want to upload those images to the current server.
If I explain it in WordPress code without API,
1. I need to create parent post ( post type - post )
2. create another post with post type "attachment", content as external URL and parent post as above post's ID
But is there any chance to use the above woocommerce API for this requirement?

Related

How to Send a notification when a post is updated on Wordpress Gutenberg

I want to create a function that connect my Wordpress site to an external API when a post is updated. I aready did the method by using the post_updated Hook, but I would like to send a notification to the WP editor. Since on Gutemberg the post requests are made using AJAX, I'm not sure how to display notification without refreshing the page. Do you know if there is a Hook to send these notifications? Thanks in advance
I spect to send a notification to Wordpress Editor
In the block Editor, the Notices Data API is used to create custom Notifications with JS/AJAX for events like when a post is published, display errors or warnings. Below is the example usage given in the WordPress Gutenberg Notifications guide:
( function ( wp ) {
wp.data.dispatch( 'core/notices' ).createNotice(
'success', // Can be one of: success, info, warning, error.
'Post published.', // Text string to display.
{
isDismissible: true, // Whether the user can dismiss the notice.
// Any actions the user can perform.
actions: [
{
url: '#',
label: 'View post',
},
],
}
);
Depending on what you need notices for, withNotices (a Higher Order Component) could be of interest to you. There is also the ability to lock/prevent post saving which can be used in conjunction with your notice/s.

WordPress Gutenberg, update post content programmatically

I have been starting to testing out Gutenberg editor with both ACF and custom blocks. And I have been looking around to solve my problem but I couldn't find anything about this topic (Maybe my google skills is not good enought)
But my case is this:
I have a custom post type where I want to set a template so they can't move around the blocks and add other blocks and so on. And on this post type around 70% is created by code. Because it is fetching all the information from an API.
Test 1: I have created an ACF block with all the fields I need, and it is working as it should when I create a new post from WP admin. But when I run the update_field function it is saving it to post_meta table as it did before. So my question here is how do I update a field so it saves it to post_content and not to post_meta table.
Test 2: I created custom blocks for all of the fields (convert each ACF field to and block) and set up the template to use these blocks only.
But here I have no idea how update update post_content with PHP or Javascript.
I hope you can help me out with this :) If anything is unclear tell, and I will try to explain it
ACF has an ability to pre-init fields before post will be visible to user on post creation page. You can try to use this function to set desired content to fields.
You can read about this here:
https://www.advancedcustomfields.com/resources/acf-prepare_field/
As of Wordpress 5.0.0
You can use template and template_lock arguments upon registering your custom post type.
Source # https://developer.wordpress.org/reference/functions/register_post_type/#changelog
You can then set an array of specific blocks to use and you can chose to restrict users from adding new blocks or removing them.
Attribute
Description
template
(array) Array of blocks to use as the default initial state for an editor session. Each item should be an array containing block name and optional attributes.
template_lock
(string/false) Whether the block template should be locked if $template is set. If set to 'all', the user is unable to insert new blocks, move existing blocks and delete blocks. If set to 'insert', the user is able to move existing blocks but is unable to insert new blocks and delete blocks. Default false.
A short example would be something along the lines of...
<?php
$args = [
//...
'template_lock' => 'all',
'template' => [
[ 'core/paragraph' ],
[ 'core/file' ],
//...
],
//...
];
register_post_type( $post_type, $args );
?>
Currently, Gutenberg documentation is scarced, you can find a complete list of blocks & parameters # https://github.com/WordPress/gutenberg/tree/master/packages/block-library/src
Gutenberg is still in development, some features don't act as they should.

Show different URL in address bar

I have a custom post with post-type "podcast" and now my podcast post single URL is http://localhost/sitename/blog/podcast/post-name. But I want to show post-type "podcast" URL like: http://localhost/sitename/test/podcast/post-name.
Currently site permalink is set to custom structure with /blog/%postname%. So blog is showing with each custom post type. And I want to keep permalink structure same but blog will not show in "podcast" post URL.
I have already created a new file for single podcast with copied single.php and changed name to single-podcast.php. It's working fine with http://localhost/xyz/blog/podcast/post-name but I want to show different name in URL.
Can you please suggest a solution for this?
Have you tried rewrite options?
https://codex.wordpress.org/Function_Reference/register_post_type#Parameters
Example:
[
'rewrite' => [
'slug' => 'your/custom/post/slug/here',
'with_front' => false,
],
]

Wordpress post to user relation ship

Is there any way to connect post to specific user in wordpress. Is there any plugin available. Or any one know the code for doing that.I have a custom post type stories. When adding stories i need to chose the corresponding users from user list. Please help
Install Post2post https://wordpress.org/plugins/posts-to-posts/
Then in your function.php write this code
p2p_register_connection_type ( array(
'name' => 'releated_user',
'from' => 'story',
'to' => 'user'
) );
here story is your custom post type slug. related_user is just a connection name.You can name it as what you like.
Then in your post type section you can see an option for selecting corresponding user.
In the custom post type setup, you first need to ensure that this post type allows for authors to be set.
This comes in from the arguments when registering the post type.
$args = array('supports'=>array('author'=>true));
If you have an admin account, you can set who the author is using the quick edit function or by allowing to see it under screen options on the full edit page.
Other than that, you can make your own complete post meta box to allow for multiple authors. I cant see a plugin on the wordpress plugin directory so creating your own to fit your needs and wants will be your best bet.

Custom Pods.io detail page

I am attempting to create a custom template page for a details page for my Pods.io 'research_faculty' pod.
I have the url going currently to /people/details and it does go to that page but I created a 'details' page that I have assigned a custom template to display the information that I want. Problem I am having is that its treating the last part of the URL as a page and Wordpress is saying page cannot be found. So it would be /people/details/wally-kolcz. How can I use the past part of the URL as the way to pull the person's details and populate the template page rather than WordPress trying to use it as another page and defaulting to the single.php template?
Pod (Advanced Options)
Custom Rewrite Slug: people/details
You can use pods_v( 'last, 'url' ); to get the last segment of the url. If you put that in a variable you can use it to build your Pods object.
// get current item
$slug = pods_v( 'last', 'url' );
// get pods object for current item
$pods = pods( 'pod_name', $slug );
See this tutorial for more information: http://pods.io/tutorials/using-pods-pages-advanced-content-types/
Um create a pods page and on the Enter page URL section enter /people/details/* so that it responds to /people/details/+ anything. Also in the slug section u can try putting {#url,2} If it doesnt work. Also go to the advanced options of your advanced content type and in the detailed page URL enter pod_name/{#permalink} though that may not be necessary but try that and test it again.Make sure the pods page also has a pod to refer to and that it points at your php details template

Resources