I need to create child pages for all pages (which do not have child pages). It shoud be one time script/function. I tried to do smth like that
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
wp_insert_post( $my_post );
But dont know that to do else.
You need save_post action, here you can find example
http://pastebin.com/CvryjJCV
Related
I've created a plugin for wordpress that can auto post pages on demand
my problem is formatting this pages to a specific template
I need that when the post page is auto inserted in the DB, set the Sidebar position to disabled in order to have a full width page and hide the page title... this options appear in the dashboard and I can click on them one by one, but that has to be automatic, not manually.
$my_post = array(
'post_title' => wp_strip_all_tags( $tituloFichaP1 ),
'post_content' => $content,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 ),
'post_type' => 'page',
'post_parent' => $parentPost,
'page_template' => 'microsite',
'comment_status' => 'open',
);
wp_insert_post( $my_post );
By the way, the page_template attribute ('page_template' => 'microsite') doesn't work either. Te post is inserted but the template is set to default.
Thanks in advance!!!
I just go through you code, you have done a simple mistake, in "page_template" that should be a file name (filename.php) of your template not a template name . for ex. if your template microsite's file name is microsite.php than your code will be.
$my_post = array(
'post_title' => wp_strip_all_tags( $tituloFichaP1 ),
'post_content' => $content,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 ),
'post_type' => 'page',
'post_parent' => $parentPost,
'page_template' => 'microsite.php',
'comment_status' => 'open',
);
wp_insert_post( $my_post );
Hope this will help you.
I have created a plugin that enables a Custom Post Type and I have inserted some default values for this Post Type with wp_insert_post.
wp_insert_post( array(
'ID' => '3',
'post_status' => 'publish',
'post_type' => 'exhibitor',
'post_title' => 'Title',
'post_content' => 'Description...'
) );
The problem is my values are reinserted everytime the page refreshes. I cannot edit or remove them.
How do I get Wordpress to only update a Custom Post Type when my plugin is activated? Everytime I refresh a page the posts are published again. So I cannot edit or delete the posts.
Regards,
In your plugin use register_activation_hook, that's plugin function to be run when the plugin is activated.
register_activation_hook(__FILE__, 'newplugin_install');
function newplugin_install() {
wp_insert_post( array(
'ID' => '3',
'post_status' => 'publish',
'post_type' => 'exhibitor',
'post_title' => 'Title',
'post_content' => 'Description...'
) );
}
hope this will work for you ;)
I have a created a wordpress plugin that needs to create a page dynamically.
When the plugin is activated a page is created COOKIE_POLICY.
From my reading I found that inserting into the DB is the best way. And below is the way to do it. However, when I activate the plugin, there is no page or post created.
I go this from:
http://codex.wordpress.org/Function_Reference/wp_insert_post
function create_policy() {
$my_post = array(
'post_title' => wp_strip_all_tags( $_POST['COOKIE_POLICY'] ),
'post_content' => $_POST['Here is all our cookie policy info'],
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,30 )
);
// Insert the post into the database
wp_insert_post( $my_post );
}
register_activation_hook( __FILE__, 'create_policy' );
This code is nearly correct. Below is the fixed code
function create_policy() {
$my_post = array(
'post_title' => 'cookiepolicy',
'post_content' => 'this is my content',
'post_type' => 'page',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 3,4 )
);
// Insert the post into the database
wp_insert_post( $my_post );
}
register_activation_hook( __FILE__, 'create_policy' );
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 );
I create lots of blogs that all share the same basic structure of a single "post" and 3 "pages" (about us, contact us, privacy). For the "post" page, I usually just edit the existing "hello world" post once I'm in the newly created blog. For the pages, I start with the "about" page and just edit that for my "about us" page.
I'd like to create a script or plug-in that I can simply add to my site that will, when executed, automatically create those pages with my boilerplate content. For the "about us" page, I can just edit the default "about page" via script. And for the initial "post" the script can just edit the "hello world" post. The other "pages" will need to be created from scratch via the script.
I'm looking for ideas on how best to do it (a plug-in or a script thats uploaded and executed) along with some help with the wordpress API calls used to create and update a post and a page's name and content.
For the default content, the about and contact content will be very short but I will need to use an external file to populate the privacy page. I'm thinking that a preformatted .html file should be used as the basis of this database insert.
Thanks for your help!
If you set up the blogs from scratch each time, you could manipulate /wp-admin/includes/upgrade.php (function wp_install_defaults()) and put your pages there.
That is, however, if you are comfortable with hacking WP core files.
Another possibility is to do it after installation with a custom SQL script. Just use phpMyAdmin to look what a current page looks like and create your own "INSERT INTO" statement. For convenience, you could write a small WP plugin, that does the insert on activation and afterwards removes itself, or something like this.
Edit: From looking at the above mentioned file, you can do everything from within PHP with this:
$wpdb->insert( $wpdb->posts, array(
'post_author' => $user_id,
'post_date' => $now,
'post_date_gmt' => $now_gmt,
'post_content' => __('Lorem ipsum...'),
'post_excerpt' => '',
'post_title' => __('About'),
/* translators: Default page slug */
'post_name' => _x('about', 'Default page slug'),
'post_modified' => $now,
'post_modified_gmt' => $now_gmt,
'guid' => $first_post_guid,
'post_type' => 'page',
'to_ping' => '',
'pinged' => '',
'post_content_filtered' => ''
));
You could put it in a plugin you just upload, activate and delete.
Here's what I use, modified for your use (havent tested it):
<?php
/*
Plugin Name: Startup Settings
Plugin URI: http://someurl/
Description: Some description
Version: 1
Author: Your Name
Author URI: http://yoursite.com
*/
function startup_settings()
{
// Remove "Hello world" post and comment.
wp_delete_post(1, TRUE);
wp_delete_comment(1);
// Insert your pages.
global $user_ID;
$about_page = array(
'post_title' => 'About us',
'post_content' => 'Your content goes here',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'page',
'post_category' => array(0)
);
$post_id = wp_insert_post($about_page);
$contact_page = array(
'post_title' => 'Contact us',
'post_content' => 'Your content goes here',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'page',
'post_category' => array(0)
);
$post_id = wp_insert_post($contact_page);
// if you want to load the privacy text from a external file (untested)
$myFile = "/path/to/privacy.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
$privacy_page = array(
'post_title' => 'Privacy',
'post_content' => $theData,
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'page',
'post_category' => array(0)
);
$post_id = wp_insert_post($privacy_page);
// Set some deafault options if you want.
$options = array(
'comment_max_links' => 0,
'comments_per_page' => 0,
'date_format' => 'd.m.Y',
'default_ping_status' => 'closed',
'links_updated_date_format' => 'l, F j, Y',
'permalink_structure' => '/%postname%/',
'rss_language' => 'en',
'use_smilies' => 0
);
foreach ( $options as $option => $value )
{
update_option($option, $value);
}
return;
}
register_activation_hook(__FILE__, 'startup_settings');
?>