admin管理员组文章数量:1122826
On the backend I can generate a backend page with add_menu_page
and have a callback function which generates the page content. On the frontend I know only of wp_insert_post
which can have a page_template
argument (did not work for me), but does not have a callback. Is there a way to generate the content of a plugin frontend page using a callback function?
Currently I have this code:
add_action('init', function (){
$this->pages = get_option($this->plugin_prefix.'pages', []);
if (!isset($this->pages['custom']) || is_null(get_post($this->pages['custom'])))
$this->pages['custom'] = wp_insert_post([
'post_title' => 'My Custom Page',
'post_content' => 'My custom page content',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page',
//'page_template' => $this->plugin_directory.'templates'.DIRECTORY_SEPARATOR.'test.php'
// not working
]);
if (!isset($this->pages['test']) || is_null(get_post($this->pages['test'])))
$this->pages['test'] = wp_insert_post([
'post_title' => 'My Test Page',
'post_content' => 'My test page content',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page'
]);
update_option($this->plugin_prefix.'pages', $this->pages);
});
It would be nice to generate the page content for these pages instead of having something static.
On the backend I can generate a backend page with add_menu_page
and have a callback function which generates the page content. On the frontend I know only of wp_insert_post
which can have a page_template
argument (did not work for me), but does not have a callback. Is there a way to generate the content of a plugin frontend page using a callback function?
Currently I have this code:
add_action('init', function (){
$this->pages = get_option($this->plugin_prefix.'pages', []);
if (!isset($this->pages['custom']) || is_null(get_post($this->pages['custom'])))
$this->pages['custom'] = wp_insert_post([
'post_title' => 'My Custom Page',
'post_content' => 'My custom page content',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page',
//'page_template' => $this->plugin_directory.'templates'.DIRECTORY_SEPARATOR.'test.php'
// not working
]);
if (!isset($this->pages['test']) || is_null(get_post($this->pages['test'])))
$this->pages['test'] = wp_insert_post([
'post_title' => 'My Test Page',
'post_content' => 'My test page content',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page'
]);
update_option($this->plugin_prefix.'pages', $this->pages);
});
It would be nice to generate the page content for these pages instead of having something static.
Share Improve this question edited Mar 19, 2024 at 21:22 inf3rno asked Mar 19, 2024 at 20:22 inf3rnoinf3rno 1356 bronze badges 2 |1 Answer
Reset to default 0I solved it with the help of @PatJ's comment.
add_filter('the_content', function ($content) use ($callback){
if (is_singular() && in_the_loop() && is_main_query()){
$id = get_the_ID();
if (in_array($id, array_values($this->pages))){
ob_start();
$callback();
return ob_get_clean();
}
}
return $content;
}, 1);
Note that in this solution the headers are already sent and only rendering is possible. If we want to send for example a location header to redirect the page, then we need a different solution, which is:
add_action('init', function (){
$id = url_to_postid($_SERVER['REQUEST_URI']);
if (in_array($id, array_values($this->pages)))
$callback();
});
So for example in the init we can do redirection if it is a POST and in the_content we can do rendering if it is a GET request.
本文标签: plugin developmentHow to generate frontend page with callback
版权声明:本文标题:plugin development - How to generate frontend page with callback? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736311961a1934924.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
the_content
filter hook? – Pat J Commented Mar 19, 2024 at 21:19