Updating published post/page in wordpress - wordpress

I have a plugin which should update post_title and post_content of a post. Because of layout problems or other security stuff, I would love to create a new revision for the post and guide the user to the wordpress post details where it can be reviewed. After checking the changes, the user should be able to set this changes live. If the post is still in draft, it is not needed.
I am using wp_update_post() to update post_title and post_content. I also tried to set status to "draft" but then the published content is not online anymore.
Any ideas how to do it?

You can use wp_insert_post() instead.
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_type' => 'revision',
'post_status' => 'inherit',
'post_parent' => $YOUR_POST_ID,
//'post_name' => $YOUR_POST_ID.'-autosave-'.uniqid()
);
wp_insert_post( $my_post );

Related

How to add post slug with Corcel for Laravel to Wordpress

I using corcel for laravel.
$item = \Corcel\Model\Post::create(
[
'post_title' => $product['name'],
'post_name' => Str::slug($product['name']),
'guid' => 'http://domain/product/' . Str::slug($product['name']) . '/',
'post_content' => $product['description'],
'post_date' => date('Y-m-d H:i:s'),
'post_type' => 'product',
]
);
But slug and guid don't filled. What am I doing wrong?
You're using Corcel wrong.
There are a lot of hooks called and other processes that are performed by WordPress before and after creating content, which aren't available when using Corcel. Corcel is read-only. If you wish to create content, I would recommend talking directly to the WordPress site using the REST API endpoints such as this one.

Creating pages when activating plugin WordPress

Anyone Please help!
I have a plugin which creates post_type pages in the backend. The plugin is creating the desired pages but the problem is whenever i try to see the page list, it shows "No pages found" message. Screenshot here: http://prnt.sc/azalub
My code for creating the required pages here:
$new_page = array('post_title' => $title,
'post_content' => '['.$shortcode.']',
'post_status' => 'publish',
'post_type' => 'page'
);
$post_id = wp_insert_post( $new_page );
For this purpose, you need to register with plugin activation hook.
See the code example below:
function add_my_custom_page() {
// Create post object
$my_post = array(
'post_title' => wp_strip_all_tags( 'My Custom Page' ),
'post_content' => 'My custom page content',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page',
);
// Insert the post into the database
wp_insert_post( $my_post );
}
register_activation_hook(__FILE__, 'add_my_custom_page');
While creating a custom post type, i had set 'query_var' to 'true' on one of the custom post type in my plugin. Setting it 'false' just made everything fine.
if you are using class try pass array($this, 'method_name') instead of function name.

Automatically send to "Post" or "Page" Wordpress plugin Contact Form 7

I want that submission from CF7 automatically gets posted to "Post" or a "Page" on my site.
I saw that it is necessary to add some action hook but I do not have the skills to do that? Can someone help me out please?
Thank you!
edit: I could do it with Blog_by_Email service (which sends submission to some email and then make a post automatically), but I need user id on the post.
You can use the wp_insert_post to do this
//sample code
if(isset($_POST['submit_button'])){
$my_post = array(
'post_title' => $_POST['title'],
'post_date' => date("Y-m-d H:i:s"),
'post_content' => $_POST['description'],
'post_status' => 'pending',
'post_type' => 'post',
'post_author' => $user_id
);
$the_post_id = wp_insert_post( $my_post );
}
for more info about wp_insert_post visit this page http://codex.wordpress.org/Function_Reference/wp_insert_post

wordpress add page

i am writing plugin for wordpress and in this plugin admin should add some information like this ===> title,text in database.
I want that this information to be added as wordpress page in database.
What function should i use to make this task?
You can use wp_insert_post for that http://codex.wordpress.org/Function_Reference/wp_insert_post
Pages are just a type of posts in WP.
$post = array(
'post_title' => 'my Title',
'post_content' => 'my Text',
'post_type' => 'page',
'post_status' => 'publish',
'post_author' => 1
);
wp_insert_post( $post );

Adding pages/posts after plugin installation

There are a few plugins that I know that after installation they will create pages or posts. How do you do this?
Tried to research but could not find helpful guide.
The wp_insert_post reference on the WordPress Codex might help.
Here's an example code of how to create a new post programmatically:
// Create post object
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
// Insert the post into the database
wp_insert_post( $my_post );
To run this code automatically when your plugin is activated, you can wrap this code in the register_activation_hook hook.

Resources