2

I've been reading documents, viewing videos, etc. on how to create a Wordpress plugin. I've learned how to filter a post, add txt to a post, use conditionals to see if the page is a single post as not to display text throughout the entire site, etc.

The part that I'm not understanding is how to create a plugin, with it's own menu item that will bring the user to this plugin that im creating.

Lets say for example my plugin should be a blank page (with Wordpress header, footer, etc.) that simply displays 'Hello World!' - How do I create this very simple plugin, on its own page (not seen all over the site) with its own menu button that brings the user to this plugin?

Do I create a template and a menu item that links to the template? I'm so confused... I found how to 'Create page' in the admin section, but that seems to only create a menu item that corresponds to a blank page where I can add html.

I can't seem to even find any plugin examples where this is done, but I would think it would be relatively easy to create a plugin that has a menu item so visitors know how to get to the plugin.

If I wanted to create a simple plugin so visitors to my Wordpress can click a menu item called 'Bicycle races' and be brought to my plugin page where they can view the output of my plugin, a list of bicycle races in this case, how would I accomplish this? Do I create a simple plugin, or do I create a template as well?

Please help me locate any possible plugin examples where this is accomplished.

Thanks

2
  • The ultimate goal for my plugin, is for it to programmatically create a menu item that says 'Volunteeers' and when the user clicks it they are brought to my plugin which will display a list of what they can apply for. When they click on one, the signed in user will be brought to a page where we gather information that we don't already have such as their phone number, age, gender, etc. and once saved we will store this information in our database so we know who volunteered for what and one of our staff can review who to contact, keep notes, etc. Commented Oct 7, 2012 at 21:05
  • The only part that I don't know is, how to make this plugin that I'm creating, its own page with its own menu button to that page. I can create the code, but I only know how to get it to run on every page of Wordpress and not on its own page and I Don't know how to create a menu item that goes to that page. Commented Oct 7, 2012 at 21:08

2 Answers 2

3

If you want a frontend page you'll have to create one with your plugin's shortcode as the content. Then you display your plugin's output in-place of that shortcode:

/*
Plugin Name: WPSE67438 Page plugin
*/
class wpse67438_plugin {
    const PAGE_TITLE = 'WPSE67438'; //set the page title here.
    const SHORTCODE = 'WPSE67438'; //set custom shortcode here.

   function __construct() {
        register_activation_hook( __FILE__, array( $this, 'install' ) );
        add_shortcode( self::SHORTCODE, array( $this, 'display' ) );
    }

public function install() {
    if( ! get_option( 'wpse67438_install' ) ) {
        wp_insert_post( array( 
                    'post_type' => 'page',
                    'post_title' => self::PAGE_TITLE,
                    'post_content' => '[' . self::SHORTCODE . ']',
                    'post_status' => 'publish',
                    'post_author' => 1
                    )
                );
        update_option( 'wpse67438_install', 1 );
    }
}

public function display( $content ) {
    $my_plugin_output = "Hello World!"; //replace with plugin's output
    return $content . $my_plugin_output;
  }
 }

 new wpse67438_plugin();
1

you'll need some plugin options at the very least to allow users to decide how/where your plugin page is accessed. some users may be using wp_list_pages to output a menu, others may use a wp_nav_menu instance to enable navigation, and in that case they could have multiple menus registered, and then there's the matter of where within a menu they'd want it. this answer contains code for auto-adding a menu item to a nav menu, but it may be better to just provide instructions for user's to add your plugin menu item.

as for the "page" itself, there are a few different strategies I've seen in various plugins:

  1. have a shortcode that users can insert into a page to display your plugin's output, like Abdussamad's answer.
  2. another version of the above- create a page on plugin activation with the content being a shortcode. users then have the option of moving the shortcode to another page, renaming the page, adding additional content, etc.
  3. have user's select a page via an admin option, and filter the_content on that page to insert your plugin's output.
  4. use a rewrite rule to create a "virtual page", like this answer. this wouldn't integrate well with a theme though, so probably doesn't meet your criteria.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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