StyleId returns 0

LPH

Well-known member
I have code that functions if styleId is set to 1. But if the user changes to a different style then the code fails.

This works if default is id 1.
PHP:
XenForo_Template_Public::setStyleId( 1 ) ;

If I try to pull the style_id based on the $visitor the print_r is returning 0. Only the default (style_id = 1) is installed.

PHP:
//Get visitor style ID
$visitor = XenForo_Visitor::getInstance();
$styleId = $visitor['style_id'];
echo '<pre>';
print_r($styleId);
echo '</pre>';
XenForo_Template_Public::setStyleId( $styleId ) ;
 
Just to update this thread.

I returned to this code today and it is working right. I have no idea why it was not returning the correct style ID but it is working right in XF 1.4.2.
 
Now to the reason for trying to pull the style ID is to have the online users widget created on the WordPress side. This code renders only if I force the style ID to 1. If the ID is rendered to 4 then the widget is blank.

Works and widget shows:

PHP:
XenForo_Template_Public::setStyleId( 1);

Fails with a blank widget and no error message in logs:

PHP:
XenForo_Template_Public::setStyleId( $styleId );

The print_r($styleId) will return the correct style ID.


Is there an obvious mistake in the code? Is there an obvious reason this returns blank when using a variable?

PHP:
		$dependencies = new XenForo_Dependencies_Public();

		$dependencies->preLoadData();

		$visitor = XenForo_Visitor::getInstance();
		$styleId = $visitor['style_id'];
		// print_r($styleId);

		XenForo_Template_Public::setStyleId( $styleId );

		XenForo_Template_Public::setLanguageId( $styleId );

		/** Start session */
		$sessionModel = $XF->getModelFromCache( 'XenForo_Model_Session' );

		$visitor = XenForo_Visitor::getInstance();

		/** Pull onlineUsers */
		$onlineUsers = $sessionModel->getSessionActivityQuickList( $visitor->toArray(), array(
			'cutOff' => array(
				'>',
				$sessionModel->getOnlineStatusTimeout()
			)
		), ( $visitor['user_id'] ? $visitor->toArray() : null ) );

		$userLimit = '';
		$page = '';
		$userPerPage = '';
		$onlineTotals = '';

		$params = array(
			'onlineUsers'  => $onlineUsers,
			'visitor'      => $visitor,
			'userLimit'    => $userLimit,
			'page'         => $page,
			'usersPerPage' => $userPerPage,
			'onlineTotals' => $onlineTotals
		);

		$template = $dependencies->createTemplateObject( 'sidebar_online_users', $params );

		echo $template->render();
 
:oops: :whistle:

It's 'funny' how I spend time looking at code and cannot figure it out. Only to post here - then see the obvious mistake !

The $styleId cannot be used in setting the language !!

Forcing to 1 in language works. Now, I have to figure out how to grab the correct language variable ...

PHP:
XenForo_Template_Public::setLanguageId( 1 );

Update: Here is the full working code.

PHP:
		/** Start session */
		$sessionModel = $XF->getModelFromCache( 'XenForo_Model_Session' );

		$visitor = XenForo_Visitor::getInstance();

		$styleId = $visitor['style_id'];

		XenForo_Template_Public::setStyleId( $styleId );

		$languageId = $visitor['language_id'];

		XenForo_Template_Abstract::setLanguageId( $languageId );

Maybe this will help someone else.
 
Last edited:
Back
Top Bottom