We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44599
    • 4 Posts
    Hi, I was wondering if anyone could assist me with a problem that I am having.

    So I'm trying to automatically post from my blog to my facebook page. I have done the php separately and it works fine, so now I am thinking about what has to be done to get it incorporated into MODx.

    The functionality that I am looking for is, on creation of a new article (i.e Blog Post) some event must be triggered by which a snippet or some other piece of code is called, that will trigger the posting to facebook.

    All of this will be done on the back end, that is to say, the creation of the new post together with whatever images/links/etc will be done from the manager, and once it's complete, and saved the event should be triggered calling the code that will make the post to facebook.

    Any help will be greatly appreciated.

    - Ed
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      If you are using the Articles package for this, the plugin would work on the onDocFormSave event, and check to make sure it's a new document and that its class_key is Article (or whatever the class_key for Articles is), then run your code.
        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
        • 3749
        • 24,544 Posts
        I would be interested in seeing your code for posting to Facebook and I think many others would be too.


        Your plugin would be something like this (connected to OnDocFormSave):

        /* Make sure it's a new doc */
        if ($mode !== modSystemEvent::MODE_NEW) return; 
        
        /* Make sure it's an Article */
        if ($resource->get('class_key') !== 'Article') return;
        
        /* your code here */
        
        return '';
          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
          • 44599
          • 4 Posts
          Hi Sottwell, BobRay,

          Thank you both for your responses, I will try that out and see if it works.

          To get the posts to facebook going through php you're going to need the Facebook Developers SDK, you can follow this tutorial
          http://www.pontikis.net/blog/auto_post_on_facebook_with_php, it's pretty straightforward.


          <?php
          require_once("./facebook-php-sdk/src/facebook.php"); // set the right path
           
          $config = array();
          $config['appId'] = 'YOUR_APP_ID';
          $config['secret'] = 'YOUR_APP_SECRET';
          $config['fileUpload'] = false; // optional
          $fb = new Facebook($config);
           
          $params = array(
            // this is the main access token (facebook profile)
            "access_token" => "YOUR_ACCESS_TOKEN",
            "message" => "MESSAGE",
            "link" => "http://www.something-random.com/",
            "picture" => "http://something-random.com/a.jpg",
            "name" => "TITLE_OF_POST",
            "caption" => "www.something-random.com",
            "description" => "DESCRIPTION"
          );
           
          try {
            $ret = $fb->api('/me/feed', 'POST', $params);
            echo 'Successfully posted to Facebook Personal Profile';
          } catch(Exception $e) {
            echo $e->getMessage();
          }
          ?>
          



          - Ed
            • 3749
            • 24,544 Posts
            Thanks Ed, does that post to the profile, or the timeline?
              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
              • 44599
              • 4 Posts
              It depends on the access token, I think if you get and use the access token for your personal profile (as I was doing), the posts go to the timeline of your profile. If you have a business page or a fan page and use either of their access tokens, it should post to the respective page. As I have neither of those I am unable to confirm but you can try it out and see.
                • 3749
                • 24,544 Posts
                Thanks for posting that. You should consider making it an extra. wink
                  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
                  • 44599
                  • 4 Posts
                  I'm actually thinking about that. If it's successful I will upload it. Thanks again for the help.