The website is for sale. Check here for more information.
This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.
Random display
This is an idea I've been tossing around for my site... Since we're a multi-genre writing site, it'd be nice if there was a way for me to add various plot descriptions to the database and then have PHP code randomly pick and display one in our sidebar.
So I've been working on getting this plugin complete. But before I really spent a lot of time getting the admin side working, I wanted to ensure the user side was at least functioning. (Since for now I can manage this by adding/editing plots manually via SQL without needing to have the backend parts done.)
So I've installed Frostschutz's Hooks plugin, but for the life of me I cannot figure out how to get my code to display properly.
I have a feeling there's an error somewhere that I'm just not seeing. (I readily admit I'm rather new to PHP and not exactly skilled.)
Here's the code I'm trying to use:
// necessary to edit the templates or pull from the database
global $mybb, $templates, $db;
//Tells the system where to hook in and what function to call
$plugins->add_hook("pre_output_page", "rndm_plot_placement");
function rndm_plot_placement(&$page)
//sets the type of query to run on the database.
$query = $db->simple_select('rndm_plot', '*', 'disabled=0', array('order_by' => 'RAND()', 'limit' => 1));
// disabled=0 means this part hasn't been hidden.
$plot = $db->fetch_array($query);
if ($plot)
$page = str_replace('rndm_plot_chosen', $plot['theplot'], $page);
'rndm_plot_chosen' is the section in templates to get replaced.
'theplot' is the actual html to get entered into the 'rndm_plot_chosen' place.
// no plots visible so get rid of the code
$page = str_replace('rndm_plot_chosen', '', $page);
In the database I have a table called "mybb_rndm_plot" with 3 entries. So I know contents are there. (I've tried calling the plugin with the prefix mybb_ and without but neither way makes any difference. Though it's my understanding that without prefix should work since the $db should add that automatically as part of it's function.)
I'm not getting any syntax errors so I don't think that's an issue. But nothing is happening at all (it's not replacing the line in the templates it's supposed to be but it's not throwing back any error messages either) so I have no idea what's going on. 
Anyone see where I've made the mistakes? Or places where it's not working? Any help would be appreciated.
(Note - I don't have the forum placed online, it's a private development board running on my local server only. But if you needed to see it live, I could set it up on my testing board and PM the link and a set of access credentials to you.)
Have you tried:
PHP Code:
die("test contents:".htmlspecialchars_uni($page)); 
at the beginning of the function to see its contents?
To download our paid plugins and receive support you must be a paid subscriber. Click here for more information.
I just tried it now and there doesn't appear to be anything.
Can you please post your plugin?

PS. I'm not understanding exactly why you're using the Hooks plugin.
To download our paid plugins and receive support you must be a paid subscriber. Click here for more information.
I was using the Hooks plugin to (attempt to) test if the active part of the code was actually doing anything. (The part that actually connects to the database and pulls the entry to be displayed then displays it.)
Since if the active part wasn't working then there wouldn't be much point in wasting time getting the admin backend stuff all done up. (I figured that admin back-end stuff could largely wait since, for now at least, I could control things from PHPMyAdmin. Adding/updating/removing entries, setting which ones are enabled, etc... And that the admin part would largely just be a convenience for me/other admins down the line.)

Here's the plugin. attachment removed
(As a heads up... it's far from complete and there are sections missing. Things I intend to add like a check for exempt groups and the admin side. So it's pretty barebones for now...
Also one thing I haven't added to the plugin, and purposefully so, is the auto-insertion of the "{rndm_plot_chosen}" part into the templates. I didn't want to add that because I wanted to be able to define where on the index this part gets added. )
A quick glance tells me you probably forgot to enable the 'randomplot_disabled' setting?
To download our paid plugins and receive support you must be a paid subscriber. Click here for more information.
I figured out what was going on... Sort of.
I was having trouble getting my own plugin to work because I'd misnamed things. (I was using "rndm_plot" internally when I should have been using "randomplot". Since it wasn't the same as the filename it was being ignored by MyBB. )
Since I wasn't able to test it that way, I was trying to use Hooks to test the base code. But because I don't understand the Hooks plugin that well, I was likely misunderstanding/misusing it. (I'm still not quite sure why it wasn't working, but oh well.)

Once I fixed the name issue MyBB allowed me to test from the plugin itself and suddenly everything started working properly. (Side note - To anyone looking to code plugins... don't read the documentation on how to do this at 4AM. You will miss things. Important things at that. And then you will wonder why it's not working properly.)

The randomplot_disable setting wasn't the issue, though it worked after I fixed the main issue. (But I've updated it to cover event of the plugin being disabled but not uninstalled so it doesn't display the {randomplot_chosen} aspect.)

But now that I know the front end aspect works, I'm going to start forging ahead into the ACP side to fully flesh it out and get it functioning properly (without requiring PHPMyAdmin access).
I guess I overlooked that while looking at the code Tongue

Good luck! You should probably check out my PDF about hooks, it might help you.
To download our paid plugins and receive support you must be a paid subscriber. Click here for more information.
I've seen a version that was posted on MyBB's forum.
Do you have plans to release the 4th and 5th parts? Since they could be pretty helpful too. (Especially the 4th part dealing with the database since wrong moves there can cause serious issues. So it's be nice to know what to avoid doing, proper safety/security measures like cleaning anything to be entered into the database, etc...)
You also have them here:

I plan on finishing them yes but probably not within the next 30 days. I'm very very very busy and I prefer to spend my time developing MyBB rather than creating these tutorials (though they're definitely helpful IMO for new plugin developers, I believe they can wait 30 days Tongue)
To download our paid plugins and receive support you must be a paid subscriber. Click here for more information.

Users browsing this thread: 1 Guest(s)

About MyBB-Plugins

Plug Yourself into the best MyBB AddOn.

Copyright © 2009-2021 MyBB-Plugins Forums.


  • 51,000+ Posts
  • 19,000+ Members
  • 3,600+ Threads

Impressed? For sure
Check Full Stats here