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

plugin development - Access to the template file of a plug-in

programmeradmin5浏览0评论

I am developing a plugin that consists only of REST API queries. It consists only of the myplugin.php and the template files (index.php, style.css & functions.js) and has no connection to the installed WP-theme.

The plugin should be accessible via the URL: example/myplugin, which I achieved with this entry in plugin.php:

add_action('init', function () {
    add_rewrite_rule('myplugin', 'wp-content/plugins/myplugin/theme/index.php', 'top');
});

In the index.php I can unfortunately only include the two external scripts (.css, .js) via absolute path, because if I define a variable in plugin.php, it is not output in index.php.

How can I integrate the template (index.php) so that it has access to the previously defined variables?

I am developing a plugin that consists only of REST API queries. It consists only of the myplugin.php and the template files (index.php, style.css & functions.js) and has no connection to the installed WP-theme.

The plugin should be accessible via the URL: example.com/myplugin, which I achieved with this entry in plugin.php:

add_action('init', function () {
    add_rewrite_rule('myplugin', 'wp-content/plugins/myplugin/theme/index.php', 'top');
});

In the index.php I can unfortunately only include the two external scripts (.css, .js) via absolute path, because if I define a variable in plugin.php, it is not output in index.php.

How can I integrate the template (index.php) so that it has access to the previously defined variables?

Share Improve this question asked Feb 10, 2022 at 11:17 koljakolja 1032 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

maybe dirty, but it works:

$slug = $_SERVER['REQUEST_URI'];
$slug = explode('/', $slug);
$slug = array_filter($slug);
$slug = end($slug);
if ($slug == 'myplugin') {
    include(__DIR__.'/theme/index.php');
    exit;
}
发布评论

评论列表(0)

  1. 暂无评论