TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

Members
Stats
  • Total Posts: 196,026
  • Total Topics: 21,333
  • Online today: 916
  • Online ever: 8,223 (February 19, 2025, 04:35:35 AM)
Users Online
  • Users: 0
  • Guests: 249
  • Total: 249

[Block] code to display a wordpress post in tinyportal

Started by PeterLawrence, November 04, 2008, 12:30:11 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

PeterLawrence

I wanted to display a Wordpress post inside my SMF forum so I developed this little TP code snippet. Here it is just in case it may be of use to someone else.

Just paste the following code into a TP PHP block and set $DefaultPost_ID to the post ID you wish to display. In the script I assume Wordpress is using the same database as SMF. If this is not the case then you'll need to assign the variable $wp_db_name. Also, if you're not using the default wordpress db prefix this needs to be specified via the variable $wp_db_prefix.

I've also included a limited wordpress post navigation via the ability to specify a page_id parameter. If not required, then simply delete the if block which contains the $_GET.

//
// Block code to display wordpress posts in Tinyportal
//

// This is the wordpress post to be displayed.
$DefaultPost_ID='219';

// Set $wp_db_name to your wordpress database.
// Here we assume that it's using the same db as smf.
global $db_name;
$wp_db_name=$db_name;

// Set wp_db_prefix to your wordpress db prefix, the default is 'wp_'.
$wp_db_prefix='wp_';

// try to connect to database.
$attempt = @mysql_select_db($wp_db_name);
if ($attempt != false)
{
$Post_ID=$DefaultPost_ID;
if ($_GET['page_id'])  // see if the user has specified a different post to display.
{
// clean input.
$Post_ID= (int) $_GET['page_id'];
}

// see if we can find the post.
$request = @mysql_query("
SELECT post_content,post_title
FROM ${wp_db_prefix}posts WHERE ID=$Post_ID");
if ($request)
{
// display the post
while ($row = @mysql_fetch_row($request))
{
echo $row[1],'<br \>'; // output post title
echo htmlspecialchars_decode($row[0]); // output post contents
}
@mysql_free_result($request);
}
else
{
echo 'Invalid query: ', mysql_error(),' <br>';
}
}
else
{
echo "<p> Can't connect to database $wp_db_name</p> ";
}

FERNSIDEâ„¢

Man this looks very nice, and will definitely be of benefit to people.
Thank you for the share, and the great explanations/guides :)

PeterLawrence

Just spotted a possible SQL injection problem in the above code...
Replacing the line..

$Post_ID=addslashes($_GET['page_id']);

with

$Post_ID= (int) $_GET['page_id'];

should fix the problem.

G6Cad

Please, update the code in your first topic, it's a lot easier for all of us to get the correct code if the changes made through the thread also gets added to the very first post.
Thank you  :up:

PeterLawrence

Just had a couple of goes at updating my original code, but keep getting the following message..

Quote
Database Error
Please try again. If you come back to this error screen, report the error to an administrator.

JPDeni

Are you sure that this is right:


${wp_db_prefix}


If I was writing it, I would use


{$wp_db_prefix}

PeterLawrence


JPDeni

That was the only thing that jumped out at me.

Another possibility is the opening of the database. For testing purposes, try removing


$attempt = @mysql_select_db($wp_db_name);


and the "if" structures that depend on it. See if that fixes your problem.

It does appear that your wp tables are in the same db as your smf tables, so it should work. I'm kinda thinking that you're going to have a problem if you try to switch databases. At the very least, you're going to have to re-open the smf database because only one can be open at a time.

PeterLawrence

#8
Sorry, a little bit of confusion here.
The Error message above is from this forum when I try to update the original posting to incorporate the code changes as requested too by G6.
However, it's now working and I've updated the code above as requested.

vdubbia

#9
this would be perfect if it grabbed the latest post in a specific category... any way to accomplish this? 

The way I understand how this currently works is that you have to manually indicate in TP what post you want to appear, correct?

This website is proudly hosted on Crocweb Cloud Website Hosting.