0

when i activate my plugin i want to auto create pages with shortcodes

this is how my plugin file is structured wp-content\plugins\My First Plugin\shortcode\user-activitys.php this is where the relevant html and php for those pages that are need to be created when i activate my plugin

what do i need to include in the user-activitys.php file with my page html and php

and what do i need to include in the main plugin file

to get user-activitys.php page to appear in the page section of wordpress with a shortcode and my html and php appear when i view the frontend page when i activate my plugin

and the code for the main plugin file here

<?php
/*
Plugin Name: our plugin
Description: ....
Author: ...
Version: ...
*/
add_action('admin_menu', 'our_setup_menu');

function our_setup_menu(){
        add_menu_page( 'our Plugin Page', 'our Plugin', 'manage_options', 'our-plugin', 'Admin_Setting_overview', 'our_init' );
}


?>
  • and the code for the main plugin file here <?php /* Plugin Name: our plugin Description: .... Author: ... Version: ... */ add_action('admin_menu', 'our_setup_menu'); function our_setup_menu(){ add_menu_page( 'our Plugin Page', 'our Plugin', 'manage_options', 'our-plugin', 'Admin_Setting_overview', 'our_init' ); } ?> – new coder Sep 14 '16 at 22:01
  • Please edit your original comment and add the code there; it's easier to read and will increase your chances of getting help. – Dave Romsey Sep 15 '16 at 0:13
  • i have added the code in the original comment but its not showing up so i added it to the comments – new coder Sep 15 '16 at 1:24
1

From what I understood from your question, you will need to use register_activation_hook(), which runs when your plugin is activated.

register_activation_hook(__FILE__, 'your_function_name');

then in that function you can use `wp_insert_post' to create your page.

Refer this link to pass new post parameters as per your need: https://developer.wordpress.org/reference/functions/wp_insert_post/

Edit:

In your plugin file make a function for shortcode.

function function_for_shortcode(){
//Your html here
}
add_shortcode('your_shortcode','function_for_shortcode');

Now when you run register_activation_hook, provide to wp_insert_post, post_content as [your_shortcode].

  • sort of i need when the plugin activates it creates a new page by shortcode within that page is the shortcode and when viewing the frontend page displays the html within the php file in wp-content\plugins\My First Plugin\shortcode\user-activitys.php i see i can add this line of code to the top of the file add_shortcode('user-activitys.', 'user-activitys.' );but i dont know what to put in the main plugin file to get that shortcode to creates that new page when i activate my plugin – new coder Sep 15 '16 at 14:24
  • I'm still not sure what you want. Anyhow, you can assign page-template to new page as your user-activitys.php. Or if you absolutely need shortcode, you'll have to put your html in a function and make that function a shortcode. – Aniruddha Gawade Sep 16 '16 at 6:03
  • thats what i want to do i do need shortcode, i want to put my html in a function and make that function a shortcode. how do i do it ? – new coder Sep 16 '16 at 19:16
  • Edited the answer. – Aniruddha Gawade Sep 17 '16 at 5:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.