HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Is there any way to pull post content through from Vanilla in a sub directory to main html homepage

I would like to show the latest posts from my vanilla forum on my websites homepage, but I can't see a plugin or article that shows how.

The forum is installed into a subdirectory of the main site, so I just need to call the posts through somehow. Is there a plugin anyone knows of that does this or can I do it with a php snippet or something?

«13

Comments

  • Jonathan WJonathan W Scranton, PA

    You could write some JavaScript to parse discussions.json. It will output the discussions you'd see on the discussion view, so just take the first element when you handle the request and format it to your needs.

    Example: http://yoursite.com/forum/discussions.json

    If you don't use pretty URLs you may have to put the index.php in there.

    Someone might have a better solution though :)

  • hgtonighthgtonight ∞ · New Moderator

    If you want a preparsed list of discussions, checkout latest post list: http://vanillaforums.org/addon/latestpostlist-plugin

    Add it to your Vanilla install, then request the module via AJAX //forums.example.com/module/latestpostlistmodule and style it however you want.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Ok first off, I don't know javascript so that's not going to be an option for me, but thank you for the suggestion.

    I have downloaded the "latestpostlist" plugin and installed it. That's where you lost me :P how do I call it into my website homepage? I don't understand the ajax bit.

  • Jonathan WJonathan W Scranton, PA
    edited April 2015

    The AJAX bit is also JavaScript. Don't be turned off by it - if you're new, you can probably figure it out quick enough with a tutorial from W3 Schools and just messing with a test HTML doc on your computer. Or better you can use something like jsfiddle which will give you real time results and let you share your code so others can help you. I'd also recommend grabbing some JQuery basics. It's JavaScript but with a framework on top that makes things much easier.

    Basically AJAX lets JavaScript make a request to a server and then receive a result. When we say make a call, we are talking about using a script to make that request. In this case the result will be the latest post you're looking for. You still need to use a script to display that on your page.

  • Jonathan WJonathan W Scranton, PA

    So I raved about how easy it would be to learn, then realized I only had a small idea of how I would do it myself. So curiosity won out and I came up with this:

    https://jsfiddle.net/xorith/4jco7nLz/11/

    Basically, the code will parse the JSON file and update the text contained within elements that have the "latest" class prefix. What that means is "latestLastName" will give you the username of the last person that commented on the discussion.

    You can use this tool to take a look at the JSON and see all the fields available. Replace the URL after the # with your own.
    http://jsonviewer.stack.hu/#http://testing.wurmly.com/discussions.json

    As for the code, just change $.latestDiscussion.url to your own JSON URL. Keep in mind that if you run that code on a host other than your forum, you will get an error. I had to add headers to my index.php to make the script work. I would never put those headers in a live install, as they give carte blanc to any AJAX requests coming from any other host.

    Let me know if you have any questions... I tried to write it as portable as possible. :)

  • Jonathan WJonathan W Scranton, PA

    It's too late to edit, but the link should be: http://jsfiddle.net/xorith/4jco7nLz/

  • Oh my lord I've just come out in a cold sweat!! This is going to take some learning! I've just started going through the javascript school as you suggested, I may have a solution by 2020 :proud:

    Still I am prepared to have a go. Any hints as to the relevant code snippets I need to focus on?

  • Ok so I've started writing a script to try and pull the table data out. I've established a connection with the database and I've written an sql query and attempted to output it. That's where I hit my first problem. I have pasted the code for the sql query below, can someone let me know if I've written it correctly please.

    $sql="SELECT * FROM Discussion ORDER BY DiscussionID DESC LIMIT 1";
    $result = $conn->query($sql);

    Discussion is the table in the db and DiscussionID is the unique ID. The tables have a prefix so should I be writing GDN_Discussion (I have already tried it with that)?

    Also, I have been using this code to try and display the results of the script but I keep getting "0 results". Any suggestions?

    if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
    echo "id: " . $row["DiscussionID"]. " - Name: " . $row["Name"]. " " . $row["Body"]. "
    ";
    }
    } else {
    echo "0 results";
    }

  • x00x00 MVP
    edited April 2015

    That looks like a recipe for cross site scripting Body is unsantised, it would be easy to inject code into your front page. Other than that there nothing wrong with your code as such, except you don't mention how you establish the connection to the database. It isn't really a question relevant to this forum, as you are circumventing the framework anyway, it is more a general php question.

    hgtonight and Jonathan W have shown you how to use the framework. Your method ignore stuf like access control. it just pulls raw data out of the database, you are also ordering by DiscussionID which isn't necessarily that useful.

    grep is your friend.

  • @x00 said:
    That looks like a recipe for cross site scripting Body is unsantised, it would be easy to inject code into your front page. Other than that there nothing wrong with your code as such, except you don't mention how you establish the connection to the database. It isn't really a question relevant to this forum, as you are circumventing the framework anyway, it is more a general php question.

    hgtonight and Jonathan W have shown you how to use the framework. Your method ignore stuf like access control. it just pulls raw data out of the database, you are also ordering by DiscussionID which isn't necessarily that useful.

    Ok this is my first attempt at php & javascripting so cut a guy a little slack eh rather than just making digs. If I manage to even remotely come close to working how to just pull the data through, then I'll concern myself with making it secure and pretty. And yes I do think it's relevant for this forum and I'm pretty sure I'm not the only person who wants to use snippets of content on webpages external to the forum itself.

  • @Jonathan W said:
    It's too late to edit, but the link should be: http://jsfiddle.net/xorith/4jco7nLz/

    OOps totally missed this post, soz. Just looking at it now =)

  • Where did I make digs? I gave you factual information.

    grep is your friend.

  • Jonathan, thank you for your efforts in helping me ;)

    I was figuring that if I just substituted your forum url for mine it would show the details from my site, but that doesn't happen, all the pulled through content just disappears. Should that happen?

  • 1st, 3rd, 4th & 5th lines. I perceive them to be unhelpful digs not exactly constructive, unlike Jonathan's and hgtonight.

  • @ahughes3 said:
    1st, 3rd, 4th & 5th lines. I perceive them to be unhelpful digs not exactly constructive, unlike Jonathan's and hgtonight.

    Well then you are unlikely to learn to do things properly if you can't take constructive criticism. Try not to be so sensitive.

    grep is your friend.

  • x00x00 MVP
    edited April 2015

    And yes I do think it's relevant for this forum and I'm pretty sure I'm not the only person who wants to use snippets of content on webpages external to the forum itself.

    People may help but it is a general php question, so you might get more information about that on a dedicated php forum.

    You you have shown work, which is good. Pulling raw data from the database can have its pitfalls, if there is a lot of post-processing required, you may be re-inventing the wheel. It is ok if it is public data that doesn’t need a lot post-processing, so long as query is not going to adversely affect performance.

    it is better than force-loading the framework which is a common mistake, that can add a lot of overhead.

    I think the ajax request is the most versatile, especially in the more dynamic web. Like I said it should respect permissions too, and in this case add other data like users.

    First I would learn about JSON, which is how the data is serialised. This is an example of the JSON API

    http://vanillaforums.org/discussions.json

    Then I would look at AJAX requests using jQuery which is a common library make JavaScript easier.

    grep is your friend.

  • Now that is a much more useful and helpful comment and much more constructive. I will try to be less sensitive but I can't promise.

    I thought it relevant here because I was assuming someone must have already done this specifically with Vanilla Forums plus I'm not a coder so this seemed the logical place to start with.

    As for the data, I only want to pull the two most recent posts to use in a snippet on my website's homepage. I don't need lots of data and so there shouldn't be much of an issue with processing.

    I am as clueless about AJAX as I am about PHP, JS & JQuery so they all look a nightmare to me right now ;)

    Jonathan has put some effort into creating a working example so I thought I should try that at least as it does the job. The suggestion from hgtonight; I installed the plugin he mentioned, but I don't know then how to "call it using AJAX".

    I've seen the JSON file for my site when Jonathan referred to it. I'll check out the AJAX requests part on W3Schools and see if I can make sense of it.

  • I thought it relevant here because I was assuming someone must have already done this specifically with Vanilla Forums plus I'm not a coder so this seemed the logical place to start with.

    Sure, it is not a bad idea. Just that the actual queries used for the forum, it a bit more complex. So you won't get all the information from one table.

    The benefit of going through he framework is you get all the processing from the model, thrown in, saves you having to replicate it or worry about it.

    However if you are very economical with want you need you method could work too. Do you care about the user name, categories, and respecting permissions?

    grep is your friend.

  • hgtonighthgtonight ∞ · New Moderator

    I have attached some example code that should be relatively easy to follow. The key knowledge is:

    1. Latest Post List must be enabled on your Vanilla forum
    2. js/ajax.js contains the pertinent JS code
    3. This requires the request to be from the Same Origin

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • @Jonathan W I would make any timer conditional of a successful get, otherwise you create race condition.

    I would also use jQuery rather than $ outside of ready. I know you can get away with it often but pass $ as the parameter of the ready function, it is a lot clearer.

    grep is your friend.

Sign In or Register to comment.