当前位置: 动力学知识库 > 问答 > 编程问答 >

wordpress - add_filter hook not working in plugin's php file

问题描述:

I am developing a plugin and it works fine when I call my code from my plugin's main php file but I want to run only once My external file is called in the plugin. I mean I want to place my function inside includes folder and in a file living there. like plugin folder/includes/file.php ... here is my code

 add_filter('wp_nav_menu_items',',my_custom_function');

function my_custom_function ($nav){

return $nav."<li class='menu-header-search'><a href='#'>Icon</a></li>";

}

Kindly let me know how can I make it work, it works in side plugin's main php file but does not work from other php files inside plugin includes folder.

网友答案:

It should work if you include your file from the plugins main file, just make sure it is included correctly.

include plugin_dir_path( __FILE__ ) . 'includes/file.php;

This would include a file in an includes folder in your custom plugin folder, and would include an file named: "file.php". If you want to know if its been loaded correctly, just add an die() statement in the included file:

die('my include file has been loaded correctly');

Just remove the die() statement after you have an confirmation that its working :) Lastly you need to copy your callback function code and paste it in your included file. If your hook still does not work, then it might be something wrong with your callback function.

The documentation for the filter hook you are using: https://developer.wordpress.org/reference/hooks/wp_nav_menu_items/

分享给朋友:
您可能感兴趣的文章:
随机阅读: