I want to build a widget by which various social media sharing buttons can be added to front end. In the widget form there will be many checkboxes for social media. Since these sharing buttons require different scripts or html tags I have to enqueue it from back end. While enqueueing I have to check which checkbox(s) are selected. But how can I get those values from outside of my widget class? Say my code is as follows:
<?php
class My_Widget extends WP_Widget {
public function __construct() {
// some stuff goes here
}
public function form( $instance ) {
?>
<label for="<?php echo $this->get_field_name( 'facebook' ); ?>">Facebook</label>
<input type="checkbox" id="<?php echo $this->get_field_id( 'facebook' ); ?>" name="<?php echo $this->get_field_name( 'facebook' ); ?>" value="facebook">
<br>
<label for="<?php echo $this->get_field_name( 'twitter' ); ?>">Twitter</label>
<input type="checkbox" id="<?php echo $this->get_field_id( 'twitter' ); ?>" name="<?php echo $this->get_field_name( 'twitter' ); ?>" value="twitter">
<?php
/* And I also take some input such an icon image
that will be used to render widget in front end */
}
public function widget( $args, $instance ) {
/* Here I render widget output
such as social icon. But I also have to enqueu
some scripts(based on what user selected/checked)
some maybe should be added just after
opening body tag or in head and some may be
in footer. In footer it can be added from here
but in head or after opening body tag
it cannot be because it is too late
*/
}
public function update( $new_instance, $old_instance ) {
// saving process goes here
}
}
?>
wp_register_script()
but then enqueue them usingwp_enqueue_script()
within the widget's output using conditionalif()
checks. Provide your widget code and it'll be a lot easier for someone to help you.