Php 如何直接从浏览器URL调用自定义插件函数
我是wordpress的新手,我正在开发wordpress自定义插件,我想直接从浏览器URL调用自定义插件函数,我正在尝试使用此URLPhp 如何直接从浏览器URL调用自定义插件函数,php,wordpress,Php,Wordpress,我是wordpress的新手,我正在开发wordpress自定义插件,我想直接从浏览器URL调用自定义插件函数,我正在尝试使用此URLhttp://localhost/wordpress-o/wp-admin/admin.php?page=master&action=test但它不起作用,这是我的全部插件代码,有人能检查一下我的代码吗,帮我解决这个问题 class WCP_BackEnd_Master_Controller { public function index() {
http://localhost/wordpress-o/wp-admin/admin.php?page=master&action=test
但它不起作用,这是我的全部插件代码,有人能检查一下我的代码吗,帮我解决这个问题
class WCP_BackEnd_Master_Controller {
public function index() {
ob_start();
global $wpdb;
include(dirname(__FILE__) . "/html/list_master_details.php");
$s = ob_get_contents();
ob_end_clean();
print $s;
}
public function test() {
echo "sdsdsd";
die;
}
function add_menu_pages() {
add_menu_page('Master', 'Master', 'manage_options', 'master', Array("WCP_BackEnd_Master_Controller", "index"));
}
}
add_action('admin_menu', array("WCP_BackEnd_Master_Controller", 'add_menu_pages'));
add_action('wp_ajax_WCP_BackEnd_Master_Controller::test', Array('WCP_BackEnd_Master_Controller', 'test'));
add_action('wp_ajax_nopriv_WCP_BackEnd_Master_Controller::test', array('WCP_BackEnd_Master_Controller', 'test'));
我发现,我们可以创建RESTAPI并使用它,这里是我使用RESTAPI所做的 需要与此URL一起使用:
http://localhost/wordpress-o/wp-json/my-route/my-phrase
function my_register_route() {
register_rest_route('my-route', 'my-phrase', array(
'methods' => 'GET',
'callback' => 'custom_phrase',
)
);
}
function custom_phrase() {
return rest_ensure_response('Hello World! This is my first REST API');
}
add_action('rest_api_init', 'my_register_route');
没有办法直接运行插件代码。Wordpress将根据您设置的操作和筛选器调用它。您能告诉我如何调用吗?