WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Creating a URL for a function (8 posts)

  1. Cory.Nickerson
    Member
    Posted 1 year ago #

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

    admin.php?page=scholarship_view&id=1

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

  2. Cory.Nickerson
    Member
    Posted 1 year ago #

    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.

  3. bcworkz
    Member
    Posted 1 year ago #

    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.

  4. Cory.Nickerson
    Member
    Posted 1 year ago #

    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.

  5. Cory.Nickerson
    Member
    Posted 1 year ago #

    <?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.

  6. Cory.Nickerson
    Member
    Posted 1 year ago #

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

    http://codex.wordpress.org/Function_Reference/add_plugins_page

  7. bcworkz
    Member
    Posted 1 year ago #

    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.

  8. bcworkz
    Member
    Posted 7 months ago #

    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 :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.