Title: How to add a virtual page (created by a plugin) to the menu builder
Tags: php;wordpress
Question: i have been making a plugin which makes it possible to show a schedule in wordpress, but until now i did not find a good way to show the page in wordress.
until now what i did was check if certain querystring variable was set.
but now i found a way to add virtual pages to wordpress:
https://coderwall.com/p/fwea7g/create-wordpress-virtual-page-on-the-fly
i renamed it to virtualClass and used it like this:
new \helpers\VirtualPage(array(
'slug' =&gt; '1002',
'post_title' =&gt; 'Fake Page Title',
'post content' =&gt; 'This is the fake page content'
to tryout, i went to the page: mywordpress.site?page_id=1002
this did work, but when i went to the backend in the menu builder, i wont see this page, i want it to be shown there so my users can add the page to their website.
Is there good tutorial on how to do this? is there a hook i can use to add a section to the menu builder?
Like in the image below: add it to either place of the red arrow
Comment: because people might change it contents and content dynamic (needs php)

Comment: Im a programmer myself i can figure this out, i think i need to use real pages then. is there a way to be sure the page is not deleted by a user?

Comment: Why not create a real page?

Comment: No, you write the content yourself.  When the plugin creates the page, you record it's ID, then you use `add_filter('the_content','YourContentFunction')` and within that function you check the page ID, if it's yours, you overwrite.  If you would like to use this method, I can write up an answer with more code

Comment: Is that a "yes" you'd like some code?

Comment: It is a little drastic, but if they try to delete it, you can exit;

Here is the accepted answer: Instead of using a virtual page, you should create a page with your plugin's activation and then overwrite that page's content.  You will also want to delete
the page on deactivation.  In case you want to protect that page from user deletion, I have included that code as well, but it uses and exit; so it's a bit harsh.
function pluginxyz_activate(){
$title = 'Whatever You Want To Call It';
$new_id = wp_insert_post(array(
'post_title'=&gt;$title,
'post_status'=&gt;'publish',
'post_type'=&gt;'page'
));
update_option('pluginxyz_page_id',$new_id);
function pluginxyz_deactivate(){
$old_id = get_option('pluginxyz_page_id');
if($old_id)wp_delete_post($old_id,true);
delete_option('pluginxyz_page_id');
function pluginxyz_protect_page($post_id){
$old_id = get_option('pluginxyz_page_id');
if($post_id == $old_id)exit('You can not delete this page');
function pluginxyz_use_content($content){
$old_id = get_option('pluginxyz_page_id');
global $post;
if($post-&gt;ID == $old_id){
//que up your content
} else {
return $content;
register_activation_hook(__FILE__,'pluginxyz_activate');
register_deactivation_hook( __FILE__, 'pluginxyz_deactivate');
add_action('wp_trash_post', 'pluginxyz_protect_page', 10, 1);
add_action('before_delete_post', 'pluginxyz_protect_page', 10, 1);
add_filter( 'the_content', 'pluginxyz_use_content' );
Comment for this answer: I should note this was not tested in any way... not even parsed for syntax


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 6, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:08/01/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><wordpress>', 'Title': 'How to add a virtual page (created by a plugin) to the menu builder', 'ViewCount': 323, 'id': 31762458, 'user_AboutMe': '<p>Software Developer at <a href="https://nsgo.nl/" rel="nofollow noreferrer">NS Go</a>.</p>\n<p>We provide solutions to manage mobility and policies within big corporations.</p>\n<p>Codewars:</p>\n<p><img src="https://www.codewars.com/users/joelharkes/badges/large" alt="Code wars batch details" /></p>\n', 'user_AccountId': 1332185.0, 'user_DisplayName': 'Joel Harkes', 'user_Reputation': 10286.0, 'user_Views': 777.0, 'user_id': 1275832.0}}