Support » Plugins » Hacks » Creating a URL for a function

  • Resolved Cory.Nickerson


    I have a custom function which simply includes another file. But I can’t figure out how to make a URL for this function.


    The link currently just loads an error page. I need it to load a file in my plugins folder scholarship/scholarship3.php.

Viewing 7 replies - 1 through 7 (of 7 total)
  • I guess basically what I need to do is create a slug for the URL and define a function for it to execute when the URL is visited? Not sure how to do this though.

    Moderator bcworkz


    To include a file, you could do something like:
    include plugins_url( 'scholarship3.php', __FILE__ );
    But I’m uncertain if that’s really what you want to do. Could you explain further the context of what is causing this page to be needed and briefly what it does. I may be able to provide better advice.

    I want it to work like how add_menu_page works kind of.

    In add_menu_page the 4th argument creates a slug for the URL then the 5th argument is a function to execute when that URL is visited.

    <?php add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position ); ?>

    See how in that function the $menu_slug and $function works? Thats what I need to do but WITHOUT creating a new menu. I just need to assign a function to a URL when that URL is visited.

    Something along the lines of how this works, but I don’t want it to create a link in the menu.

    Moderator bcworkz


    OK, that helps, but I’m still a little uncertain where you’re going with this. I will focus on you want to do something in PHP when an URL is requested.

    The add_menu_page() was not a good analogy actually. When you provide a function argument, the menu_slug argument becomes just a label. The URL is never requested, only the function is called. Anyway…

    I’ll assume you have a link(s) on your site to an arbitrary PHP page that you’ve created. It may or may not contain HTML elements. If you want this page to do something in PHP, you don’t necessarily need a function, any code encountered outside of a function or similar definition will be executed immediately. Or you can define a function and then have the page immediately call the function upon page load.

    If you want to use WordPress functions on this page, you need to initiate the WordPress environment. This is best done with require_once(wp-load.php); , with the path corrected as needed. If the user must be logged in, require once wp-admin/admin.php instead.

    I hope this helps, if I have misunderstood, I apologize, please try to explain again an I’ll do my best to help.

    Moderator bcworkz


    Follow up: My suggestion to require_once wp-load.php, while it works, is considered very poor practice because one cannot know the WP file structure for any particular installation so the relative reference could fail. It’s often suggested to use an AJAX technique in order to access WP functions. Such a solution is almost always possible, though not always desirable. As it happens, there is a similar technique to the AJAX approach, except no javascript or jQuery is required, your request handler can be initiated from a simple HTML link or form action. It is suitable for any GET or POST request. The handler can include another PHP file and in the process enable access to WP functions on that file.

    See Plugin_API/Action_Reference/admin_post_(action).

    Better late than never 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Creating a URL for a function’ is closed to new replies.