Delete Page after plugin deactivation code not working - php

I am trying to delete page after plugin deactivate.The code is not working for delete page.
register_activation_hook( __FILE__, 'insert_page' );
function insert_page(){
// Create post object
$my_post = array(
'post_title' => 'Menu',
'post_content' => 'Short Code',
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'post_type' => 'page',
);
// Insert the post into the database
wp_insert_post( $my_post, '' );
$newvalue = wp_insert_post( $post, false );
update_option( 'Menu', $newvalue );
}
register_deactivation_hook( __FILE__, 'deactivate_plugin' );
function deactivate_plugin() {
$page = get_page_by_title('Menu');
wp_delete_post($page);
}
I have even tried wp_delete_post($page, true);. It is also not working. Help me where i am wrong.

I'm going to address a few things if you don't mind:
First of all, you should work on your code indentation/formatting. Future you thanks you!
Second of all you're calling the wp_insert_post() function twice, but it's only working once. You can also check to see if wp_insert_post() returns a truthy value before calling update_option().
Third, be careful with "common" function names, as you risk having redeclaration errors! Try and use namespaces, classes or at the very least prefix your functions names to avoid future headaches.
Fourth, you don't need to set the $error parameter to false on wp_insert_post() as that is its default value.
Lastly, to answer your question (finally!) it's not working because wp_delete_post() requires a Post ID and you're passing a Post Object. Instead you'll want to use $page->ID. Here's your code with the changes I outlined above:
register_activation_hook( __FILE__, 'so_50960355_insert_page' );
function so_50960355_insert_page(){
// Define my page arguments
$page = array(
'post_title' => 'Menu',
'post_content' => 'Short Code',
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'post_type' => 'page',
);
if( $page_id = wp_insert_post( $page ) ){
// Only update this option if `wp_insert_post()` was successful
update_option( 'my_menu_page_id', $page_id );
}
}
register_deactivation_hook( __FILE__, 'so_50960355_delete_page' );
function so_50960355_delete_page(){
$page_id = intval( get_option( 'my_menu_page_id' ) );
// Force delete this so the Title/slug "Menu" can be used again.
wp_delete_post( $page_id, true );
}

Related

Create custom wordpress page after install my custom plugin

How to create custom wordpress page after install my custom integration wordpress plugin.
Ex : (If we have install Woocommerce it's automatically create the cart & checkout & myaccount page). I need like some format.
You can create new page on plugin activation hook in your plugin
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');
try this to your plugin
It will check that a page with the same name does not exist.
register_activation_hook( __FILE__, 'custom_plugin_activation' );
function custom_plugin_activation() {
if ( ! current_user_can( 'activate_plugins' ) ) return;
global $wpdb;
if ( null === $wpdb->get_row( "SELECT post_name FROM {$wpdb->prefix}posts WHERE post_name = 'new-page-slug'", 'ARRAY_A' ) ) {
$current_user = wp_get_current_user();
// create post object
$page = array(
'post_title' => __( 'New Page' ),
'post_status' => 'publish',
'post_author' => $current_user->ID,
'post_type' => 'page',
);
// insert the post into the database
wp_insert_post( $page );
}
}
Here is a full list of parameters accepted by the wp_insert_post function.

wp_insert_post() Returns Action failed. Please refresh the page and retry

I am trying to create a new Wordpress Post using wp_insert_post() within a Wordpress Plug-in for a AJAX call. I have inspected that the related parametrs are pasing to the function but as my response I am getting Action failed. Please refresh the page and retry and this is upon wp_insert_post().
EDIT: I have checked the database and all the data stored properly in the database
Also I tried the wp_send_json( get_post(27538) ); die; and it works fine within the same function.
add_action( 'wp_ajax_tcf_et_create_new_estimate', 'tcf_et_create_new_estimate' );
function tcf_et_create_new_estimate()
{
$project_name = $_REQUEST['project_name'];
$email_address = $_REQUEST['email_address'];
$ikea_system = $_REQUEST['ikea_system'];
$project_notes = $_REQUEST['project_notes'];
$user = get_user_by( 'email', $email_address );
$user_id = is_int( $user->ID ) ? $user->ID : 1;
$wishlist_data = array(
'post_type' => 'wishlist',
'post_title' => sanitize_text_field( $project_name ),
'post_content' => $project_notes,
'post_status' => 'publish',
'ping_status' => 'closed',
'post_excerpt' => '',
'post_author' => $user_id
);
// die('here'); --> THIS CODE WORKS AND IT RETUNS "here" AS RESPONCE IF UNCOMMENTED
$webhook_id = wp_insert_post( $wishlist_data );
wp_send_json( $webhook_id );
die;
}
Is there anything to pass for the wp_insert_post() than above code?
The reason was I am trying to add 'post_type' => 'wishlist' before the wishlist is registered.

functions.php with wp_redirect($url); exit(); makes wordpress site blank

I'm creating a form for the users to submit posts from the front end.
Once the form is submitted the users should be redirected to the post they have just created.
I have this code in my functions.php. However it makes my website blank...
I think it's related to the exit() line, I've tried to modify it but it doesn't work, nothing happens at all. It just shows a white page.
<?php
wp_register_script( 'validation', 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js', array( 'jquery' ) );
wp_enqueue_script( 'validation' );
$post_information = array(
'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
'post_content' => $_POST['postContent'],
'post_type' => 'post',
'post_status' => 'publish'
);
$post_id = wp_insert_post($post_information);
$url = get_permalink( $post_id );
wp_redirect($url);
exit();
?>
Do you have any ideas? How can I fix that? Thanks!
OK, it won't work like that.
First of all, you shouldn't add scripts like that when the functions.php is loaded (because it's loaded much too early, before WP has actually decided what to do with the request that comes from the browser) - use the wp_enqueue_scripts for that:
<?php
function add_my_scripts() {
wp_register_script( 'validation', 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js', array( 'jquery' ) );
wp_enqueue_script( 'validation' );
}
add_action( 'wp_enqueue_scripts', "add_my_scripts");
?>
Your creation of a new post runs on every request - even for that request when your browser wants to show that new post.
Depending on what exactly you need, you might want to put this into an action hook as well, but it should help already if you check that it was actually a POST request that contains a postTitle, something like this:
<?php
if( $_SERVER["REQUEST_METHOD"] == "POST" && array_key_exists("postTitle", $_POST)) {
$post_information = array(
'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
'post_content' => $_POST['postContent'],
'post_type' => 'post',
'post_status' => 'publish'
);
$post_id = wp_insert_post($post_information);
if(is_wp_error($post_id)) {
print "An error occured :(\n";
var_export($post_id);
}
else {
$url = get_permalink( $post_id );
wp_redirect($url);
}
exit();
}
?>

Wordpress Plugin - Delete a Page Created when deactivating

I tried to make a custom post type plugin for my own use, and managed to make a function that creates the page for it so far. What I want to do is to delete the said page when the plugin is activated. How should the code be?
This is my code for creating the said page upon plugin activation:
function create_video_pages() {
$post = array(
'comment_status' => 'open',
'ping_status' => 'closed' ,
'post_date' => date('Y-m-d H:i:s'),
'post_name' => 'videos',
'post_status' => 'publish' ,
'post_title' => 'Videos',
'post_type' => 'page',
);
$newvalue = wp_insert_post( $post, false );
update_option( 'vidpage', $newvalue );
}
Get the post_id from your vidpage option.
Then use it to delete that post.
function deactivate_plugin() {
$page_id = get_option('vidpage');
wp_delete_post($page_id);
}
register_deactivation_hook( __FILE__, 'deactivate_plugin' );
You can do this using register_deactivation_hook and function wp_delete_post which deletes post with everything that is tied to it.
What about this?
function on_deactivating_your_plugin() {
$page = get_page_by_path( 'about' );
wp_delete_post($page->ID);
}
register_deactivation_hook( __FILE__, 'on_deactivating_your_plugin' );

Wordpress and wp_insert_post not working

i'm doing an webapp that works with a wordpress website.
Until yesterday all works like a charm, but from today, when i save the post on wordpress db, it now not working...
Not give me error (php error or wordpress error), but simply a white page.
This is my full code:(sorry for many commented lines and echo, but i'm testing)
The important part:
// Create post object
$my_post = array(
'post_title' => $article_title,
'post_name' => $article_title,
'post_content' => $article_content,
'post_status' => 'pending',
'post_author' => 1
);
// Insert the post into the database
wp_insert_post($my_post, TRUE);// try first time
$post_status = "pending";
$post_type = "post";
$post_parent = "0";
$post_ID = (int) $wpdb->insert_id;
echo $post_ID; die;
Yesterday with a simple wp_insert_post(my_post); all worked fine, today (of course i think i edited some files...or i don't now), not works more and not give me error.
What do you think ?
Thank you, really!
EDIT: The problem was that i not insert post_category and wp need it!
You can capture the $post_id with the wp_insert_post() function...
$post_ID = wp_insert_post($my_post, TRUE);// try first time
Then you can dump out the $post_ID variable to get the ID or the error.
var_dump($post_ID);
add_action( 'init', 'my_func' );
function my_func() {
$my_post = '';
if( get_page_by_title($article_title,'OBJECT','post') == NULL )
$my_post= array(
'post_title' => $article_title,
'post_name' => $article_title,
'post_type' => 'post',
'post_content' => $article_content,
'post_status' => 'pending',
'post_author' => 1
);
$post_id = wp_insert_post( $my_post );
echo 'Post ID - '.$post_id;
}

Categories