We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49577
    • 12 Posts
    Hi Guys,

    I am in the middle of reworking a personal website of mine into modx for easier management of it's content.

    What I am struggling with is using modx code on external pages.

    I have been researching this for a few weeks but have come up empty handed.

    I have read various forum posts, blogs and articles relating to the modx api to do this but each time I try I receive script errors or files being referenced which just flat out dont exist in my revolution install.

    Given these issues I wonder if there is no documentation for the current Revolution API and what I have read so far is based off the older Evolution which is why I am getting errors.

    Is there a guide anywhere to help me accomplish this?

    Can I post the articles I have found so far and the problems I am encountering with each one?

    The reason for this is to put my modx header and menu on my external forum. I am doing this at the moment with custom code in my forums header but this is not dynamic and any changes in modx also need to be reflected in the forums header template also.

    Please let me know if I need to provide more information. I am not a coding guru - This may be part of my struggles...

    Thank you in advance.

    This question has been answered by multiple community members. See the first response.

    • http://rtfm.modx.com/revolution/2.x/developing-in-modx/other-development-resources/loading-modx-externally

      Using the API externally isn't for the coding neophyte, but if you can include files and instantiate objects, then you shouldn't have a problem.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 49577
        • 12 Posts
        Thank you for the reply Sottwell,

        I have read that article previously but had no luck. I have read it again and have placed that code into a PHP file and adjusted the paths to suit. No errors! Im not sure what I was doing wrong so many times before.

        I have then used a php include in my forums header to include this file but I really am struggling with what to do next.

        Obviously not knowing much php or html code is my downfall here as it is all very confusing.

        I guess what I am asking is if anyone can point me in the right direction of the code I need to enter into my forums html header template to pull the chunk from modx and in turn display the header and navigation.

        in my html header template for the forum I have this code to pull the chunk in where I want it but this results in a blank page when adding the second line... These php tags are required for the forum to know to process this code as php.

        <!-- PHP --> include("./modxapi.php"); <!-- ENDPHP -->
        
        <!-- PHP --> $modx->getChunk($header); <!-- ENDPHP -->


        I apologise if I this forum is meant for purely modx support as I am asking more of a coding question...
        • Not knowing how your forum processes code, it's hard to say. But I would think that the modxapi.php file would have the getChunk API call in it, after the code to create the new $modx object.

          I suspect that your problem is an issue of scope. The $modx object is a variable, and variables are generally only available inside of the function that creates them. Other functions have no access to them. For example, if you have a function definition in a snippet or plugin in MODX, you must declare the $modx variable as global:
          function myFunction() {
          global $modx;
          ...
          }

          [ed. note: sottwell last edited this post 9 years, 4 months ago.]
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 49577
            • 12 Posts
            I have picked this up again after a break from it as I was getting very frustraited.

            Still cannot get this working, this is the last piece of the puzzle I need.

            Can you point me in the right direction or offer me any examples of this I can look at?
            • discuss.answer
              • 3749
              • 24,544 Posts
              If you want to display a chunk that's stored in the MODX database, once you instantiate MODX, you can do something like this:


              $chunk = $modx->getChunk('ChunkName');
              echo $chunk;


              You might find this helpful: http://bobsguides.com/blog.html/2013/07/13/using-modx-outside-of-modx/.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
              • discuss.answer
                • 49577
                • 12 Posts
                Yes I have read though that blog post many times and I do have the code you suggested currently - no luck with that. No errors, just nothing being pulled through. I am missing something somewhere.

                I dont know enough code-wise to troubleshoot this issue.

                I'll keep prodding at it I guess.

                EDIT:

                I have made some progress... I now have a blank page with the words "No User" so now it is definately pulling information from MODX!. Has wiped out all forum content though.. I gather the code is not executing any further due to a error in the modx stuff.

                To rid this message I changed the code in my included php file "$modx->initialize('web');" I changed the user "web" to a user I have created in modx but this has resulted in a blank page.. hmm...

                EDIT2:

                I should have read some of this code more carfully.. I entered a username in the corrent place and changed the above back to web. I now have the text from my header displaying but no images or styling.. also the paths on the forum have been messed up.. Example:

                The file path for the forum is ("Web Index"/phpbb), modx is stored alongside such as ("web Index"/modx).
                So what is happening now is the forum has no images and any links are not pointing to the correct location anymore for an example.. clicking a forum post takes me to something like: "/modx/viewforum.php?f=2" should be pointing to "/phpbb/viewforum.php?f=2"

                I think I am well out of depth now unless this is a common noob error and easily fixed. I am not sure.

                EDIT3:

                Removing "<base href="[[++site_url]]" />" from this particular chunk i am pulling in has resolved the file paths issue but I still have no styling just plain text.. hmm I bet it's because of the location of the CSS refrenced in modx... argh

                EDIT4:

                I have accomplished it!

                I had to hard code the path of my CSS as the relative path was obviously different for modx and phpbb.

                One final question I have... I have a CSS menu using wayfinder which highlights the current page, how can I pass this information through to modx so it think the static "forum" page is being visited so this link will light up? [ed. note: bonkas last edited this post 9 years, 2 months ago.]
                  • 3749
                  • 24,544 Posts
                  If you know where the CSS file(s) are, you can just add this for each one (assuming that your page has proper HTML, with a head and body section).

                  $modx->regClientCSS('url/of/css/file');




                  [ed. note: BobRay last edited this post 9 years, 2 months ago.]
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 49577
                    • 12 Posts
                    Quote from: BobRay at Feb 19, 2015, 05:25 AM
                    If you know where the CSS file(s) are, you can just add this for each one (assuming that your page has proper HTML, with a head and body section).

                    $modx->regClientCSS('path/to/css/file');





                    Wow thanks bob that is a much better way.

                    I have modified my post above also.
                      • 3749
                      • 24,544 Posts
                      One final question I have... I have a CSS menu using wayfinder which highlights the current page, how can I pass this information through to modx so it think the static "forum" page is being visited so this link will light up?

                      I think this will do it. In the part where you instantiate MODX, set $modx->resource to that page using the page ID (say it's 12).

                      $modx->resource =& $modx->getObject('modResource', 12);
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting