05-18-2014, 07:22 AM
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:
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.)
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:
Code:
// 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.
*/
}
else
{
// no plots visible so get rid of the code
$page = str_replace('rndm_plot_chosen', '', $page);
}
}
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.)