最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Getting Fatal error: Uncaught Error: Call to undefined function plugin_dir_path() when linking to another file within my w

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked May 28, 2020 at 2:32 wafutechwafutech 1013 bronze badges 1
  • 1 As the error states, the plugin_dir_path() function is not defined (doesn't exist) when your code attempts to call it. You probably need to wrap your test plugin code in a new function which hooks into WP and runs at a later point. – Kevin Vess Commented May 28, 2020 at 5:04
Add a comment  | 

1 Answer 1

Reset to default 0

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论