How do I create a new page with the plugin I’m building
-
Hi,
I’m currently in the process of buidling my first own WordPress plugin. It’s a fairly simple plugin that will be used by a band that wants to have a simple tool to add show dates and have them presented on their WordPress site.
I’ve built the admin interface, and managed to make the plugin create the database on activation. Further, I’ve come so far as having the plugin inserting the data posted on the admin page correctly into the database table it created in the first place.
What’s left is figuring out how to make the plugin generate a “show dates”-page (which content will loop through the DB and present the data found), and add a link to it in the main menu.
I’ve searched all over the internets for some sort of hint on how to achieve the above, but haven’t found anything (which kinda surprised me, since it should be quite common right?). Can anyone give me some sort of crashcourse, or point me in the right direction? Am I wrong to assume that there’s some sort of built-in action to do this (something like a “add_pages_page()”, but for the front end menu or something)?
Grateful for any kind of advice from the WP community.
Thanks in advance,
your humble padawan
// Erik
-
Really, noone?
This is something I just did too in the past few days. This is my first venture into the WordPress plugin world and I based the basics on:
https://corpocrat.com/2009/12/27/tutorial-how-to-write-a-wordpress-plugin/
In the main plugin file:
/* Runs when plugin is activated */ register_activation_hook(__FILE__,'my_plugin_install'); /* Runs on plugin deactivation*/ register_deactivation_hook( __FILE__, 'my_plugin_remove' );
Then:
function my_plugin_install() { global $wpdb; $the_page_title = 'Whatever You Want'; $the_page_name = 'whatever-you-want'; // the menu entry... delete_option("my_plugin_page_title"); add_option("my_plugin_page_title", $the_page_title, '', 'yes'); // the slug... delete_option("my_plugin_page_name"); add_option("my_plugin_page_name", $the_page_name, '', 'yes'); // the id... delete_option("my_plugin_page_id"); add_option("my_plugin_page_id", '0', '', 'yes'); $the_page = get_page_by_title( $the_page_title ); if ( ! $the_page ) { // Create post object $_p = array(); $_p['post_title'] = $the_page_title; $_p['post_content'] = "This text may be overridden by the plugin. You shouldn't edit it."; $_p['post_status'] = 'publish'; $_p['post_type'] = 'page'; $_p['comment_status'] = 'closed'; $_p['ping_status'] = 'closed'; $_p['post_category'] = array(1); // the default 'Uncatrgorised' // Insert the post into the database $the_page_id = wp_insert_post( $_p ); } else { // the plugin may have been previously active and the page may just be trashed... $the_page_id = $the_page->ID; //make sure the page is not trashed... $the_page->post_status = 'publish'; $the_page_id = wp_update_post( $the_page ); } delete_option( 'my_plugin_page_id' ); add_option( 'my_plugin_page_id', $the_page_id ); } function my_plugin_remove() { global $wpdb; $the_page_title = get_option( "my_plugin_page_title" ); $the_page_name = get_option( "my_plugin_page_name" ); // the id of our page... $the_page_id = get_option( 'my_plugin_page_id' ); if( $the_page_id ) { wp_delete_post( $the_page_id ); // this will trash, not delete } delete_option("my_plugin_page_title"); delete_option("my_plugin_page_name"); delete_option("my_plugin_page_id"); }
I then sniff my page by hooking into the ‘parse_query’ filter…
`
function my_plugin_query_parser( $q ) {$the_page_name = get_option( “my_plugin_page_name” );
$the_page_id = get_option( ‘my_plugin_page_id’ );$qv = $q->query_vars;
// have we NOT used permalinks…?
if( !$q->did_permalink AND ( isset( $q->query_vars[‘page_id’] ) ) AND ( intval($q->query_vars[‘page_id’]) == $the_page_id ) ) {$q->set(‘my_plugin_page_is_called’, TRUE );
return $q;}
elseif( isset( $q->query_vars[‘pagename’] ) AND ( ($q->query_vars[‘pagename’] == $the_page_name) OR ($_pos_found = strpos($q->query_vars[‘pagename’],$the_page_name.’/’) === 0) ) ) {$q->set(‘my_plugin_page_is_called’, TRUE );
return $q;}
else {$q->set(‘my_plugin_page_is_called’, FALSE );
return $q;}
}
add_filter( ‘parse_query’, ‘my_plugin_query_parser’ );(Sorry, here is the properly formatted code – wouldn’t a POST preview function be great?!)
By the way: once my plugin page is flagged in the $wp_query object, I then use the ‘the_posts’ filter to see if $wp_query->get(‘my_plugin_page_is_called’) is set. I can then override:
$posts[0]->post_title and $posts[0]->post_content with my own content.function my_plugin_query_parser( $q ) { $the_page_name = get_option( "my_plugin_page_name" ); $the_page_id = get_option( 'my_plugin_page_id' ); $qv = $q->query_vars; // have we NOT used permalinks...? if( !$q->did_permalink AND ( isset( $q->query_vars['page_id'] ) ) AND ( intval($q->query_vars['page_id']) == $the_page_id ) ) { $q->set('my_plugin_page_is_called', TRUE ); return $q; } elseif( isset( $q->query_vars['pagename'] ) AND ( ($q->query_vars['pagename'] == $the_page_name) OR ($_pos_found = strpos($q->query_vars['pagename'],$the_page_name.'/') === 0) ) ) { $q->set('my_plugin_page_is_called', TRUE ); return $q; } else { $q->set('my_plugin_page_is_called', FALSE ); return $q; } } add_filter( 'parse_query', 'my_plugin_query_parser' );
It all works fine, but I am also wondering if this is the ‘correct’ way of doing things.
Francis.
Hi Fcrossen
Can you post your coding logic for the part oyu entioned here:
$posts[0]->post_title and $posts[0]->post_content with my own content.
I am stuck with something but think you have solved this already!
Many thanks
@andym4e: Sorry – didn’t see your post until now.
I came across a funny thing where $q->did_permalink was not being set when they were actually in use. I don’t know if it is a bug or a ‘feature’ – I didn’t have time to investigate.
Here’s what I do now:
And here is the properly formatted code!
if( isset( $q->query_vars['page_id'] ) AND ( intval($q->query_vars['page_id']) == $the_page_id ) ) { // My page has been called - NO permalinks } elseif( isset( $q->query_vars['pagename'] ) AND ( ($q->query_vars['pagename'] == $the_page_name) OR ($_pos_found = strpos($q->query_vars['pagename'],$the_page_name.'/') === 0) ) ) { // My page has been called - with permalinks } else { // Just a normal WordPress page }
function my_plugin_page_filter( $posts ) { global $wp_query; if( $wp_query->get('my_plugin_page_is_called') ) { $posts[0]->post_title = 'The Page Title'; $posts[0]->post_content = 'The page contents.'; } return $posts; } add_filter( 'the_posts', 'my_plugin_page_filter' );
Hi there!
Thanks for the great tips!
Is there any practical way to hide page appearing in admin dashboards page listing (located in /wp-admin/edit-pages.php)?
Thanks.
I can’t think of a way to prevent the page from appearing.
In fairness you don’t need a page. I did this so my page would appear in the WordPress menu system. You could sniff out the request and create a blank post with your own content and title.
I also had a comment from R’phael Spindel suggesting I look at WP_Rewrite. That could accomplish the same thing.
Indeed, I can create one on the fly. I didn’t came a cross with that idea. Good advice, thanks.
I’m creating my very first plugin to get familiar with the WordPress. Nothing fancy, just simple guestbook for myself. But you got to start somewhere, you know ??
I ended up using shortcodes.
Hi there,
I used this topic for creating my own plugin. Everything works so far, but when i go to the main page on my sandbox.. i see the hello world post. When i click on it, i am in the ‘hello world’ post, but the title and the content has been replaced by the plugin page post.
If you download this code and put it as memberlist.php in your plugin folder of your sandbox you will see what i mean.
This is really weird. This is the code, i know it is not documented very well, but the class name says enough. I want to create a ‘memberlist’ page where i output all the members in a (paged) list. I start documenting the class when everything works.
<?php /* Plugin Name: Mallona Memberlist Description: ~ Version: 1.0 Author: Dominique de Graaff Author URI: mailto:[email protected] */ if (!class_exists('Memberlist_Plugin')) { class Memberlist_Plugin { public $_name; public $page_title; public $page_name; public $page_id; public function __construct() { $this->_name = 'memberlist'; $this->page_title = 'Memberlist'; $this->page_name = $this->_name; $this->page_id = '0'; register_activation_hook(__FILE__, array($this, 'activate')); register_deactivation_hook(__FILE__, array($this, 'deactivate')); register_uninstall_hook(__FILE__, array($this, 'uninstall')); add_filter('parse_query', array($this, 'query_parser')); add_filter('the_posts', array($this, 'page_filter')); } public function activate() { global $wpdb; delete_option($this->_name.'_page_title'); add_option($this->_name.'_page_title', $this->page_title, '', 'yes'); delete_option($this->_name.'_page_name'); add_option($this->_name.'_page_name', $this->page_name, '', 'yes'); delete_option($this->_name.'_page_id'); add_option($this->_name.'_page_id', $this->page_id, '', 'yes'); $the_page = get_page_by_title($this->page_title); if (!$the_page) { // Create post object $_p = array(); $_p['post_title'] = $this->page_title; $_p['post_content'] = "This text may be overridden by the plugin. You shouldn't edit it."; $_p['post_status'] = 'publish'; $_p['post_type'] = 'page'; $_p['comment_status'] = 'closed'; $_p['ping_status'] = 'closed'; $_p['post_category'] = array(1); // the default 'Uncatrgorised' // Insert the post into the database $this->page_id = wp_insert_post($_p); } else { // the plugin may have been previously active and the page may just be trashed... $this->page_id = $the_page->ID; //make sure the page is not trashed... $the_page->post_status = 'publish'; $this->page_id = wp_update_post($the_page); } delete_option($this->_name.'_page_id'); add_option($this->_name.'_page_id', $this->page_id); } public function deactivate() { $this->deletePage(); $this->deleteOptions(); } public function uninstall() { $this->deletePage(true); $this->deleteOptions(); } public function query_parser($q) { if(isset($q->query_vars['page_id']) AND (intval($q->query_vars['page_id']) == $this->page_id )) { $q->set($this->_name.'_page_is_called', true); } elseif(isset($q->query_vars['pagename']) AND (($q->query_vars['pagename'] == $this->page_name) OR ($_pos_found = strpos($q->query_vars['pagename'],$this->page_name.'/') === 0))) { $q->set($this->_name.'_page_is_called', true); } else { $q->set($this->_name.'_page_is_called', false); } } function page_filter($posts) { global $wp_query; if($wp_query->get($this->_name.'_page_is_called')) { $posts[0]->post_title = __('Memberlist'); $posts[0]->post_content = 'The contents'; } return $posts; } private function deletePage($hard = false) { global $wpdb; $id = get_option($this->_name.'_page_id'); if($id && $hard == true) wp_delete_post($id, true); elseif($id && $hard == false) wp_delete_post($id); } private function deleteOptions() { delete_option($this->_name.'_page_title'); delete_option($this->_name.'_page_name'); delete_option($this->_name.'_page_id'); } } } $memberlist = new Memberlist_Plugin(); ?>
And what do you guys mean by ‘not’ using a page, but just a blank post. Do you have a little example for me that explains this?
By the way, i’m on 3.0 now (if that does matter)
Thanks!
Your problem is on line 15… if you are using permalinks then page_id is 0 and isset returns true.
Try:
if(!empty($q->query_vars['page_id']) AND (intval($q->query_vars['page_id']) == $this->page_id ))
- The topic ‘How do I create a new page with the plugin I’m building’ is closed to new replies.