I’m trying to add custom HTML, CSS and JQuery code to certain wordpress posts but I don’t know if I’m using the right approach since I am just adding the code right into the post.Because there is going to be more posts which may need to use the custom code and with this approach I have to copy/past and customize the same code to those posts too.
Is there maybe a better approach to do this?
I don’t know much about creating wordpress plugins but an idea tells me plugins are the right way to go, If so how can I turn this to a plugin for wordpress?
Here is an example of the code:
<p style="text-align: left;">Post begins here and this is the text...
<div class="myDiv" >button</div>
<style type="text/css">
.myDiv{
color: #800080;
border: #000;
border-radius: 20px;
border-style: solid;
width: 50px;
}
</style>
<script type="text/javascript">
<!--
$(".farzn").on("click", function(){
alert('its Working');
});
//--></script>
Writing a Plugin is quite easy: create a PHP file with the following content:
Upload it to your
wp-content/plugins
folder and it will show up in the plugins list.And now the fun, the hooks
wp_head
andwp_footer
can be used for small inline styles and scripts. Check Conditional Tags for all filtering possibilities.The best practice is to enqueue all your styles and scripts as separate files with the action hook
wp_enqueue_scripts
. Conditional Tags can also be used.