Create custom wordpress page after install my custom plugin - php

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.

Related

How to delete a post depends on post author role?

I have created a custom post type and I have Woo Subscriptions. I would like to create a function that detects the post author role of each post.
If the author role is "Unsuscribed" then, change post status by that author to Draft. So with this, if users stops to pay the subscription, automatically their posts will not be shown.
Thank you!
I think that you can use the add_user_role action hook. This hook fires after a user has been given a new role.
add_action( 'add_user_role', 'unpublish_non_subscriber_posts', 10, 2 );
function unpublish_non_subscriber_posts( $user_id, $role ) {
if ( $role !== 'unsubscribed' ) {
return;
}
$user_posts = get_posts( array(
'posts_per_page' => -1,
'post_type' => 'your_custom_post_type',
'post_status' => 'publish',
'author' => $user_id,
'fields' => 'ids'
) );
if ( count( $user_posts ) === 0 ) {
return;
}
foreach ( $user_posts as $post_id ) {
wp_update_post( array(
'ID' => $post_id,
'post_status' => 'draft'
) );
}
}

Wordpress - Add a page upon activation of plugin

I am currently making a Wordpress plugin, but I couldn't find an answer to this.
How do you add a page upon activation of the plugin?
I have added posts upon activation earlier with the wp_insert_post function, but I can't find a way to insert a page.
You should use the activation hook for plugins in order to make any actions upon the plugin activation.
register_activation_hook( __FILE__, 'activation_hook_callback');
function activation_hook_callback()
{
//add the post type and other options in the array for the query
$page = array(
'post_status' => 'publish' ,
'post_title' => 'Page name',
'post_type' => 'page',
);
//add the page and ID will be saved.
$the_page_itself = wp_insert_post( $page );
}
This should work.
I solved it. Wordpress has several post-types:
Post (Post Type: 'post')
Page (Post Type: 'page')
Attachment (Post Type: 'attachment')
Revision (Post Type: 'revision')
Navigation menu (Post Type: 'nav_menu_item')
To add a post upon activation of my plugin:
function add_page_upon_activation() {
$arr = array(
'post_title' => 'title',
'post_name' => 'slug',
'post_status' => 'publish',
'post_type' => 'page',
'post_content' => 'yes, a nice page',
);
wp_insert_post($arr);
}
add_action( 'activated_plugin', 'add_page_upon_activation' );

Add page template to a custom page using custom plugin wordpress

I am new to developing the wordpress plugin. I am developing a plugin and I have succeeded in creating a new page whenever the plugin is activated but I want to add a custom php template file to that page. Thats means on activation I want to create a custom page with a custom template. Is it possible to do?
To create a new page I have used this code but could not find anything on adding the template thing:
register_activation_hook(__FILE__,'my_plugin_install');
function my_plugin_install() {
global $wpdb;
$the_page_title = 'Custom Cart';
$the_page_name = 'cart';
delete_option("Custom Cart");
add_option("Custom Cart", $the_page_title, '', 'yes');
delete_option("cart");
add_option("cart", $the_page_name, '', 'yes');
delete_option("my_plugin_page_id");
add_option("my_plugin_page_id", '0', '', 'yes');
$the_page = get_page_by_title( $the_page_title );
if ( ! $the_page ) {
$_p = array();
$_p['post_title'] = $the_page_title;
$_p['post_content'] = "This text may be overridden by the plugin. You shouldn't edit it.";
$_p['post_status'] = 'publish';
$_p['post_type'] = 'page';
$_p['comment_status'] = 'closed';
$_p['ping_status'] = 'closed';
$_p['post_category'] = array(1);
$the_page_id = wp_insert_post( $_p );
}
else {
$the_page_id = $the_page->ID;
$the_page->post_status = 'publish';
$the_page_id = wp_update_post( $the_page );
}
delete_option( 'my_plugin_page_id' );
add_option( 'my_plugin_page_id', $the_page_id );
}
This is a simple way to create a page with attach template in WordPress. To create a new page I have used this code:
register_activation_hook(__FILE__,'my_plugin_install');
function my_plugin_install() {
$the_slug = 'cart';
$args = array(
'name' => $the_slug,
'post_type' => 'page',
'post_status' => 'publish',
'numberposts' => 1
);
$my_posts = get_posts($args);
if(empty($my_posts)){
$my_post = array(
'post_title' => 'Custom Cart',
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page',
'post_name' => 'cart'
);
// Insert the post into the database
$post_id=wp_insert_post( $my_post );
update_post_meta( $post_id, '_wp_page_template', 'page-templates/cart.php' ); // optional to add page template
}
}
Add custom page template: /theme/your-theme-name/page-templates/cart.php:
<?php
/*
* Template Name: Custom Cart Page
*/
get_header(); ?>
HTML code here ..
<?php get_footer();?>

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

Setting a new page with wp_insert_post as start page

I'm creating an APP with a Wordpress child theme. It has a start page created with wp_insert_post when activating the theme. How do I set this page as startpage with PHP in functions.php?
// Install theme
if ( is_admin() && isset($_GET['activated'] ) && $pagenow == 'themes.php' ) {
$last_id = wp_insert_post(array(
'post_type' => 'page',
'post_title' => 'Welcome to this wonderful page!',
'post_content' => 'Holy smoke',
'post_name' => 'startpage',
'post_status' => 'publish',
'comment_status' => 'closed'
));
update_post_meta($last_id, "_wp_page_template", "page.php");
// Set this page as startpage... but how?
} // Install theme
If understood correctly, you are searching for the option page_on_front:
Just use:
// Set "static page" as the option
update_option( 'show_on_front', 'page' );
// Set the front page ID
update_option( 'page_on_front', $last_id );

Categories