How can plugins have their own pages?

My plugin needs to introduce a number of interfaces to WordPress for the public traffic. What is the best way of introducing new interfaces?

Related posts

Leave a Reply

2 comments

  1. Believe it or not the WordPress official documentation has a great page covering this topic called Administration Menus. It also provides example code on how to use WordPress hooks in your plugin code to add custom setting pages and other pages like that.

    The page can be found here. Fire up the kettle and make yourself a coffee or tea before reading, because it is quite a large page. It even covers things like adding in forms and saving settings for your plugin that you can access.

    ** Update **

    After further understanding what you were asking, it’s apparent you want to create a WordPress page from within your plugin, not an admin screen. Depending on whether or not you want to create it upon activation or when a user does a particular thing I’ll assume you want to create it upon activation and remove it upon deactivation.

    I did not add in code for deleting pages, you should be able to work that part out though. The following should add in a page upon activation of your plugin.

        <?php
    
        register_activation_hook(__FILE__, 'activate_plugin');
        register_deactivation_hook(__FILE__, 'deactivate_plugin');
        register_uninstall_hook(__FILE__, 'uninstall_plugin');
    
        function activate_plugin() {
    
            // This function creates the pages
            create_pages();
        }
    
        function deactivate_plugin() {
    
            // This function removes the pages
            remove_pages();
        }
    
        function uninstall_plugin() {
    
            // This function removes the pages
            remove_pages();
        }
    
        function create_pages() {
    
            // Create post object
            $_p = array();
            $_p['post_title']     = "Some page title";
            $_p['post_content']   = "This is content in a page added via a plugin. This was not added manually, run!";
            $_p['post_status']    = 'publish';
            $_p['post_type']      = 'page';
            $_p['comment_status'] = 'closed';
            $_p['ping_status']    = 'closed';
            $_p['post_category'] = array(1); // the default 'Uncategorised'
    
            $page_id = wp_insert_post($_p);
    
            if ( $page_id ) {
    
                return true;
    
            }
    
        }
    
        function remove_pages() {
    
            // Code in here to remove pages you added
    
        }
    
    ?>
    

    If you have any questions or issues using the sample code post up what you’re trying to accomplish and someone will sort you out. Good luck.

  2. I’m not sure that Dwayne’s answer on how to create a WordPress “Page” is what the poster is looking for. Here are some disadvantages of creating a WordPress Page.

    1. It may interfere with the site, for example start showing up on menus.
    2. It is visible to the site admins, who may mess with it.
    3. I think there are a lot of limitations to a Page, for example you don’t have full control over the appearance and you can’t really interact with the user. (I’m not a WordPress expert, so I might be missing something here.)
    4. It’s overkill if you just want a simple page.

    Based on the poster’s desire for “interfaces to WordPress”, I’m going to assume Am01 wants to create a “page” (lower-case), not a WordPress “Page”–ie, just some arbitrary PHP creating arbitrary HTML.

    I had the same this problem. I needed a callback URL for an OAuth login sequence. Also, I wanted to create a self-contained test-bed page for my Plugin. Here is how I did it:

    1. Created a file foo.php in my plugin directory.
    2. At the top, put

      <?php
      // HACK: initialize the WordPress environment, enough to use the Plugin
      require_once( dirname(__FILE__) . '/../../../wp-load.php' );
      
    3. Wrote my code below. The code can make all normal calls to WordPress and the Plugin.

    4. Refer to the page from the Plugin with plugins_url('foo.php', __FILE__).

    This is unofficial! One reason it is a hack is it hard-codes the location of wp-load.php. If the plugin directory were in a non-default location, it might not work. Also, the URL to the page is pretty ugly (/wp-content/...).

    But I couldn’t find an official or cleaner way to do this. If someone has one, I’d be grateful for it!