开发者

how do I programically create a page in WordPress if it doesn’t exist already?

开发者 https://www.devze.com 2023-01-08 17:34 出处:网络
how do I programically create a page in WordPress if it doesn开发者_开发百科’t exist already?

how do I programically create a page in WordPress if it doesn开发者_开发百科’t exist already?


I want to write a plugin and to put some html controls in a page which will automatically create when user install the plug in

Based on that comment, you want to hook a function to your plugin's activation hook, which inserts a WordPress post object into the database;

function my_plugin_activate()
{
    wp_insert_post(array(
        'post_type' => 'page',
        'post_title' => 'Page Title',
        'post_content' => 'Page Content',
        'post_name' => 'page-slug',
    ));
}
register_activation_hook(__FILE__, 'my_plugin_activate');


How will you identify that page? Assuming you have a specific title, use something like

if( get_page_by_title('my_title') === false ) // page doesn't exist
{
    // insert the page using wp_insert_post
}

If you need help with wp_insert_post, please comment.


You can implement a function that intercepts the template_redirect action/filter, and inside that function include the theme's header and footer, while creating your own content to put into the body of the page. See the API link (above) for an example.

Here's a tutorial on the web.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号