Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
1  
I've added an answer below, but upon re-reading your question I think I might have misunderstood what you are asking. Are you asking how to create plugin specific pages in the "wp-admin" panel or creating front-facing pages like a profile page or contact page for example? –  DigitalSea Jan 30 '12 at 22:53
    
Creating admin pages is easy. I want to know how to create non-admin pages. –  Roman Jan 31 '12 at 7:04
    
where, on wich point you will create pages; the post type page, also content on this post type? –  bueltge Jan 31 '12 at 9:49
    
Oh, so you mean you would like to create a standard page via your plugin like you would if you logged in, went to pages and then clicked add new, correct? Say for example your plugin is a custom login plugin and it creates a custom login page called "superplugin-login" I'll update my answer for you. –  DigitalSea Jan 31 '12 at 22:55
    
Please see my updated anwer, Am01 I think it's what you were asking for. –  DigitalSea Feb 3 '12 at 1:05

2 Answers 2

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.

share|improve this answer

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!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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