Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Merge existing plugin content to this plugin WordPress

  • SOLVED

I have found the following code / plugin which looks like it provides the exact functionality I need but I need to know how I can change the following to work within a plugin I am currently writing.

Ideally I want myplugin.php to be able to set the following variables in the plugin below based on my plugin's existing output:
e.g.

var $page_title = $someVariablefrommyplugin;
var $page_slug = $someVariablefrommyplugin2;
$post->post_content = SsomeVariableThatContainsMyPlugin'sOutput


I can post some of my plugin's code too if needed?

My first thoughts are that it might be best to add the following FakePage class to myplugin.php somewhere in order to load the different required variables dynamically but I am a little lost!

Thanks



<?php
/**
* Plugin Name: Fake Page Plugin 2
* Plugin URI: http://scott.sherrillmix.com/blog/blogger/creating-a-better-fake-post-with-a-wordpress-plugin/
* Description: Creates a fake page without a 404 error (based on <a href="http://headzoo.com/tutorials/wordpress-creating-a-fake-post-with-a-plugin">Sean Hickey's Fake Plugin Page</a>)
* Author: Scott Sherrill-Mix
* Author URI: http://scott.sherrillmix.com/blog/
* Version: 1.1
*/

class FakePage
{
/**
* The slug for the fake post. This is the URL for your plugin, like:
* http://site.com/about-me or http://site.com/?page_id=about-me
* @var string
*/
var $page_slug = 'about-me';

/**
* The title for your fake post.
* @var string
*/
var $page_title = 'About Me';

/**
* Allow pings?
* @var string
*/
var $ping_status = 'open';

/**
* Class constructor
*/
function FakePage()
{
/**
* We'll wait til WordPress has looked for posts, and then
* check to see if the requested url matches our target.
*/
add_filter('the_posts',array(&$this,'detectPost'));
}


/**
* Called by the 'detectPost' action
*/
function createPost()
{

/**
* What we are going to do here, is create a fake post. A post
* that doesn't actually exist. We're gonna fill it up with
* whatever values you want. The content of the post will be
* the output from your plugin.
*/

/**
* Create a fake post.
*/
$post = new stdClass;

/**
* The author ID for the post. Usually 1 is the sys admin. Your
* plugin can find out the real author ID without any trouble.
*/
$post->post_author = 1;

/**
* The safe name for the post. This is the post slug.
*/
$post->post_name = $this->page_slug;

/**
* Not sure if this is even important. But gonna fill it up anyway.
*/
$post->guid = get_bloginfo('wpurl') . '/' . $this->page_slug;


/**
* The title of the page.
*/
$post->post_title = $this->page_title;

/**
* This is the content of the post. This is where the output of
* your plugin should go. Just store the output from all your
* plugin function calls, and put the output into this var.
*/
$post->post_content = $this->getContent();

/**
* Fake post ID to prevent WP from trying to show comments for
* a post that doesn't really exist.
*/
$post->ID = -1;

/**
* Static means a page, not a post.
*/
$post->post_status = 'static';

/**
* Turning off comments for the post.
*/
$post->comment_status = 'closed';

/**
* Let people ping the post? Probably doesn't matter since
* comments are turned off, so not sure if WP would even
* show the pings.
*/
$post->ping_status = $this->ping_status;

$post->comment_count = 0;

/**
* You can pretty much fill these up with anything you want. The
* current date is fine. It's a fake post right? Maybe the date
* the plugin was activated?
*/
$post->post_date = current_time('mysql');
$post->post_date_gmt = current_time('mysql', 1);

return($post);
}

function getContent()
{
return '<p>Hi there! You are viewing my fake post!</p>';
}

function detectPost($posts){
global $wp;
global $wp_query;
/**
* Check if the requested page matches our target
*/
if (strtolower($wp->request) == strtolower($this->page_slug) || $wp->query_vars['page_id'] == $this->page_slug){
//Add the fake post
$posts=NULL;
$posts[]=$this->createPost();

/**
* Trick wp_query into thinking this is a page (necessary for wp_title() at least)
* Not sure if it's cheating or not to modify global variables in a filter
* but it appears to work and the codex doesn't directly say not to.
*/
$wp_query->is_page = true;
//Not sure if this one is necessary but might as well set it like a true page
$wp_query->is_singular = true;
$wp_query->is_home = false;
$wp_query->is_archive = false;
$wp_query->is_category = false;
//Longer permalink structures may not match the fake post slug and cause a 404 error so we catch the error here
unset($wp_query->query["error"]);
$wp_query->query_vars["error"]="";
$wp_query->is_404=false;

}
return $posts;
}
}

/**
* Create an instance of our class.
*/
new FakePage;
?>

Answers (2)

2010-01-27

Julio Protzek answers:

I changed the plugin code a bit. So you can use it this way:

new FakePage(array(
'slug'=> 'about-us',
'title'=> 'About Us',
'content'=> '<p>Hi there! You are viewing my fake post!</p>'
));

new FakePage(array(
'slug'=> 'clients',
'title'=> 'Clients',
'content'=> '<p>Our client list:</p>'
));

You can copy the class to your plugin file if you want.

Take a look:
http://pastium.org/view/24100932568ac6e264500b66681ff7ec

I cleaned up the comments so you can see the code.

I changed the following lines:
7 - To store the content
10 to 14 - So you can configure everything once.
37 - So the class can use the stored content.
43 - To make the code easier to read.
59 to 62 - Related to 43

PS. I found a bug in my code :) Everything is ok now


dave comments:

This seems to work exactyl as I want. Thank you very much!

2010-01-26

Max answers:

You should:
- add a new member variable $your_output in the class FakePage
- call your code at the top of the detectPost() method.
- store the output in order to set the values of

$post->page_title
$post->page_slug
$this->your_output

- change the getContent() method to


function getContent()
{

return $this->your_output;

}


Let us know if this solution works for you :)

Regards


dave comments:

Few questions...

I put the following as the first thing within the class FakePage yeah?

var $your_output;

I then call my code here yeah?


function detectPost($posts){
global $wp;
global $wp_query;

/* Some code to call my existing plugin? */
/* Can this be an include like below? */

include('wp-content/plugins/get_myplugin_results.php');


How do I and what am I storing exactly when you sayt?

"store the output in order to set the values of"

Thanks!


Max comments:

We need to see your plugin code and how it works to give you more precise information.
The solution of Julio is a good starting point for integrating your code.