Adding pages/posts after plugin installation - wordpress

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.

Related

Updating published post/page in 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 );

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.

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 );

Using wp_insert_post() for custom post type posts are saved but uneditable

Hello I have made a PHP script for wordpress that insert new posts using a custom type via wp_insert_post.
Using the code above I can get the post saved but i can't edit it using the admin panel. I'm wondering why? can anybody help me?
$post = array(
'comment_status' => 'closed',
'post_author' => 1,
'post_date' => date('Y-m-d H:i:s'),
'post_content' => $postdata['name'],
'post_status' => 'publish',
'post_title' => strtoupper(strip_tags($postdata['name'])),
'post_type' => 'xCustom' // custom type
);
wp_insert_post($post);
Thank you
Wordpress converts all custom post type slugs to lowercase, so instead of using:
'post_type' => 'xCustom'
You should be using:
'post_type' => 'xcustom'
I just spent 3 hours troubleshooting this on my own site.

Getting wp_list_categories() custom walker to paginate

I've got a custom post type registered, with custom taxonomies, everything good and clear.
I wish I could somehow display all the categories of the taxonomy with pagination.
I am using a custom category Walker and am thinking to register a custom page rewrite for page query, and add some code to the category walker to display only the desired interval. Am I on the right direction?
Also, wp_list_categories sends to the category Walker the entire list of categories. Is there any way to get only the desired interval?
In this particular setup, no.
But I found a workaround: I registered a function that created a custom-type post each time I added a category in the taxonomy. That way I used the archive feature for custom post types, made available in 3.1-RC1.
function create_crew_post_on_term($term_id) {
$term = get_term($term_id, 'crew');
$post = array(
'comment_status' => 'open',
'ping_status' => 'open',
'post_author' => 1,
'post_content' => '',
'post_date' => date('Y-m-d H:i:s'),
'post_excerpt' => '',
'post_name' => $term->slug,
'post_status' => 'publish',
'post_title' => $term->name,
'post_type' => 'crew'
);
wp_insert_post( $post );
}
add_action('created_crew', 'create_crew_post_on_term');

Resources