[SOFT REJECT] Script and Style should not be Hardcoded?

Hi guys,

We just got a soft reject from Theme Forest and right now on the way of fixing our themes :). I have trouble with one of the items specified by the reviewer mainly…

Scripts and styles should not be hardcoded anywhere in your theme or added any other way but with wp_enqueue_ hook and to be added from the functions file. This includes custom JS/CSS.*

Question 1

Let’s say I have this simple inline script in a page:

<?php
	$name = 'John';
?>

<script>
	var name = '<?php echo $name; ?>';
	alert( name );
</script>

Since script should not be hardcoded anywhere in theme, except with wp_enqueue_* hook, how do I accomplish that?

Question 2

In the loop, I use CSS background-image to make post-thumbnail fixed-sized.

for example:

<div class="post-thumb" style="background-image:url(<?php echo $post_thumb_url; ?>);">
</div>

Since style also should not be hardcoded in theme, how do I solve this? It’s inside the loop, so it’s unique for each loop.
Or the use of background-image is frowned upon?

  1. https://developer.wordpress.org/reference/functions/wp_add_inline_script/

use that to add any inline JS

  1. https://developer.wordpress.org/reference/functions/wp_add_inline_style/

For inline CSS but it would be added as tags and not “inline” in the html element so would be:

.post-thumb {
 background-image:url($url);
}

That’s not the problem. It’s something else. You can use this

Only tried the inline-js so far but It works!
Thank you Gareth!

Scripts and styles should not be hardcoded anywhere in your theme or added any other way but with wp_enqueue_ hook and to be added from the functions file. This includes custom JS/CSS.*

Btw this statement from the reviewer clearly asking me to put all file related to script and styles inside functions.php file. Does this include scripts and styles added using wp_add_inline_Script and wp_add_inline_style? I will need to use that function on my page template to get the variables.

just checking. Thank you!

Hi,

Did you find the solution?

Thanks