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' );
Related
Further i need the automatically created page to be edited by the respective subscribe only and when published it should be visible to all users who visit the website
Perhaps you need to use the user_register hook and execute the function in which you create the post, and to create a post, you need to use wp_insert_post function
add_action('user_register','my_function');
function my_function($user_id){
// Create post object
$my_post = array(
'post_title' => 'Title of page',
'post_type' => 'post', // Set type of your post
'post_content' => $_POST['post_content'],
'post_status' => 'publish', // Page published
'post_author' => $user_id // Assign page author
);
// Insert the post into the database
wp_insert_post( $my_post );
}
You can also manage Roles and Capabilities for your post type
Examples:
https://wordpress.stackexchange.com/a/208739/193674
https://wordpress.stackexchange.com/a/108375/193674
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.
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' );
I want to create system to insert wordpress post from front-end to logged user. But I have checked same title post many times added by different user.
How I add error message if post title already exist
Here is my code
$u_id = get_current_user_id();
$my_post = array(
'post_title' => 'title',
'post_content' => 'this is post content',
'post_status' => 'publish',
'post_author' => $u_id,
'post_type' => 'customer_post',
);
$post_ID = wp_insert_post( $my_post );
You can prevent this by checking some condition. You should try this. Wrap your code within this condition, it will not allow post with same title.
if (!get_page_by_title($title, 'OBJECT', 'post') ){
//your code goes here.
}
I have a WordPress page created dynamically
$my_post = array(
'post_title' => 'page-for-download',
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page'
);
now my question is how to assign page template to this page dynamically
WordPress keeps the page template in a post meta entry(named _wp_page_template). Here is what you should do once you create the page:
update_post_meta( $new_post_id, '_wp_page_template', 'custom-template.php' );
Where $new_post_id is the result of wp_insert_post()(I assume that this is what you are using to create the new post). Note, that you might want to check to see if you have an actual id(by default wp_insert_post() will return false if it fails to create a new post).
You can see that information in the first NOTE in the Parameters section of the WordPress codex page Function Reference/wp insert post