Wordpress Plugin - Delete a Page Created when deactivating - php

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

Related

Create custom post type whenever another custom post type is created

I'm trying to add a feature to my Wordpress website, that every time I add a post (custom post type), another post (different custom post type) is created.
I have tried adding an action in my function file but it does not seem to work.
functions.php
function add_notification($post_id) {
global $wpdb;
$post_type = get_post_type($post_id);
if($post_type == 'exercises'){
$title = "BLA";
$post_id_new = wp_insert_post(
array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_title' => $title,
'post_status' => 'publish',
'post_type' => 'notification'
)
);
}
}
add_action('publish_post', 'add_notification');
I have also tried it with other hooks like new_to_publish and publish_post
With that I was expecting to have a new post (notification) when I add an exercise post.
I think it's something silly I'm forgetting but it's been like 3 days that I'm stuck in this problem.
For custom post type we have to use hook like
add_action('publish_custom-post-name','function');
In your case you have to use Like Below
function add_notification($post_id) {
global $wpdb;
$post_type = get_post_type($post_id);
if($post_type == 'exercises'){
$title = "BLA";
$post_id_new = wp_insert_post(
array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_title' => $title,
'post_status' => 'publish',
'post_type' => 'notification'
)
);
}
}
add_action('publish_exercises', 'add_notification');
Please Try this. If any query feel free to ask.

Create a page with gutenberg HTML block automatically upon plugin activation

I have managed to create a page upon plugin activation.
i need to add content to the page that get created. so i used following code. The problem is it creates a classic editor in the page and not the gutenberg HTML editor.
register_activation_hook( __FILE__, 'my_plugin_install_function');
function my_plugin_install_function()
{
//post status and options
$post = array(
'comment_status' => 'closed',
'ping_status' => 'closed' ,
'post_author' => get_current_user_id(),
'post_date' => date('Y-m-d H:i:s'),
'post_name' => 'Checklists',
'post_status' => 'publish' ,
'post_content' => '[customization-shortcode]',
'post_title' => 'Checklists',
'post_type' => 'dash',
);
//insert page and save the id
$newvalue = wp_insert_post( $post, false );
//save the id in the database
update_option( 'hclpage', $newvalue );
}
i expect to have my shortcode displayed in a gutenberg HTML block and not the classic editor
I have found a solution to this, all i did is change following line:
'post_content' => '[customization-shortcode]',
to
'post_content' => '<!-- wp:html -->[customization-shortcode]<!-- /wp:html -->',
This will create a usual gutenberg HTML block instead the old classic one.

Delete Page after plugin deactivation code not working

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

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.

Add html/PHP pages on plugin activation (Wordpress)

Well am creating my first wordpress plugin, which should create a page (relatively large) on activation.
Currently i can create a file using :
$_p['post_title'] = $the_page_title;
$_p['post_content'] = '<h1>PAGE CONTENT</h1>';
$_p['post_status'] = 'publish';
$_p['post_type'] = 'page';
$_p['comment_status'] = 'closed';
$_p['ping_status'] = 'closed';
$_p['post_category'] = array(1); // the default 'Uncatrgorised'
// Insert the post into the database
$the_page_id = wp_insert_post( $_p );
The problem is, I cant put all the content of the file(which is large) as string to 'post_content' index. I want to know if there is a way, where i can simply either:
'post_content' => link to the file in my plugin directory
'post_content' => call a function which will return html content as string :( [worst case]
OR, Some more simpler way to achieve the objective.
Please help me.
Well, what i did to solve the problem is:
//**SECTION : 1**
function user_login_foo() {
return get_login_form(); // get_login_form() function will return the html template i want to display.
}
add_shortcode('user_login', 'user_login_foo'); // created a shortcode
**// SECTION: 2**
function get_login_form()
{
ob_start(); ?>
<h3><?php __('Login'); ?></h3>
<form action="" method="post">
<fieldset>
// the login form comes here
</fieldset>
</form>
<?php
return ob_get_clean();
}
function validate_login_user() {
// the login validation logic comes here
}
add_action('init', 'validate_login_user');
SECTION 1: registered a shortcode that will call a function [say,foo1()] and return the value.
The function foo1() calls another a function [say foo2()] which returns a clean html form in response to the call (when the shortcode is called).
SECTION 2: In this section I defined the function foo2(), within which the html form [login form] is defined and returned to foo1() [where it is displayed].
Then i created an action [ add_action('init', 'validate_login_user'); ] which will call the function validate_login_user() on initialization, inside this function i checked for isset(METHOD[username]) and isset(METHOD[password]) and then do respective logic.
Like this i created multiple [shortcodes] for each of the pages I wanted to create at the time of activation,
Then :
**step 1:** register_activation_hook(__FILE__,'activation_plugin');
**step 2:** activation_plugin(){
'390' => [ // '390' is page id
'post_title' => 'Page title say login',
'post_content' => "[user_login]",
'post_status' => 'publish',
'post_type' => 'page',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_category' => array(1)
],
'391' => [
'post_title' => 'page title 2',
'post_content' => "[short_code2]",
'post_status' => 'publish',
'post_type' => 'page',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_category' => array(1)
],
// like this add multiple shortcodes
}
// this foreach will create all the pages
foreach ($_ as $key => $value) {
$the_page_title = $value['post_title'];
$the_page_name = $value['post_title'];
// the menu entry...
delete_option($value['post_title']);
add_option($value['post_title'], $the_page_title, '', 'yes');
// the slug...
delete_option($value['post_title']);
add_option($value['post_title'], $the_page_name, '', 'yes');
// the id...
delete_option($key);
add_option($key, '0', '', 'yes');
$the_page = get_page_by_title( $the_page_title );
if ( ! $the_page ) {
$the_page_id = wp_insert_post( $value );
}
else {
$the_page_id = $the_page->ID;
$the_page->post_status = 'publish';
$the_page_id = wp_update_post( $the_page );
}
delete_option( $key );
add_option( $key, $the_page_id );
}
Create Page On Plugin activation with content
$page_content= 'Content of page';
$demo_page = array(
'comment_status' => 'closed',
'ping_status' => 'closed' ,
'post_author' => 1,
'post_content' => $page_content,
'post_date' => date('Y-m-d H:i:s'),
'post_name' => 'page_name',//display on address bar
'post_status' => 'publish' ,
'post_title' => 'Page Display Name',
'post_type' => 'page',
);
//insert page and save the id
$demo_page_value = wp_insert_post( $demo_page, false );
//save the id in the database
update_option( 'testpage', $$demo_page_value );
You Use and Guide and read this link How to Create plugin and pages https://codex.wordpress.org/Creating_Options_Pages

Categories