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 Are you looking for the the_content filter hook? – Pat J Commented Mar 19, 2024 at 21:19
  • 1 @PatJ I edited the question, maybe it is clearer now. – inf3rno Commented Mar 19, 2024 at 21:24
Add a comment  | 

1 Answer 1

Reset to default 0

I 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