Getting Fatal error: Uncaught Error: Call to undefined function plugin_dir_path() when linking to another file within my wordpress plugin

wordpress.stackexchange https://wordpress.stackexchange.com/questions/367725

Question

I created a plugin TESTPLUGIN and added it on wordpress admin menu. I can access the plugin from the admin menu just well. The problem I am having is how to link to another file within the plugin. The file members.php is loaded by the add menu callback function. In this members.php I want to add a link to show_member.php load a single member when the name of a member is clicked. Both files are under one directory.

`<a href="<?php echo  plugin_dir_url(__FILE__).'show_member.php?id=4';?>">John Smit`h</a>
the url resolves to  **http://localhost/mywebsite/wp-content/plugins/myplugins/views/members.php** which is the correct path of show_member.php

But am getting this error message when I click the link: Uncaught Error: Call to undefined function plugin_dir_path() in C:\xampp\htdocs... I have also tried plugins_url() but got the same error. Can someone guide how I could link to another file within my wordpress plugin. What an I doing wrong? Thanks.

Was it helpful?

Solution

I solved the problem using the admin_url() wordpress function. In order to link to another page in the plugin or even to another plugin within my project, I did it by passing the registered url to admin_url() function.

<a href="<?php echo  admin_url('admin.php?page=edit-member-page&id='.$id);?>">John Smit`h</a>

//The $id variable passed to the link will be retrieved by use of `$_GET['id']; superglobal on edit_member.php But first url sluf: edit-member-page first should be registered in your plugin or functions.php

in my plugin main

 //add menu call back
    function load_edit_member_page_callback(){
    include_once("edit_member.php")
    }
//add admin menu action
add_action('admin_menu','register_edit_member_fun');
//implement register_edit_member_fun function
function register_edit_member_fun(){
add_menu_page(
    'edit member', //page title
    '', //menu title (I left it blank because  I don't want the menu to appear to users. I just need to make use of the slug to link internally
    'manage_options', //capability
    'edit-member-page', //slug, this is what I passed to hre attribute above
    'load_edit_member_page_callback',//callable function


    );
}

Hope this helps someone.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top