comment 0

add_shortcode not working

Heyho !

Today I change my site themes and upgrade the system ( currently I am using wordpress ) to latest version. While I migrating to the latest wordpress version (4.1.1) I had a little probelm on my shortcode function. the add_shortcode isn’t working. Then I check on wordpres codex ( https://codex.wordpress.org/Function_Reference/add_shortcode ), there’s no new update on this pages. So, I tried to register my add_shortcode function to init.
here is the old code

function myAge() {
return'
<script type="text/javascript">
var days = (new Date() - new Date(1990, 6, 21)) / (1000 * 60 * 60 * 24);
document.write(Math.round(days)+" days old");
</script>
';
}
add_shortcode('showAge', 'myAge');


Sponsored links



that’s my custom function to show how old me on about pages.
and here is how to fix the problem , we register our shortcode to init level

add_action('init', 'add_myAge_shortcode', 97);
function add_myAge_shortcode(){
	add_shortcode('showAge', 'myAge');
}
function myAge($attr) {
return'
<script type="text/javascript">
var days = (new Date() - new Date(1990, 6, 21)) / (1000 * 60 * 60 * 24);
document.write(Math.round(days)+" days old");
</script>
';
}

Hopefully this will help. Feel free to add any comment or question on the topic.

Leave a Reply

Your email address will not be published. Required fields are marked *