Completely custom page in WordPress generated by a plugin

I am writing a WordPress plugin that has an AJAX element to it – blocks of HTML are fetched by the front end from the plugin using AJAX.

I am having difficulty joining up the pieces here, and I suspect it is just a terminology issue. How would I implement a page completely provided by the plugin?

  • The content of the page will be HTML – the plugin can generate this in response to POST or GET parameters.
  • There needs to be a route to this page. The route does not have to be user-friendly or a REST style – just some kind of URI that gets to the plugin. Is there perhaps a way to register a custom page with an arbitrary name, without having to create it as a WP post?
  • I would like all this to be self-contained in the plugin, so should not involve the administrator having to create posts or pages, or have to add anything to the theme.
  • Ideally I would avoid any URLs that go into the wp-admin directory. End users don’t belong in here.

Related posts

1 comment

  1. I would strongly suggest referring to https://codex.wordpress.org/AJAX_in_Plugins#Ajax_on_the_Viewer-Facing_Side

    You need to have a php script in your plugin directory that returns what you request, and you need to determine that url at run time for reference in your ajax. The above link gives an example for enqueuing and using wp_localize_script() to provide the url for your custom php script.

    wp_enqueue_script( 'ajax-script', 
            plugins_url( '/js/my_query.js', __FILE__ ), array('jquery') );
    
      // in JavaScript, object properties are accessed as 
      //  ajax_object.ajax_url, ajax_object.we_value
      wp_localize_script( 'ajax-script', 'ajax_object',
                array( 'ajax_url' => plugins_url( '/php/myapi.php' ));
    

    Your javascript file will be included on every page and will listen for events on the page which require a block of HTML, as you’ve described it.

    Your file myapi.php then needs to take a request, probably using a query string, get the appropriate content from the wordpress api, and respond with said content, which your javascript will put into place.


    To have access to the wordpress api as well though, you have two options:

    1. Force wordpress to run starting with your file, by including wp-load.php. This is probably not the cleanest way.
    2. Set up a custom page or slug to direct to your plugin.

    I would advise the second option, and advise a slug, in which case you may find this post helpful: wp_rewrite in a WordPress Plugin


    From Jason’s comment, based on the link above:

    The rewrite rules are mentioned a lot, but are really a distraction –
    they just help to make URLs look more “friendly”, which was not an
    objective here. The key is: register a custom GET parameter; look for
    that parameter early in the page rendering process; if you find the
    parameter is set, then output/echo stuff and die(). There are a
    number of hooks that can be used to look at the parameters, chosen
    dependin on how much you want WP to set up and process first.

Comments are closed.