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.

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 Answers
2

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();

Leave a Comment