javascript - Wordpress plugin: how do you pass parameters to be used as attributes in a javascrtipt tag? -


i'm trying write widget plugin in wordpress creates javascript tag , drops in footer of page. know can use wp_enqueue_script create this, no problem. need pass parameter widget settings script tag.

example:

wp_enqueue_script( 'script-name', '/js/example.js' );

will render tag looks this:

<script type='text/javascript' src='/js/example.js'></script>

what want create tag looks this:

<script type='text/javascript' data-id='12345' src='/js/example.js'></script>

where 12345 passed in widget settings.

how do this?

here figured out. in widget code, create front-end display piece, use this:

add_action('wp_footer',create_function( '', 'echo(\'<script type="text/javascript" data-id="' . $instance['widgetparamname'] . '" src="//someurl.com/some.js"></script>\');' ),1,0); 

$instance passed in already, , contains values widget's admin interface. whatever value put widget, show in array. refer correct element of array.

what code ads action wp_footer hook without requiring function (to cannot pass values) , whatever put in here written in footer area of site.

its worth noting first parameter create_function must empty, or wordpress throw error.


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -