How do I make a wordpress plugin with menu item etc

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.

Read More

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

Related posts

Leave a Reply

2 comments

  1. 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();
    
  2. 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.