We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • discuss.answer
    • 51292
    • 18 Posts
    Quote from: donshakespeare at Oct 26, 2015, 12:54 PM
    Place this snippet in your default homepage
    <!--?php
    $from = $_SERVER['HTTP_REFERER']; // note, $_SERVER anything can be forged
    
    if (strpos($from, 'facebook.com') !== false) {
      $id = 12; // id of campaign page to be displayed
      $url = $modx--->makeUrl(); 
      $modx->sendRedirect($url);
    }
    
    if (strpos($from, 'google.com') !== false) {
      $id = 14; // id of campaign page to be displayed
      $url = $modx->makeUrl(); 
      $modx->sendRedirect($url);
    }

    I think you're missing $id in the makeUrl call, but that was useful, however I would like to not redirect the user, to keep the URL alias the same. Is it possible to dynamically render a resource?
      • 3749
      • 24,544 Posts
      I think what you may want is donshakespeare's code, but with $modx->sendForward($id), instead of sendRedirect(). The sendForward method will keep the URL the same and will "pull in" the full content of the page specified. Note that it will use the template and template variable values of the page being pulled in, not the current page.

      You won't need makeUrl() because sendForward() takes a resource ID as its argument.
        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
        • 51292
        • 18 Posts
        Quote from: BobRay at Oct 26, 2015, 01:41 PM
        I think what you may want is donshakespeare's code, but with $modx->sendForward($id), instead of sendRedirect(). The sendForward method will keep the URL the same and will "pull in" the full content of the page specified. Note that it will use the template and template variable values of the page being pulled in, not the current page.

        You won't need makeUrl() because sendForward() takes a resource ID as its argument.

        Thanks, this is exactly what I needed. For reference this is what I ended up with:

        $tvId = 2;
        $persona = "facebook"; //this will come from a cookie value
        
        /* Get the records from the database that match this page slug */
        $query = $modx->newQuery('modTemplateVarResource', array(
            'value' => $modx->resource->GetTVValue($tvId), // get the other pages that match this page
            'tmplvarid' => $tvId,
        ));
        
        $pages = $modx->getCollection('modTemplateVarResource', $query);
        foreach($pages as $page)
        {  
        
            // get the resource object for this relationship
            $page = $modx->getObject('modResource', $page->contentid);
            $page_persona = $page->getTVValue(1);
            // if this resources persona matches the current user's persona, and we're not already viewing the page we should be
            if ($page_persona == $persona && $page->id != $modx->resource->id) {
                  $modx->sendForward($page->id);
            }
        }
          • 3749
          • 24,544 Posts
          That looks great. I was going to suggest getting the modTemplateVarResource objects because it's much faster.
            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
            • 3749
            • 24,544 Posts
            I think this is the only option: http://php.net/manual/en/function.setcookie.php
              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
              • 51292
              • 18 Posts
              Yeah, totally forgot about that. I ended up deleting the post when I remembered. I was also able to find a plugin to do it through the CMS called cookieJar.