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?
1 Answer
Reset to default 0maybe 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;
}