How to properly apply WordPress's theme options to CSS and JavaScript in a page?

Thanks guys! It’s very clear now for css application. :slight_smile:

What would you suggest about applying options from PHP to javascript codes? Does it use the same way of css? To echo extra that includes all var for options to the header too?

This may be a silly question but I’m just not familiar with manipulating works between them.

UXbarn said

What would you suggest about applying options from PHP to javascript codes?

http://ottopress.com/2010/passing-parameters-from-php-to-javascripts-in-plugins/
pixelentity said
UXbarn said

What would you suggest about applying options from PHP to javascript codes?

http://ottopress.com/2010/passing-parameters-from-php-to-javascripts-in-plugins/

And now i have a question for you :slight_smile: i generate the file with php and it seems that there is a better way. How do you pass the variables exactly? …/script.js?param1=hello&param2=world ? And how do you get them inside the script?

duotive said

And now i have a question for you :slight_smile: i generate the file with php and it seems that there is a better way. How do you pass the variables exactly?




here’s an example: we have a js script registered under the id “pe_theme_video”, after that, we call
wp_localize_script("pe_theme_video",'peFallBackPlayer',array("url"=>$url)));




WordPress creates the “peFallBackPlayer” variable (object) right before the related script (pe_theme_video) gets loaded:





Then, in the js file, you can fetch the value:

var fbp = decodeURIComponent(window.peFallBackPlayer.url);

BF

duotive said
Lion said

For custom CSS styling I generate + minify css file and save it on server only when user clicks on ‘Save Changes’ button in theme options, so there are no additional WP queries made on each page view.

How many customers had issues with this? I can bet that many did not have the permission set for the file/folder and windows servers will bust the writing process + get_files_content restriction in effect, those are a few problems that i can say for sure that you have with this.

Actually so far no one had issue with this method. Another good way will be to store generated CSS in database and output it with single echo.
I dont see any reason to call so many queries on DB to generate CSS.

:slight_smile:

pixelentity said
UXbarn said

What would you suggest about applying options from PHP to javascript codes?

http://ottopress.com/2010/passing-parameters-from-php-to-javascripts-in-plugins/

Thank you so much @pixelentity. You just saved my life! :smitten:

Thanks BitFade

for css… http://codex.wordpress.org/Function_Reference/wp_add_inline_style

<?php
function my_styles_method() {
	wp_enqueue_script(
		'custom-style',
		get_template_directory_uri() . '/css/custom_script.css'
	);
        $color = get_option('my_custom_color'); 
        $custom_css = "
                .mycolor{
                        background: {$color};
                }";
        wp_add_inline_style('custom-style',$custom_css);
}
add_action('wp_enqueue_scripts', 'my_styles_method');
?>

for js… http://codex.wordpress.org/Function_Reference/wp_localize_script

OrganicBeeMedia, thanks for the info, so, what do u think, it’s the best way to use wp_add_inline_style?

Pixelous said

OrganicBeeMedia, thanks for the info, so, what do u think, it’s the best way to use wp_add_inline_style?

Im not sure I’d say its the best way but its the most secure…

it also relies on a stylesheet so if some one want to de-register it from a child theme it will remove the options etc… and they can apply there own

now if your theme just needs to change a few colors/fonts etc then yes its viable but if your themes are adding 4339428434832843248 options to do everything then not so much(which I dont recommend in the first place bloat)

if youre messing with the headers to create a style.php file then just make sure you’re passing the data in a secure manor so it doesn’t leave all your customers sites wide open to get hacked(XSS)

Please do not write below this line

Thank You for contacting Pixelentity,

If you need support on a purchased item, head to our support forum: http://support.pixelentity.com
If you can’t find an existing solution, register and open a new thread in the forum matching your purchased item.

If your question is related to any other matter then you don’t need to do anything else, we will reply within 48 hours.

Best regards,
The pixelentity team