douxin8383 2015-07-23 12:15
浏览 42

为什么Wordpress add_menu与Initial插件页面分开时不起作用?

So I'm working on a very very basic plugin for Wordpress. I copied the following from the internet a menu item plugin named menu.php

<?php

/*
Plugin Name: Test plugin
Description: A test plugin to demonstrate wordpress functionality
Author: Simon Lissack
Version: 0.1
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

add_action('admin_menu', 'test_plugin_setup_menu');

function test_plugin_setup_menu(){
    add_menu_page( 'Test Plugin Page', 'Test Plugin', 'manage_options', 'test-plugin', 'test_init' );
}

function test_init(){
    echo "<h1>Hello World!</h1>";
}

?>

This works as expected but it's a stand alone plugin. I currently have a different plugin

<?php

/*
Plugin Name: My plugin
Description: blah blah
Author: Me
Version: 0.1
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

fun fun database work on register_activation_hook
?>

The problem is I'd like to have it all in one plugin not two I tried this and I deleted the commenting that made it register as a plugin in menu.php

/*
Plugin Name: My plugin
Description: blah blah
Author: Me
Version: 0.1
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

include_once('menu.php');
fun fun database work
?>

Fatal error: Call to undefined function wp_get_current_user() in wp-includes\capabilities.php on line 1387 and this is the error I get. However doing the following gives me the desired result

/*
Plugin Name: My plugin
Description: blah blah
Author: Me
Version: 0.1
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

 add_action('admin_menu', 'test_plugin_setup_menu');

function test_plugin_setup_menu(){
    add_menu_page( 'Test Plugin Page', 'Test Plugin', 'manage_options', 'test-plugin', 'test_init' );
}

function test_init(){
    echo "<h1>Hello World!</h1>";
}

fun fun database work
?>

The reason I'd like to know how to get it to work using the include or require or at least know why I can't is because I'm sure my plugin will grow and I would like to have it in a bunch of smaller easier for me to maintain files than one big initial file.

Thank you for your help ohh I've also tried

<?php

/*
Plugin Name: My plugin
Description: blah blah
Author: Me
Version: 0.1
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

  //  add_action('plugins_loaded','load_menu');//didn't work same error
add_action('init','load_menu');//didn't work same error

function load_menu(){
    include_once('menu.php');
 }
fun fun database work
?>

Thanks again for any help

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 ogg dd trandata 报错
    • ¥15 高缺失率数据如何选择填充方式
    • ¥50 potsgresql15备份问题
    • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
    • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
    • ¥60 pb数据库修改与连接
    • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
    • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
    • ¥20 神经网络Sequential name=sequential, built=False
    • ¥16 Qphython 用xlrd读取excel报错