Add a page to wordpress - wordpress

I'm currently in the process of building my own WordPress plugin.So i need to create a new page(or post) that will be automatically added to word press when the plugin is activated.And this will be removed when plugin is deactivated.Content in the page is what content i am typing in the plugin.
HOW CAN I DO THAT?

you can use wp_insert_post function to create page or post check this http://codex.wordpress.org/Function_Reference/wp_insert_post
ex.
// 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
$post_id = wp_insert_post( $my_post);
you can use $post_id withing add_post_meta or update_post_meta or use post_meta varible to install and uninstall the page.

Something like this? There's some code referenced in the linked post for dealing with page creation. There's also the codex.

Related

Creating a multiple subpages in Wordpress with the same content and template as the parent

Is there a way to generate multiple new pages in wordpress with the same template, content and everything else except page name in Wordpress. The names of the new pages will be from a list of about 300 cities.
I found a plugin called BulkPress where i can insert the list and it generates the pages but with the default template and no content.
EDIT: Sorry i missed one important thing!
All the pages need to be subpages of one already made parent page and the template and content needs to be copied from that parent!
You could create this with programmatically. Put below code in your active template functions.php. It will insert 300 pages.
wp_insert_post( array $postarr, bool $wp_error = false, bool $fire_after_hooks = true )
e.g.,
foreach($i=1;$i<=300;$i++){
$my_post = array(
'post_title' => wp_strip_all_tags( "Your post Title" ),
'post_type' => 'page',
'post_content' => "Post Content Goes here",
'post_status' => 'publish',
'post_author' => 1,
'page_template' => 'template-blog.php',
'post_author' => get_user_by( 'id', 1 )->user_id,
);
// Insert the post into the database
wp_insert_post( $my_post );

Dynamically maintain virtual pages URL without create pages from admin

I created a page named UserName its URL is http://my_site/username/. On this page I am showing three links, and each link behaves as meta info for UserName.
Let's suppose:
If UserName contains info about User then the three links are:
About Me
Images
Videos
and links contains href like:
http://my_site/username/about_me
http://my_site/username/images
http://my_site/username/videos
now I create three general files like:
about_me.php
images.php
videos.php
and want to include these file by checking the URL, but I don't know how.
I did it without adding new page from wp-admin because there will be so many UserName pages but they all have same three links and will show the info about respective user.
And if I prefer to create About Me child page whose parent will be the UserName page then admin will need to create 3*(n UserName) pages where n least value is 100 and could be 1000s
But when I click any link WP says
Page Not Found
This is somewhat embarrassing, isn’t it?
I select Custom Structure from Settings and I have no more idea about WP permalinks.
You may call I need to create Virtual pages for all users.
If it is not possible then is it possible that while adding new UserName page then on published three pages (About Me, Images & Videos) will automatically with parent page newly UserName page and with a defined Page Template. If it is possible then how?
Wow I got an idea, implement & hurray it worked.
In wp-admin/includes/post.php I add my script in function edit_post( $post_data = null )
First I checked if post not already exists then run my script which is:
$post = array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => $current_user_id,
'post_date' => date('Y-m-d H:i:s'),
'post_date_gmt' => date('Y-m-d H:i:s'),
'post_name' => 'Image',
'post_parent' => $post_parent,
'post_status' => 'publish',
'post_title' => 'Image',
'post_type' => 'page'
);
// Insert in to WP wp_posts table
$this_post_id = wp_insert_post( $post, $wp_error );
// Insert in to wp_postmeta table
$meta_id = update_post_meta($this_post_id , '_wp_page_template', 'page-three-columns.php');

how to add new pages using WP core function

how can I add a page using core functions of wordpress.I can find function for Post etc as well but couldn't find any for Pages.Any one who can help me
Thanks
You use the same function to add posts or pages, just add the post_type parameter like this :
$args = array( 'post_type' => 'page',
'post_content' => 'You content...',
'post_parent' => $parent_id; // ID of the page this one should be a child of
... // etc.
...
'post_title' => 'Title for your page');
wp_insert_post ($args);

How to prevent wp_insert_post() to set Uncategorized category?

I'm using Wordpress 3.5 and it seems that wp_insert_post() cannot set categories anymore, the documentation syas :
post_category no longer exists, try wp_set_post_terms() for setting a
post's categories
The problem is that wp_set_post_terms() or wp_set_object_terms() require the postID, which is returned by wp_insert_post(). While this is fine to set category terms to the post inserted by wp_insert_post(), the problem is that every time I call wp_insert_post() I get the Uncategorized category in my post, in addition to the category terms I set after calling wp_insert_post(). How can I prevent the Uncategorized to be always there?
I don't know where have you found that wp_insert_post() can't set categories anymore but from the WordPress Doc you can do it like
// 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) // id's of categories
);
// Insert the post into the database
wp_insert_post( $my_post );
Bellow is an working example of mine that I'm using in one my site to add new post dynamically by an admin with a category name of location with two meta fields, input taken from the user (I've filtered user inputs but omitted here)
$category='location'; // category name for the post
$cat_ID = get_cat_ID( $category ); // need the id of 'location' category
//If it doesn't exist create new 'location' category
if($cat_ID == 0) {
$cat_name = array('cat_name' => $category);
wp_insert_category($cat_name); // add new category
}
//Get ID of category again incase a new one has been created
$new_cat_ID = get_cat_ID($category);
$my_post = array(
'post_title' => $_POST['location_name'],
'post_content' => $_POST['location_content'],
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array($new_cat_ID)
);
// Insert a new post
$newpost_id=wp_insert_post($my_post);
// if post has been inserted then add post meta
if($newpost_id!=0)
{
// I've checked whether the email and phone fields are empty or not
// add both meta
add_post_meta($newpost_id, 'email', $_POST['email']);
add_post_meta($newpost_id, 'phone', $_POST['phone']);
}
Also remember, every time you add a new post without a category, WordPress sets the default category for that post and it's uncategorized if you did not change it from the admin panel, you can change the default category from uncategorized to anything you want.
Update:
Since the post_category is no more exists so you can replace
'post_category' => array($new_cat_ID)
with following
'tax_input' => array( 'category' => $new_cat_ID )
in the example given above. You can also use
$newpost_id=wp_insert_post($my_post);
wp_set_post_terms( $newpost_id, array($new_cat_ID), 'category' );
Remember that, in this example, the $new_cat_ID has been found using following line of code
$new_cat_ID = get_cat_ID($category);
but it's also possible to get the category id using following code
$category_name='location';
$term=get_term_by('name', $category_name, 'category');
$cat_ID = $term->term_id;
Read more about get_term_by function.

Creating a post using PHP in one of the blogs in WP MU

I have a PHP program to create a site/blog in a networking enabled WordPress. After the site creation, I want to create a post using the same PHP program.
If I use wp_insert_post() function, it's creating the post in the main site/blog, not in the new site/blog I created. I also tried using switch_to_blog() before calling the wp_insert_post() but no luck.
I got the answer for this... The culprit is $blog_id, the moment I changed the variable name to $new_blog_id, it started working. Thanks
The following used as Must Use plugin does the job:
<?php
/* Plugin Name: New Post on Site Creation */
add_action( 'wpmu_new_blog', 'default_post_so_5334372', 10, 6 );
function default_post_so_5334372( $blog_id, $user_id, $domain, $path, $site_id, $meta )
{
switch_to_blog( $blog_id );
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1
);
wp_insert_post( $my_post );
restore_current_blog();
}

Resources