• Welcome to Tux Reports: Where Penguins Fly. We hope you find the topics varied, interesting, and worthy of your time. Please become a member and join in the discussions.

Building XenLate

XenLate is a simple addon to show WordPress posts in the sidebar of XenForo. The first release simply added a listener to the model. A template did a xen:callback. Everything was very basic and the model was actually jammed with HTML as well as PHP code. Nothing was clean but it moved the idea into an addon.

Today I released 1.1.0 and wanted to go through the files in case someone else wanted to see how the addon was built.

XenLate 1.1 New CSS.png

Image shows new CSS added after XenLate 1.1.0 was uploaded.

The Listener

PHP:
<?php

/**
* Class TRN_XenLate_Listener_Index
*/
class TRN_XenLate_Listener_Index
{
   /**
    * @param $hookName
    * @param $contents
    * @param array $hookParams
    * @param XenForo_Template_Abstract $template
    */
   public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
   {
      if ($hookName == 'ad_sidebar_below_visitor_panel')
      {
         /** @var  $LatestWPPostsModel TRN_XenLate_Model_WPPosts */
         $LatestWPPostsModel = XenForo_Model::create('TRN_XenLate_Model_WPPosts');
         $LatestWPPosts      = $LatestWPPostsModel->getPosts();
         $LatestPosts     = $LatestWPPostsModel->preparePosts($LatestWPPosts);

         $viewParams = array(
            'latestposts' => $LatestPosts,
         ); // Pass variables to template

         $trn_xenword_contents = $template->create('trn_xenlate_latestposts', $viewParams);
         $contents = $trn_xenword_contents . $contents;
      }
   }
}

The model

PHP:
<?php
/**
* Filename: WPPosts.php
* Author: LPH
*/

/**
* Class TRN_XenLate_Model_WPPosts
*/
class TRN_XenLate_Model_WPPosts extends XenForo_Model
{
    /**
     * @return array|false
     */
    public function getPosts()
    {

        define('WP_USE_THEMES', false);
        define('DOCUMENT_ROOT', $_SERVER['DOCUMENT_ROOT']);

        // Change the path to the correct location
        require(DOCUMENT_ROOT . '/wp-blog-header.php');

        $args = array(
            'numberposts' => '5',
            'offset'      => 0,
            'post_type'   => 'post',
            'post_status' => 'publish'
        );

        $LatestWPPosts = wp_get_recent_posts($args);

        return $LatestWPPosts;
    }

   /**
    * @param $LatestWPPosts
    *
    * @return string
    */
   public function preparePosts(array $LatestWPPosts)
   {
      $LatestPosts = array();
      foreach($LatestWPPosts as &$LatestWPPost)
      {
         $LatestPostTitle = $LatestWPPost['post_title'];
         $LatestPostUrl = esc_url(get_permalink($LatestWPPost['ID']));
         $LatestPostExcerpt = $LatestWPPost['post_excerpt'];
         $LatestPostThumbnail = get_the_post_thumbnail_url($LatestWPPost['ID'], array(50, 50));
         $LatestPosts[] = array( 'postTitle' => $LatestPostTitle, 'postUrl' => $LatestPostUrl , 'postExcerpt' => $LatestPostExcerpt, 'postThumbnailUrl' => $LatestPostThumbnail ) ;
      }

      return $LatestPosts;
   }
}

// End WPPosts.php

Templates

Code:
<xen:require css="trn_xenlate_latestposts.css" />

<div class="section">
    <div class="secondaryContent">
        <div class="visitorText">
        <h3>Latest Articles</h3>
            <div class="LatestWPPosts">
                <xen:include template="trn_xenlate_latestpost_items" />
            </div>
        </div>
    </div>
</div>

Code:
<xen:foreach loop="$latestposts" value="$latestpost">

    <div class="recent_post">
        <xen:if is="{$latestpost.postThumbnailUrl} != ''">
            <div class="thumbnail"><img src="{$latestpost.postThumbnailUrl}"></div>
        </xen:if>
 
        <h4><a href="{$latestpost.postUrl}">{$latestpost.postTitle}</a></h4>
    
        <div class="excerpt">{$latestpost.postExcerpt}</div>

    </div>

</xen:foreach>

Feel free to download the addon and try it on your site.
  • Like
Reactions: robru

Blog entry information

Author
LPH
Views
1,676
Last update

More entries in General

More entries from LPH

Top