How to create a new page from my Wordpress plugin

In my wordpress plugin, I want to generate a page on the fly.

I could create a page for me. But I would rather not take any steps for them. Just let them activate it and it works.

So, I was wondering if there is a way to do this that supports all the functionality of the plugin.

My initial idea was to add a rewrite rule

add_rewrite_rule ('my_page / $', 'wp-content / plugins / my_plugin / page.php', 'top');

Then in my plugin I can have page.php. This works well, but I cannot get the header / footer, etc.

I am very new to Wordpress, so the odds are that I don't see anything obvious.

+3
source share
1 answer

You can create a 404-page piece of code that wp_insert_post () does, and then redirect the user to it.

Your 404.php theme will look like this:

<?php
$post_id = wp_insert_post("post_title" => "my post title", "post_content" => "Lol");
header("location:" . get_permalink( $post_id ) );
die();
?>
+4
source

All Articles