Programatically change style settings

MGSteve

Well-known member
How easy / hard would it be to change the style settings via a cron job? (i.e. a separate PHP script, not an XF cron job)

Why I hear you ask. Well, on my site we change the key colours of the site style to key into the header image. In VB we do it by overriding the stylesheet with another one, but I'd rather update the style data directly.

How easy / hard is it to do this in XF?
 
I assume you just want to change palette colours, or do you want to edit colours defined specifically within CSS style properties?
 
This should do what you want. Just edit the contents of the $properties array and $styleId to suit your own purpose, then run it with PHP on the command line or via cron.
PHP:
<?php

if (PHP_SAPI != 'cli')
{
	die('This script may only be run at the command line.');
}

if (!class_exists('XenForo_Autoloader', false))
{
	$fileDir = '/full/path/to/xenforo';
	chdir($fileDir);

	require_once($fileDir . '/library/XenForo/Autoloader.php');
	XenForo_Autoloader::getInstance()->
		setupAutoloader($fileDir . '/library');

	XenForo_Application::initialize($fileDir . '/library', $fileDir);
}

$definitions = XenForo_Application::getDb()->fetchPairs('
	SELECT property_name, property_definition_id
	FROM xf_style_property_definition
');

// specify the style you want to work with
$styleId = 4;

// specify the properties you want to update
$properties = array(
	$definitions['pageBackground'] => 'orange',
	$definitions['dimmedTextColor'] => 'hotpink',
	$definitions['primaryDarker'] => 'rgb(0,0,128)',
	$definitions['secondaryMedium'] => '#ff99ff',
	$definitions['imagePath'] => 'styles/myOtherStyle',
);

XenForo_Model::create('XenForo_Model_StyleProperty')->
	saveStylePropertiesInStyleFromInput($styleId, $properties);

echo 'Updated specified properties.' . PHP_EOL;
 
Top Bottom