wordpress add page - wordpress

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

Related

Wordpress Woocommerce. I have products that are in promotions. Wanna add a sticker "Promo" for every one product i create with php code

Here is part of code i use for submitting products. With every product i add wanna enable a sticker on product area. I can do it manually from Wordpress edit product but not from here.
$post = array(
'post_author' => $user_id,
'post_content' => $returnContentDesc,
'post_status' => 'publish',
'post_title' => $productName, //$product->part_num,
//$product->is_on_sale()
//'post_parent' => $productClass,
'post_excerpt' => "$productAvail<br`><b>Гаранция:</b> $transTextWarranty",
//'post' => '',
'orderby' => 'menu_order',
'hierarchical' => '1',
'post_type' => 'product'
);
$post_id = wp_insert_post( $post, $wp_error ); //Create post - product based on array
I found some solution, setting a sale price manually sets the sticker "Promotion". Will try to test it with the php script. Flatsome theme has "Extra" options then Custom Bubble which the same sticker manually

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.

wp_insert_post Only Once for Custom Post Type

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

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.

How to Set POST permalink/slug in wordpress using wp_insert_post

I m writing simple script using wp_insert_post() to post article in blog.
but one problem here, I want to make Title and slug of an URL different.
How to achieve this?
for example:
Title: How to make your diet success
Slug: 7-ways-to-make-succes-Diet
post_title sets the title, and post_name sets the slug.
So:
// Create post object
$my_post = array(
'post_title' => 'How to make your diet success',
'post_name' => '7-ways-to-make-succes-Diet',
'post_content' => 'my content',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
// Insert the post into the database
wp_insert_post( $my_post );

Resources