Reputation: 987
How to enable JS scripts via custom fields in Wordpress?
I thought it must be sth like this in functions.php:
<?php add_action('wp_footer', 'custom_script');
function custom_script () {
global $wp_query;
$postid = $wp_query->post->ID;
$js get_post_meta($postid, 'customscript', true){
?> <script type="text/javascript"> <?php echo $js; ?> </script > <?php
}
wp_reset_query();
}
?>
But out of some reason, its not working. What am I doing wrong?
And how can I enable to edit this custom field only for admins? Dont know where to use this and if it will do what I am aiming:
if( current_user_can('administrator') {})
Upvotes: 0
Views: 59
Reputation: 1624
Yes you can use the custom fields by checking from the top bar of the WordPress page admin section. However, this is not a good solution as it will likely be confusing after you have multiple custom fields which is sure.
You can use the custom meta box function for this and add the functionality to your page which is easier done through a plugin called advanced custom fields which you already seem to know.
So, if you don't want to use the plugin instead you want to do it custom then there is a function callend add_meta_box. This function allows you to add a seperate box on a specific post_type.
For Quick Tutorial: https://www.sitepoint.com/adding-custom-meta-boxes-to-wordpress/
Hope this gives an overview on meta boxes.
--- OR ---
There is many ways to enable JS via custom fields in wordpress. you can go the link, you can get more idea as per your functionality.
enable JS via custom fields in wordpress
Upvotes: 1