Wordpress Plugin Add New Page - wordpress

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

Related

How to add unfiltered_html capability

When posting html content below it posts fine with the admin, but any other user it strips all HTML tags
$my_post = array('post_title' => $title,
'post_content' => $content,
'post_category' => array(520),
'post_status' => 'Publish',
'post_name' => $url
);
$result = wp_insert_post( $my_post );
Is there a way to allow any user to post HTML tags.
I found that if I use this before the code, it works.
$sub_role = get_role( 'subscriber' );
$sub_role->add_cap( 'unfiltered_html', true );

Wordpress - Delete custom-post-type post

I am trying to add a button in wordpress that deletes a custom-post-type post with a specific title and current user as author.
Problem that occurring is that all the posts gets deleted, all the job_alert posts, not only for this specific author or with this title.
Can someone see why?
$delete_post = array(
'post_type' => 'job_alert',
'post_title' => $title,
'post_status' => 'publish',
'post_author' => $current_user->ID
);
$posts = new WP_Query( $delete_post );
if ( $posts->have_posts() ) {
while ( $posts->have_posts() ) {
$posts->the_post();
wp_delete_post( get_the_ID());
}
}
I also have this code that creates a post and that works great. Similar code.
$new_post = array(
'post_type' => 'job_alert',
'post_title' => $title,
'post_status' => 'publish',
'post_author' => $current_user->ID
);
$post_id = wp_insert_post( $new_post );
For deleting the particular post, here is the solution,
Please use name parameter instead of post_title in your query, then only it will return the required post which you want.
I have modified your code. Please find the updated code below:
$delete_post = array(
'post_type' => 'job_alert',
'name' => $title,
'post_status' => 'publish',
'post_author' => $current_user->ID
);
Now, your required post will be returned.
Hope, this may be helpful to you and let me know, if you have any query.
Thanks.

Set sidebar position disabled and hide page title through php wordpress plugin

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.

How to create post automatic after clicking chechout page in woocommerce?

whenever someone purchases the product test, a new post is created and saved under the category with the post meta being set with key "order_id" being set to the order_id of the order.
add_action( 'woocommerce_after_checkout_validation', 'test3' );
function test3(){
global $woocommerce;
print_r($woocommerce);
$my_post = array(
'post_title' => 'Test',
'post_content' => 'This is test post',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 )
);
// Insert the post into the database
wp_insert_post( $my_post );
}
I think you want to create a new post with postmeta 'order_id' => 45 after checkout completion? if its so, my code will help you.
add_action('woocommerce_new_order', 'order_check', 10, 1);
function order_check($order_id) {
$my_post = array(
'post_title' => 'Test',
'post_content' => 'This is test 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);
add_post_meta($post_id, 'order_id', $order_id);
}

Insert wordpress post via php fatal error

When I want to use wp_insert_post( $my_post ); function i get the following error:
Fatal error: Cannot redeclare create_initial_post_types() (previously
declared in
/home/izradawe/public_html/mydomain.com/wp-includes/post.php:20) in
/home/izradawe/public_html/mydomain.com/wp-includes/post.php on line
152
Can you help me with this?
Code which I using:
include_once('../wp-load.php');
include("../wp-includes/post.php");
// Creating post
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => 5,
'post_type' => 'post'
);
// Insert the post into the database
wp_insert_post( $my_post );
In order to gain access to Wordpress' main functions, try:
<?php
include '../wp-blog-header.php';
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => 5,
'post_type' => 'post'
);
wp_insert_post( $my_post );
?>
If you writing the code inside single.php or index.php or any template file inside template directory no need to include the file wp-load.php or post.php
Change include into include_once.

Resources